summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2020-06-02 15:50:11 -0700
committerGitHub <noreply@github.com>2020-06-02 15:50:11 -0700
commited960d81a9667d5cca2d444839f5ce63e2f38911 (patch)
tree2d9f2f8e7ce60a83e5409d073f74422bcc2df60e /packages/browser/src/helpers
parent743de54fa9b0cbef261cdbedf1c567c2202737cd (diff)
parentbb5e3e99f7e50b9cec607b4fda34dcbd1e04aae9 (diff)
Merge pull request #21 from MasterKale/feature/improve-browser
Refactor Megamix 1
Diffstat (limited to 'packages/browser/src/helpers')
-rw-r--r--packages/browser/src/helpers/base64URLStringToBuffer.ts33
-rw-r--r--packages/browser/src/helpers/bufferToBase64URLString.ts21
-rw-r--r--packages/browser/src/helpers/toBase64String.test.ts15
-rw-r--r--packages/browser/src/helpers/toBase64String.ts6
-rw-r--r--packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts8
5 files changed, 57 insertions, 26 deletions
diff --git a/packages/browser/src/helpers/base64URLStringToBuffer.ts b/packages/browser/src/helpers/base64URLStringToBuffer.ts
new file mode 100644
index 0000000..baa258f
--- /dev/null
+++ b/packages/browser/src/helpers/base64URLStringToBuffer.ts
@@ -0,0 +1,33 @@
+/**
+ * Convert from a Base64URL-encoded string to an Array Buffer. Best used when converting a
+ * credential ID from a JSON string to an ArrayBuffer, like in allowCredentials or
+ * excludeCredentials
+ *
+ * Helper method to compliment `bufferToBase64URLString`
+ */
+export default function base64URLStringToBuffer(base64URLString: string): ArrayBuffer {
+ // Convert from Base64URL to Base64
+ const base64 = base64URLString.replace(/-/g, '+').replace(/_/g, '/');
+ /**
+ * Pad with '=' until it's a multiple of four
+ * (4 - (85 % 4 = 1) = 3) % 4 = 3 padding
+ * (4 - (86 % 4 = 2) = 2) % 4 = 2 padding
+ * (4 - (87 % 4 = 3) = 1) % 4 = 1 padding
+ * (4 - (88 % 4 = 0) = 4) % 4 = 0 padding
+ */
+ const padLength = (4 - (base64.length % 4)) % 4;
+ const padded = base64.padEnd(base64.length + padLength, '=');
+
+ // Convert to a binary string
+ const binary = atob(padded);
+
+ // Convert binary string to buffer
+ const buffer = new ArrayBuffer(binary.length);
+ const bytes = new Uint8Array(buffer);
+
+ for (let i = 0; i < binary.length; i++) {
+ bytes[i] = binary.charCodeAt(i);
+ }
+
+ return buffer;
+}
diff --git a/packages/browser/src/helpers/bufferToBase64URLString.ts b/packages/browser/src/helpers/bufferToBase64URLString.ts
new file mode 100644
index 0000000..954f4b0
--- /dev/null
+++ b/packages/browser/src/helpers/bufferToBase64URLString.ts
@@ -0,0 +1,21 @@
+/**
+ * Convert the given array buffer into a Base64URL-encoded string. Ideal for converting various
+ * credential response ArrayBuffers to string for sending back to the server as JSON.
+ *
+ * Helper method to compliment `base64URLStringToBuffer`
+ */
+export default function bufferToBase64URLString(buffer: ArrayBuffer): string {
+ const bytes = new Uint8Array(buffer);
+ let str = '';
+
+ for (const charCode of bytes) {
+ str += String.fromCharCode(charCode);
+ }
+
+ const base64String = btoa(str);
+
+ return base64String
+ .replace(/\+/g, '-')
+ .replace(/\//g, '_')
+ .replace(/=/g, '');
+}
diff --git a/packages/browser/src/helpers/toBase64String.test.ts b/packages/browser/src/helpers/toBase64String.test.ts
deleted file mode 100644
index bbcb11b..0000000
--- a/packages/browser/src/helpers/toBase64String.test.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import toBase64String from './toBase64String';
-
-import toUint8Array from './toUint8Array';
-
-test('should convert a Buffer to a string with a length that is a multiple of 4', () => {
- const base64 = toBase64String(Buffer.from('123456', 'ascii'));
-
- expect(base64.length % 4).toEqual(0);
-});
-
-test('should convert a Uint8Array to a string with a length that is a multiple of 4', () => {
- const base64 = toBase64String(toUint8Array('123456'));
-
- expect(base64.length % 4).toEqual(0);
-});
diff --git a/packages/browser/src/helpers/toBase64String.ts b/packages/browser/src/helpers/toBase64String.ts
deleted file mode 100644
index 3178695..0000000
--- a/packages/browser/src/helpers/toBase64String.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import base64js from 'base64-js';
-
-export default function toBase64String(buffer: ArrayBuffer): string {
- // TODO: Make sure converting buffer to Uint8Array() is correct
- return base64js.fromByteArray(new Uint8Array(buffer)).replace(/\+/g, '-').replace(/\//g, '_');
-}
diff --git a/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
index c37fed0..8fad78b 100644
--- a/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
+++ b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
@@ -1,16 +1,14 @@
-import base64js from 'base64-js';
import type { PublicKeyCredentialDescriptorJSON } from '@simplewebauthn/typescript-types';
+import base64URLStringToBuffer from './base64URLStringToBuffer';
+
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),
+ id: base64URLStringToBuffer(id),
};
}