40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
|
|
import * as plugins from '../plugins.js';
|
||
|
|
import type { MfaManager } from './classes.mfamanager.js';
|
||
|
|
|
||
|
|
@plugins.smartdata.Manager()
|
||
|
|
export class MfaChallenge extends plugins.smartdata.SmartDataDbDoc<MfaChallenge, any, MfaManager> {
|
||
|
|
public static hashToken(tokenArg: string) {
|
||
|
|
return plugins.smarthash.sha256FromStringSync(tokenArg);
|
||
|
|
}
|
||
|
|
|
||
|
|
@plugins.smartdata.unI()
|
||
|
|
public id: string;
|
||
|
|
|
||
|
|
@plugins.smartdata.svDb()
|
||
|
|
public data = {
|
||
|
|
userId: '',
|
||
|
|
tokenHash: '',
|
||
|
|
status: 'pending' as 'pending' | 'completed' | 'expired',
|
||
|
|
availableMethods: [] as Array<'totp' | 'backupCode' | 'passkey'>,
|
||
|
|
primaryAuthMethod: 'password' as 'password' | 'email',
|
||
|
|
createdAt: 0,
|
||
|
|
expiresAt: 0,
|
||
|
|
completedAt: null as number | null,
|
||
|
|
};
|
||
|
|
|
||
|
|
public isExpired(nowArg = Date.now()) {
|
||
|
|
return this.data.expiresAt < nowArg;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async markCompleted() {
|
||
|
|
this.data.status = 'completed';
|
||
|
|
this.data.completedAt = Date.now();
|
||
|
|
await this.save();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async markExpired() {
|
||
|
|
this.data.status = 'expired';
|
||
|
|
await this.save();
|
||
|
|
}
|
||
|
|
}
|