import * as Hapi from '@hapi/hapi' ; import * as Joi from 'joi' ; import { verifySecurityAnswer , IVerifySecurityAnswerResponse } from './service' ; import { unauthorized } from '@hapi/boom' ; import { getRetrievalStepInformation , RetrievalSteps , storeRetrievalStepInformation } from '@auth/features/retrievalSteps/verifyUser/service' ; interface IPayload { answer : string ; nonce : string ; } interface IResponse { matched : boolean ; nonce : string ; securityQuestionKey ? : string ; } export default async function verifySecurityQuestionHandler ( request , h ) { const payload = request . payload as IPayload ; const retrievalStepInformation = await getRetrievalStepInformation ( payload . nonce ) . catch ( ( ) => { throw unauthorized ( ) ; } ) ; if ( retrievalStepInformation . status !== RetrievalSteps . NUMBER_VERIFIED . toString ( ) ) { return unauthorized ( ) ; } let verificationResult ; try { verificationResult = await verifySecurityAnswer ( retrievalStepInformation . userId , retrievalStepInformation . securityQuestionKey , payload . answer ) ; } catch ( err ) { return unauthorized ( ) ; } await storeRetrievalStepInformation ( payload . nonce , verificationResult . matched ? RetrievalSteps . SECURITY_Q_VERIFIED : RetrievalSteps . NUMBER_VERIFIED , { ... retrievalStepInformation , securityQuestionKey : verificationResult . questionKey } ) ; const response = { matched : verificationResult . matched , nonce : payload . nonce } ; if ( ! verificationResult . matched ) { response . securityQuestionKey = verificationResult . questionKey ; } return response ; } export const verifySecurityQuestionSchema = Joi . object ( { answer : Joi . string ( ) . required ( ) , nonce : Joi . string ( ) . required ( ) } ) ; export const verifySecurityQuestionResSchema = Joi . object ( { matched : Joi . bool ( ) . required ( ) , securityQuestionKey : Joi . string ( ) . optional ( ) , nonce : Joi . string ( ) . required ( ) } ) ;