import { User, JSONWebToken, Credential } from '@digitalpersona/core';
import { IAuthService } from '@digitalpersona/services';
import { Authenticator } from '../../private';

/**
 * Personal Identification Number (PIN) authentication API.
 * PIN support only authentication. Identification is not supported.
 */
export class PinAuth extends Authenticator
{
    /** Constructs a new PIN authentication API object.
     * @param authService - an {@link AuthService|authentication service client} connected to the server.
     */
    constructor(authService: IAuthService) {
        super(authService);
    }

    /** Authenticates the user using user's PIN code.
     * @param identity - a {@link User |username} or a JSON Web Token.
     * @param pin - a user's PIN.
     * @returns a promise to return a JSON Web Token containing a claim (`crd`) showing the fact
     * a PIN was used.
     * Will reject if authentication fails.
     * @remarks
     * If the `identity` parameter is a user name, a new token will be created.
     * If the `identity` parameter is a JSON Web Token, an updated token will be returned.
     */
    public authenticate(
        identity: User|JSONWebToken,
        pin: string,
    ): Promise<JSONWebToken>
    {
        return super._authenticate(identity, new Credential(Credential.PIN, pin));
    }
}
