import * as otplib from 'otplib' ; import { OperatorUserData } from '../../common/types/operator-user' ; const MIN_REMAINING_TIME = 5 ; async function generateTotp ( key ) { return new Promise ( ( resolve ) => { const ttl = otplib . authenticator . timeRemaining ( ) ; const wait = ttl < MIN_REMAINING_TIME ? ttl + 1 : 0 ; setTimeout ( ( ) => { resolve ( otplib . authenticator . generate ( key ) ) ; } , wait * 1000 ) ; } ) ; } export default class OperatorUser { public username : string ; public password : string ; private totpSecret : string ; public role : string ; constructor ( userData ) { this . username = userData . username ; this . password = userData . password ; this . totpSecret = userData . secretTotp ; this . role = userData . role ; } private async getTotp ( ) { return generateTotp ( this . totpSecret ) ; } wrapTotp ( ) { const timeout = ( MIN_REMAINING_TIME + 1 ) * 1000 ; return cy . wrap ( this . getTotp ( ) , { timeout } ) ; } }