summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2020-05-25 23:27:37 -0700
committerMatthew Miller <matthew@millerti.me>2020-05-25 23:27:37 -0700
commitf910f057457108003bcc39378fb5ad94529c6bad (patch)
tree9a6064a5a490aecece233c8f845d80c21618de36 /packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
parent916087c78ff525fbdf3a37e13c40276a3cf2d703 (diff)
Add toPublicKeyCredentialDescriptor browser helper
Diffstat (limited to 'packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts')
-rw-r--r--packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts16
1 files changed, 16 insertions, 0 deletions
diff --git a/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
new file mode 100644
index 0000000..ad166a5
--- /dev/null
+++ b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
@@ -0,0 +1,16 @@
+import base64js from 'base64-js';
+import type { PublicKeyCredentialDescriptorJSON } from '@webauthntine/typescript-types';
+
+export default function toPublicKeyCredentialDescriptor(
+ descriptor: PublicKeyCredentialDescriptorJSON,
+): PublicKeyCredentialDescriptor {
+ // Make sure the Base64'd credential ID length is a multiple of 4 or else toByteArray will throw
+ const { id } = descriptor;
+ const padLength = 4 - (id.length % 4);
+ const paddedId = id.padEnd(id.length + padLength, '=');
+
+ return {
+ ...descriptor,
+ id: base64js.toByteArray(paddedId),
+ };
+}