@@ -25,9 +25,9 @@ export function initUserAuthWebAuthn() {
2525
2626 $ . getJSON ( `${ appSubUrl } /user/webauthn/assertion` , { } )
2727 . done ( ( makeAssertionOptions ) => {
28- makeAssertionOptions . publicKey . challenge = decodeFromBase64 ( makeAssertionOptions . publicKey . challenge ) ;
28+ makeAssertionOptions . publicKey . challenge = decodeURLEncodedBase64 ( makeAssertionOptions . publicKey . challenge ) ;
2929 for ( let i = 0 ; i < makeAssertionOptions . publicKey . allowCredentials . length ; i ++ ) {
30- makeAssertionOptions . publicKey . allowCredentials [ i ] . id = decodeFromBase64 ( makeAssertionOptions . publicKey . allowCredentials [ i ] . id ) ;
30+ makeAssertionOptions . publicKey . allowCredentials [ i ] . id = decodeURLEncodedBase64 ( makeAssertionOptions . publicKey . allowCredentials [ i ] . id ) ;
3131 }
3232 navigator . credentials . get ( {
3333 publicKey : makeAssertionOptions . publicKey
@@ -67,14 +67,14 @@ function verifyAssertion(assertedCredential) {
6767 type : 'POST' ,
6868 data : JSON . stringify ( {
6969 id : assertedCredential . id ,
70- rawId : bufferEncode ( rawId ) ,
70+ rawId : bufferURLEncodedBase64 ( rawId ) ,
7171 type : assertedCredential . type ,
7272 clientExtensionResults : assertedCredential . getClientExtensionResults ( ) ,
7373 response : {
74- authenticatorData : bufferEncode ( authData ) ,
75- clientDataJSON : bufferEncode ( clientDataJSON ) ,
76- signature : bufferEncode ( sig ) ,
77- userHandle : bufferEncode ( userHandle ) ,
74+ authenticatorData : bufferURLEncodedBase64 ( authData ) ,
75+ clientDataJSON : bufferURLEncodedBase64 ( clientDataJSON ) ,
76+ signature : bufferURLEncodedBase64 ( sig ) ,
77+ userHandle : bufferURLEncodedBase64 ( userHandle ) ,
7878 } ,
7979 } ) ,
8080 contentType : 'application/json; charset=utf-8' ,
@@ -96,14 +96,21 @@ function verifyAssertion(assertedCredential) {
9696 } ) ;
9797}
9898
99- // Encode an ArrayBuffer into a base64 string.
100- function bufferEncode ( value ) {
99+ // Encode an ArrayBuffer into a URLEncoded base64 string.
100+ function bufferURLEncodedBase64 ( value ) {
101101 return encodeToBase64 ( value )
102102 . replace ( / \+ / g, '-' )
103103 . replace ( / \/ / g, '_' )
104104 . replace ( / = / g, '' ) ;
105105}
106106
107+ // Dccode a URLEncoded base64 to an ArrayBuffer string.
108+ function decodeURLEncodedBase64 ( value ) {
109+ return decodeFromBase64 ( value
110+ . replace ( / _ / g, '/' )
111+ . replace ( / - / g, '+' ) ) ;
112+ }
113+
107114function webauthnRegistered ( newCredential ) {
108115 const attestationObject = new Uint8Array ( newCredential . response . attestationObject ) ;
109116 const clientDataJSON = new Uint8Array ( newCredential . response . clientDataJSON ) ;
@@ -115,11 +122,11 @@ function webauthnRegistered(newCredential) {
115122 headers : { 'X-Csrf-Token' : csrfToken } ,
116123 data : JSON . stringify ( {
117124 id : newCredential . id ,
118- rawId : bufferEncode ( rawId ) ,
125+ rawId : bufferURLEncodedBase64 ( rawId ) ,
119126 type : newCredential . type ,
120127 response : {
121- attestationObject : bufferEncode ( attestationObject ) ,
122- clientDataJSON : bufferEncode ( clientDataJSON ) ,
128+ attestationObject : bufferURLEncodedBase64 ( attestationObject ) ,
129+ clientDataJSON : bufferURLEncodedBase64 ( clientDataJSON ) ,
123130 } ,
124131 } ) ,
125132 dataType : 'json' ,
@@ -195,11 +202,11 @@ function webAuthnRegisterRequest() {
195202 } ) . done ( ( makeCredentialOptions ) => {
196203 $ ( '#nickname' ) . closest ( 'div.field' ) . removeClass ( 'error' ) ;
197204
198- makeCredentialOptions . publicKey . challenge = decodeFromBase64 ( makeCredentialOptions . publicKey . challenge ) ;
199- makeCredentialOptions . publicKey . user . id = decodeFromBase64 ( makeCredentialOptions . publicKey . user . id ) ;
205+ makeCredentialOptions . publicKey . challenge = decodeURLEncodedBase64 ( makeCredentialOptions . publicKey . challenge ) ;
206+ makeCredentialOptions . publicKey . user . id = decodeURLEncodedBase64 ( makeCredentialOptions . publicKey . user . id ) ;
200207 if ( makeCredentialOptions . publicKey . excludeCredentials ) {
201208 for ( let i = 0 ; i < makeCredentialOptions . publicKey . excludeCredentials . length ; i ++ ) {
202- makeCredentialOptions . publicKey . excludeCredentials [ i ] . id = decodeFromBase64 ( makeCredentialOptions . publicKey . excludeCredentials [ i ] . id ) ;
209+ makeCredentialOptions . publicKey . excludeCredentials [ i ] . id = decodeURLEncodedBase64 ( makeCredentialOptions . publicKey . excludeCredentials [ i ] . id ) ;
203210 }
204211 }
205212
0 commit comments