UNPKG

@enteocode/nestjs-mfa

Version:

Implementation agnostic RFC-compliant Multi-Factor Authentication (2FA/MFA) module for NestJS with recovery code support

76 lines (75 loc) 2.42 kB
import { StreamableFile } from '@nestjs/common'; import { OtpService } from './otp.service'; import { StorageService } from './storage.service'; import { Identifier, Token, TokenOptions } from './types'; import { EventEmitter2 as EventEmitter } from '@nestjs/event-emitter'; import { TokenType } from './token.type'; import { Format } from './qr/qr-code.format'; import { QrCodeService } from './qr/qr-code.service'; import type { MfaModuleOptionsInterface } from './mfa.module.options.interface'; import type { SecretKey } from '@otplib/core'; export declare class MfaService { private readonly options; private readonly otp; private readonly storage; private readonly qr; private readonly emitter; private readonly logger; constructor(options: MfaModuleOptionsInterface, otp: OtpService, storage: StorageService, qr: QrCodeService, emitter: EventEmitter); /** * Checks if Multi-Factor Authentication is enabled * * @public * @param user */ isEnabled(user: Identifier): Promise<boolean>; /** * Enable Multi-Factor Authentication * * @public * @param user */ enable(user: Identifier): Promise<SecretKey>; /** * Disable Multi-Factor Authentication (if it was enabled) * * @public * @param user */ disable(user: Identifier): Promise<boolean>; /** * Check the 6-digit token * * @public * @param user * @param token */ verify(user: Identifier, token: Token): Promise<void>; /** * Generates a 6-digit token for timeout-based authentication (useful for email-based validation) * * @public * @param user * @param type * @param options */ generate(user: Identifier, type: TokenType.TIMEOUT, options?: TokenOptions): Promise<Token>; /** * Generates a KeyURI for Authenticator (if QR code is generated at client-side) * * @public * @param user * @param type * @see https://github.com/google/google-authenticator/wiki/Key-Uri-Format */ generate(user: Identifier, type: TokenType.AUTHENTICATOR): Promise<string>; /** * Generates a QR code (stream to minimize memory usage) * * @public * @param user * @param type * @param format */ generate(user: Identifier, type: TokenType.AUTHENTICATOR, format: Format): Promise<StreamableFile>; }