import QRCode from 'qrcode' import type { BaseCommand } from '@adonisjs/core/ace' import totpService from '#services/totp_service' /** * Shows the QR code of a new TOTP secret, then asks for a code to make * sure the authenticator app is set up. Returns the encrypted secret. */ export async function enrollTotp(command: BaseCommand, username: string): Promise { const totp = totpService.generate(username) command.logger.info( 'Scanne ce QR code avec ton application de double authentification (Aegis, 2FAS, Google Authenticator…) :' ) console.log(await QRCode.toString(totp.uri, { type: 'terminal', small: true })) command.logger.info(`Ou saisis la clé à la main : ${totp.base32}`) for (let attempt = 1; attempt <= 3; attempt++) { const code = await command.prompt.ask('Code à 6 chiffres affiché par l’application') if (totpService.check(totp.encrypted, username, code.trim()) !== null) return totp.encrypted command.logger.warning('Code incorrect, réessaie (vérifie l’heure du téléphone).') } command.logger.error('Trop d’essais : rien n’a été enregistré.') return null }