import { MfaAuthenticateChallenge , MfaRegistrationChallenge } from './types' ; import { base64urlToBuffer , bufferToBase64url } from 'shared/utils/base64url' ; export function makeMfaRegistrationChallenge ( json ) { const webauthnPublicKey = json . webauthn ?. publicKey ; if ( webauthnPublicKey ) { const challenge = webauthnPublicKey . challenge || '' ; const id = webauthnPublicKey . user ?. id || '' ; const excludeCredentials = webauthnPublicKey . excludeCredentials || [ ] ; webauthnPublicKey . challenge = base64urlToBuffer ( challenge ) ; webauthnPublicKey . user . id = base64urlToBuffer ( id ) ; webauthnPublicKey . excludeCredentials = excludeCredentials . map ( ( credential , i ) => { excludeCredentials [ i ] . id = base64urlToBuffer ( credential . id ) ; return excludeCredentials [ i ] ; } ) ; } return { u2fRegisterRequest : json . u2f , qrCode : json . totp ?. qrCode , webauthnPublicKey , } ; } export function makeMfaAuthenticateChallenge ( json ) { const webauthnPublicKey = json . webauthn_challenge ?. publicKey ; if ( webauthnPublicKey ) { const challenge = webauthnPublicKey . challenge || '' ; const allowCredentials = webauthnPublicKey . allowCredentials || [ ] ; webauthnPublicKey . challenge = base64urlToBuffer ( challenge ) ; webauthnPublicKey . allowCredentials = allowCredentials . map ( ( credential , i ) => { allowCredentials [ i ] . id = base64urlToBuffer ( credential . id ) ; return allowCredentials [ i ] ; } ) ; } return { u2f : { appId : json . appId , challenge : json . challenge , registeredKeys : json . u2f_challenges || [ ] , } , webauthnPublicKey : webauthnPublicKey , } ; } export function makeWebauthnCreationResponse ( res ) { if ( ! res ) { throw new Error ( 'error creating credential, please try again' ) ; } const clientExtentions = res . getClientExtensionResults ( ) ; return { id : res . id , type : res . type , extensions : { appid : Boolean ( clientExtentions ?. appid ) , } , rawId : bufferToBase64url ( res . rawId ) , response : { attestationObject : bufferToBase64url ( res . response ?. attestationObject ) , clientDataJSON : bufferToBase64url ( res . response ?. clientDataJSON ) , } , } ; } export function makeWebauthnAssertionResponse ( res ) { if ( ! res ) { throw new Error ( 'error obtaining credential, please try again' ) ; } const clientExtentions = res . getClientExtensionResults ( ) ; return { id : res . id , type : res . type , extensions : { appid : Boolean ( clientExtentions ?. appid ) , } , rawId : bufferToBase64url ( res . rawId ) , response : { authenticatorData : bufferToBase64url ( res . response ?. authenticatorData ) , clientDataJSON : bufferToBase64url ( res . response ?. clientDataJSON ) , signature : bufferToBase64url ( res . response ?. signature ) , userHandle : bufferToBase64url ( res . response ?. userHandle ) , } , } ; }