Options
All
  • Public
  • Public/Protected
  • All
Menu

Class AuthenticationEngine<Account>

Authentication Engine manages user authentication and account.

Type parameters

Hierarchy

  • AuthenticationEngine

Constructors

constructor

Methods

activateAccount

  • activateAccount(activateAccountToken: string): Promise<void>
  • Activate previously registered account.

    throws

    {Exception} With code:
    - ErrorCodes.SESSION_NOT_FOUND when token is not valid
    - ErrorCodes.ACCOUNT_WITH_DUPLICATED_FIELDS when account contains duplicated fields. This scenario might occur because unactivated accounts are stored in a temporary storage ActivateAccountSessionRepository and while user tries to activate his account, another user was able to register an account with same fields and activate it before (very unlikely, but still might happen). In case this exception is thrown, user account registration is considered failed and user to register another account.

    Parameters

    • activateAccountToken: string

      Activate account token.

    Returns Promise<void>

authenticate

changeForgottenPassword

  • changeForgottenPassword(token: string, newPassword: string): Promise<void>

changePassword

  • changePassword<Context>(changePasswordContext: Context): Promise<void>

createForgotPasswordSession

  • createForgotPasswordSession(accountUniqueFieldName: `readBy${Capitalize}`, accountUniqueFieldValue: string, sideChannel: "email" | "sms"): Promise<Account>
  • Creates forgot password session and starts account recovery procedure.
    Creates forgot password token and sends it via specified sideChannel.
    If account has AccountModel.pubKey, token will be encrypted before being sent to user. In this case user needs to decrypt token with his Private Key and send plaintext token back to server.
    In case any exception is thrown, operation is considered failed.

    Important! When account not found err is thrown, REST API should not respond with error, in order to prevent user enumeration, i.e it should respond with ok (something like token was sent to specified email, even if account doesn't exist).

    throws

    {Exception} With code:
    - ErrorCodes.ACCOUNT_NOT_FOUND when account unique field value is not valid
    - ErrorCodes.ACCOUNT_DISABLED when account is disabled
    - ErrorCodes.NO_TELEPHONE_NUMBER when account has no telephone number
    - ErrorCodes.UNKNOWN_CREATE_FORGOT_PASSWORD_SESSION_SIDE_CHANNEL when invalid sideChannel was provided.

    Parameters

    • accountUniqueFieldName: `readBy${Capitalize}`

      Name of the account field, value of which user remembers.

    • accountUniqueFieldValue: string

      Value of that field.

    • sideChannel: "email" | "sms"

      Side channel used to send token to user.

    Returns Promise<Account>

    User account.

disableAccount

  • disableAccount(accountId: string, until: number, cause: string): Promise<void>
  • Disable user account.

    Important! This operation should be done only by admin, or by application's business logic.

    Parameters

    • accountId: string

      Account id.

    • until: number

      Unix timestamp until account will be disabled.
      Use {@link UnixTimestamp} or AccountStatus.DISABLED_UNTIL_ACTIVATION to disable account for an undefined amount of time.

    • cause: string

      String message that contains explanation why account is disabled.

    Returns Promise<void>

enableAccount

  • enableAccount(accountId: string): Promise<void>
  • Enable user account.

    Important! This operation should be done only by admin.

    Parameters

    • accountId: string

      Account id.

    Returns Promise<void>

getFailedAuthentications

  • getFailedAuthentications(accountId: string, startingFrom?: number, endingTo?: number): Promise<FailedAuthenticationModel[]>
  • Get failed authentication attempts into user account.

    Important! This method has authorization implications. It needs to be called only by authenticated users for their account or by admin.

    Parameters

    • accountId: string

      Account id.

    • Optional startingFrom: number

      Starting timestamp.

    • Optional endingTo: number

      Ending timestamp.

    Returns Promise<FailedAuthenticationModel[]>

getSuccessfulAuthentications

  • Get successful authentications into user account.

    Important! This method has authorization implications. It needs to be called only by authenticated users for their account or by admin.

    Parameters

    • accountId: string

      Account id.

    • Optional startingFrom: number

      Starting timestamp.

    • Optional endingTo: number

      Ending timestamp.

    Returns Promise<SuccessfulAuthenticationModel[]>

register

setTwoFactorAuthEnabled

verifyPassword

  • verifyPassword(password: string, account: Account, context: BaseContext): Promise<boolean>
  • Verifies user password.

    throws

    {Exception} With code:
    - ErrorCodes.ACCOUNT_DISABLED when invalid password was provided too many times

    Parameters

    • password: string

      Password provided by user.

    • account: Account

      User account.

    • context: BaseContext

      Operation context.

    Returns Promise<boolean>

    Whether password is valid or not.