Files
big-brother/api/app/services/totp_enrollment.ts
T
2026-10-12 14:25:22 +02:00

27 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string | null> {
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
}