@@ -202,22 +202,26 @@ function base64urlToUint8array(base64Bytes) {
202
202
return base64js.toByteArray((base64Bytes + padding).replace(/\//g, "_").replace(/\+/g, "-"));
203
203
}
204
204
function uint8arrayToBase64url(bytes) {
205
- return base64js.fromByteArray(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
205
+ if (bytes instanceof Uint8Array) {
206
+ return base64js.fromByteArray(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
207
+ } else {
208
+ return uint8arrayToBase64url(new Uint8Array(bytes));
209
+ }
206
210
}
207
211
208
212
fetch(/* ... */) // Make the call that returns the credentialCreateJson above
209
213
.then(credentialCreateJson => ({ // Decode byte arrays from base64url
210
214
publicKey: {
211
215
...credentialCreateJson.publicKey,
212
216
213
- challenge: base64urlToUint8Array (credentialCreateJson.publicKey.challenge),
217
+ challenge: base64urlToUint8array (credentialCreateJson.publicKey.challenge),
214
218
user: {
215
219
...credentialCreateJson.publicKey.user,
216
- id: base64urlToUint8Array (credentialCreateJson.publicKey.user.id),
220
+ id: base64urlToUint8array (credentialCreateJson.publicKey.user.id),
217
221
},
218
222
excludeCredentials: credentialCreateJson.publicKey.excludeCredentials.map(credential => ({
219
223
...credential,
220
- id: base64urlToUint8Array (credential.id),
224
+ id: base64urlToUint8array (credential.id),
221
225
})),
222
226
223
227
// Warning: Extension inputs could also contain binary data that needs encoding
@@ -230,13 +234,15 @@ fetch(/* ... */) // Make the call that returns the credential
230
234
type: publicKeyCredential.type,
231
235
id: publicKeyCredential.id,
232
236
response: {
233
- attestationObject: uint8arrayToBase64url(response.response.attestationObject),
234
- clientDataJSON: uint8arrayToBase64url(response.response.clientDataJSON),
235
- transports: response.response.getTransports() || [],
237
+ attestationObject: uint8arrayToBase64url(publicKeyCredential.response.attestationObject),
238
+ clientDataJSON: uint8arrayToBase64url(publicKeyCredential.response.clientDataJSON),
239
+
240
+ // Attempt to read transports, but recover gracefully if not supported by the browser
241
+ transports: publicKeyCredential.response.getTransports && publicKeyCredential.response.getTransports() || [],
236
242
},
237
243
238
244
// Warning: Client extension results could also contain binary data that needs encoding
239
- clientExtensionResults: response .getClientExtensionResults(),
245
+ clientExtensionResults: publicKeyCredential .getClientExtensionResults(),
240
246
}))
241
247
.then(encodedResult =>
242
248
fetch(/* ... */)); // Return encoded PublicKeyCredential to server
@@ -314,7 +320,11 @@ function base64urlToUint8array(base64Bytes) {
314
320
return base64js.toByteArray((base64Bytes + padding).replace(/\//g, "_").replace(/\+/g, "-"));
315
321
}
316
322
function uint8arrayToBase64url(bytes) {
317
- return base64js.fromByteArray(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
323
+ if (bytes instanceof Uint8Array) {
324
+ return base64js.fromByteArray(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
325
+ } else {
326
+ return uint8arrayToBase64url(new Uint8Array(bytes));
327
+ }
318
328
}
319
329
320
330
fetch(/* ... */) // Make the call that returns the credentialGetJson above
@@ -324,10 +334,10 @@ fetch(/* ... */) // Make the call that returns the credential
324
334
allowCredentials: credentialGetJson.publicKey.allowCredentials
325
335
&& credentialGetJson.publicKey.allowCredentials.map(credential => ({
326
336
...credential,
327
- id: base64urlToUint8Array (credential.id),
337
+ id: base64urlToUint8array (credential.id),
328
338
})),
329
339
330
- challenge: base64urlToUint8Array (credentialGetJson.publicKey.challenge),
340
+ challenge: base64urlToUint8array (credentialGetJson.publicKey.challenge),
331
341
332
342
// Warning: Extension inputs could also contain binary data that needs encoding
333
343
extensions: credentialGetJson.publicKey.extensions,
@@ -339,14 +349,14 @@ fetch(/* ... */) // Make the call that returns the credential
339
349
type: publicKeyCredential.type,
340
350
id: publicKeyCredential.id,
341
351
response: {
342
- authenticatorData: uint8arrayToBase64url(response .response.authenticatorData),
343
- clientDataJSON: uint8arrayToBase64url(response .response.clientDataJSON),
344
- signature: uint8arrayToBase64url(response .response.signature),
345
- userHandle: response .response.userHandle && uint8arrayToBase64url(response .response.userHandle),
352
+ authenticatorData: uint8arrayToBase64url(publicKeyCredential .response.authenticatorData),
353
+ clientDataJSON: uint8arrayToBase64url(publicKeyCredential .response.clientDataJSON),
354
+ signature: uint8arrayToBase64url(publicKeyCredential .response.signature),
355
+ userHandle: publicKeyCredential .response.userHandle && uint8arrayToBase64url(publicKeyCredential .response.userHandle),
346
356
},
347
357
348
358
// Warning: Client extension results could also contain binary data that needs encoding
349
- clientExtensionResults: response .getClientExtensionResults(),
359
+ clientExtensionResults: publicKeyCredential .getClientExtensionResults(),
350
360
}))
351
361
.then(encodedResult =>
352
362
fetch(/* ... */)); // Return encoded PublicKeyCredential to server
0 commit comments