name: two-factor-auth-plugin title: Two-Factor Authentication description: Vendure plugin that adds two-factor authentication (TOTP authenticator apps and backup codes) to administrator sign-in, in the Admin API and the React Dashboard. version: 3.8.0 tags:
Adds a second factor to administrator sign-in: a 6-digit code from an authenticator app (TOTP, RFC 6238), with single-use backup codes for a lost device.
An administrator who has turned it on never gets a session from the password alone. The password step returns a short-lived challenge, and only the challenge plus a valid code creates a session.
TwoFactorEvent for enrolment, disabling, resets, backup code use, wrong codes and lockoutsCustomer sign-in on the Shop API is not affected.
DashboardPlugin from @vendure/dashboard/plugin. The legacy Angular Admin UI is not supported: with this plugin, an administrator who has turned 2FA on cannot sign in there.Generate an encryption key and keep it with your other secrets:
The plugin adds two database tables (two_factor_credential, two_factor_challenge). Generate and run a migration after adding it. Rebuild the Dashboard, since the plugin ships a Dashboard extension.
| Option | Type | Default | Description |
|---|---|---|---|
encryptionKeys | string[] | none | Required. Base64-encoded 32-byte keys. The first encrypts; older keys after it still decrypt (see Key rotation). |
issuer | string | Vendure | The name shown in authenticator apps. |
required | boolean | (ctx, user) => boolean | Promise<boolean> | false | Whether an administrator must use 2FA. The function gets the User with its roles loaded. |
challengeTtlSeconds | number | 300 | How long the challenge from the password step stays valid. |
maxFailedAttempts | number | 5 | Wrong codes in a row before the second factor is locked. |
lockoutSeconds | number | 900 | How long the lock lasts. |
backupCodeCount | number | 10 | How many backup codes to issue. |
Require it for every administrator with a given role:
Put a new key first and keep the old one after it. Secrets made with the old key are re-encrypted with the new one the next time their owner signs in. Remove the old key only when no secret uses it; a secret whose key is gone can no longer be decrypted, and its owner needs a reset.
native authentication strategy and keeps its name. For an administrator who has turned 2FA on, or who must, a correct password is refused with authenticationError: "TWO_FACTOR_REQUIRED". login and authenticate(input: { native }) therefore never create a session for them.twoFactorLogin(username, password) mutation checks the password and returns a status:
NOT_REQUIRED: sign in with login as usual.CODE_REQUIRED: a challengeToken to redeem with a code.ENROLMENT_REQUIRED: a challengeToken plus a new secret and QR code. Confirm with completeTwoFactorLoginEnrolment(challengeToken, code), which returns the backup codes.INVALID_CREDENTIALS.authenticate mutation with the twoFactor strategy, which creates the session:authenticationError is one of TWO_FACTOR_INVALID_CODE, TWO_FACTOR_CHALLENGE_EXPIRED, TWO_FACTOR_LOCKED or TWO_FACTOR_ENROLMENT_INCOMPLETE. code takes a TOTP code or a backup code; it is not needed after a confirmed enrolment.
Signed-in administrators manage their own second factor with activeAdministratorTwoFactor, beginTwoFactorEnrolment(password), confirmTwoFactorEnrolment(code), disableTwoFactor(code) and regenerateTwoFactorBackupCodes(code). Turning it on or off signs out the administrator's other sessions. A required administrator cannot turn it off.
resetAdministratorTwoFactor(administratorId) removes another administrator's second factor and signs them out everywhere. It needs the ResetTwoFactor permission (superadmins have it). Administrator.twoFactorEnabled shows who has it on.
/two-factor-login, which signs in every administrator, with or without a second factor.Core's resetPassword signs the administrator in with the new password. The plugin replaces that resolver: the password is reset as before, and a session is only created when no second factor applies. Otherwise the result is the administrator without a session, and they sign in with the second factor.
ResetTwoFactor uses Reset 2FA.authOptions.disableAuth turns all of this off; the plugin logs a warning at startup.