41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { BaseCommand, args } from '@adonisjs/core/ace'
|
||
import type { CommandOptions } from '@adonisjs/core/types/ace'
|
||
|
||
/**
|
||
* New double authentication secret (lost phone, new app…).
|
||
*/
|
||
export default class UserReset2fa extends BaseCommand {
|
||
static commandName = 'user:reset-2fa'
|
||
static description = 'Réinitialiser la double authentification d’un compte'
|
||
|
||
static options: CommandOptions = {
|
||
startApp: true,
|
||
}
|
||
|
||
@args.string({ description: 'Identifiant du compte' })
|
||
declare username: string
|
||
|
||
async run() {
|
||
const { default: User } = await import('#models/user')
|
||
const { enrollTotp } = await import('#services/totp_enrollment')
|
||
|
||
const user = await User.findBy('username', this.username)
|
||
if (!user) {
|
||
this.logger.error(`Compte « ${this.username} » introuvable.`)
|
||
this.exitCode = 1
|
||
return
|
||
}
|
||
|
||
const totpSecret = await enrollTotp(this, user.username)
|
||
if (!totpSecret) {
|
||
this.exitCode = 1
|
||
return
|
||
}
|
||
|
||
user.totpSecret = totpSecret
|
||
user.totpLastStep = null
|
||
await user.save()
|
||
this.logger.success('Double authentification réinitialisée.')
|
||
}
|
||
}
|