@@ -14,9 +14,9 @@ export function initUserAuthWebAuthn() {
14
14
15
15
$ . getJSON ( `${ appSubUrl } /user/webauthn/assertion` , { } )
16
16
. done ( ( makeAssertionOptions ) => {
17
- makeAssertionOptions . publicKey . challenge = decode ( makeAssertionOptions . publicKey . challenge ) ;
17
+ makeAssertionOptions . publicKey . challenge = decodeURLEncodedBase64 ( makeAssertionOptions . publicKey . challenge ) ;
18
18
for ( let i = 0 ; i < makeAssertionOptions . publicKey . allowCredentials . length ; i ++ ) {
19
- makeAssertionOptions . publicKey . allowCredentials [ i ] . id = decode ( makeAssertionOptions . publicKey . allowCredentials [ i ] . id ) ;
19
+ makeAssertionOptions . publicKey . allowCredentials [ i ] . id = decodeURLEncodedBase64 ( makeAssertionOptions . publicKey . allowCredentials [ i ] . id ) ;
20
20
}
21
21
navigator . credentials . get ( {
22
22
publicKey : makeAssertionOptions . publicKey
@@ -56,14 +56,14 @@ function verifyAssertion(assertedCredential) {
56
56
type : 'POST' ,
57
57
data : JSON . stringify ( {
58
58
id : assertedCredential . id ,
59
- rawId : bufferEncode ( rawId ) ,
59
+ rawId : encodeURLEncodedBase64 ( rawId ) ,
60
60
type : assertedCredential . type ,
61
61
clientExtensionResults : assertedCredential . getClientExtensionResults ( ) ,
62
62
response : {
63
- authenticatorData : bufferEncode ( authData ) ,
64
- clientDataJSON : bufferEncode ( clientDataJSON ) ,
65
- signature : bufferEncode ( sig ) ,
66
- userHandle : bufferEncode ( userHandle ) ,
63
+ authenticatorData : encodeURLEncodedBase64 ( authData ) ,
64
+ clientDataJSON : encodeURLEncodedBase64 ( clientDataJSON ) ,
65
+ signature : encodeURLEncodedBase64 ( sig ) ,
66
+ userHandle : encodeURLEncodedBase64 ( userHandle ) ,
67
67
} ,
68
68
} ) ,
69
69
contentType : 'application/json; charset=utf-8' ,
@@ -85,14 +85,21 @@ function verifyAssertion(assertedCredential) {
85
85
} ) ;
86
86
}
87
87
88
- // Encode an ArrayBuffer into a base64 string.
89
- function bufferEncode ( value ) {
88
+ // Encode an ArrayBuffer into a URLEncoded base64 string.
89
+ function encodeURLEncodedBase64 ( value ) {
90
90
return encode ( value )
91
91
. replace ( / \+ / g, '-' )
92
92
. replace ( / \/ / g, '_' )
93
93
. replace ( / = / g, '' ) ;
94
94
}
95
95
96
+ // Dccode a URLEncoded base64 to an ArrayBuffer string.
97
+ function decodeURLEncodedBase64 ( value ) {
98
+ return decode ( value
99
+ . replace ( / _ / g, '/' )
100
+ . replace ( / - / g, '+' ) ) ;
101
+ }
102
+
96
103
function webauthnRegistered ( newCredential ) {
97
104
const attestationObject = new Uint8Array ( newCredential . response . attestationObject ) ;
98
105
const clientDataJSON = new Uint8Array ( newCredential . response . clientDataJSON ) ;
@@ -104,11 +111,11 @@ function webauthnRegistered(newCredential) {
104
111
headers : { 'X-Csrf-Token' : csrfToken } ,
105
112
data : JSON . stringify ( {
106
113
id : newCredential . id ,
107
- rawId : bufferEncode ( rawId ) ,
114
+ rawId : encodeURLEncodedBase64 ( rawId ) ,
108
115
type : newCredential . type ,
109
116
response : {
110
- attestationObject : bufferEncode ( attestationObject ) ,
111
- clientDataJSON : bufferEncode ( clientDataJSON ) ,
117
+ attestationObject : encodeURLEncodedBase64 ( attestationObject ) ,
118
+ clientDataJSON : encodeURLEncodedBase64 ( clientDataJSON ) ,
112
119
} ,
113
120
} ) ,
114
121
dataType : 'json' ,
@@ -184,11 +191,11 @@ function webAuthnRegisterRequest() {
184
191
} ) . done ( ( makeCredentialOptions ) => {
185
192
$ ( '#nickname' ) . closest ( 'div.field' ) . removeClass ( 'error' ) ;
186
193
187
- makeCredentialOptions . publicKey . challenge = decode ( makeCredentialOptions . publicKey . challenge ) ;
188
- makeCredentialOptions . publicKey . user . id = decode ( makeCredentialOptions . publicKey . user . id ) ;
194
+ makeCredentialOptions . publicKey . challenge = decodeURLEncodedBase64 ( makeCredentialOptions . publicKey . challenge ) ;
195
+ makeCredentialOptions . publicKey . user . id = decodeURLEncodedBase64 ( makeCredentialOptions . publicKey . user . id ) ;
189
196
if ( makeCredentialOptions . publicKey . excludeCredentials ) {
190
197
for ( let i = 0 ; i < makeCredentialOptions . publicKey . excludeCredentials . length ; i ++ ) {
191
- makeCredentialOptions . publicKey . excludeCredentials [ i ] . id = decode ( makeCredentialOptions . publicKey . excludeCredentials [ i ] . id ) ;
198
+ makeCredentialOptions . publicKey . excludeCredentials [ i ] . id = decodeURLEncodedBase64 ( makeCredentialOptions . publicKey . excludeCredentials [ i ] . id ) ;
192
199
}
193
200
}
194
201
0 commit comments