summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2020-05-17 21:03:04 -0700
committerMatthew Miller <matthew@millerti.me>2020-05-17 21:03:04 -0700
commit256a20719b493d9e5116beb5344e1eaf74f469fd (patch)
treee5292f66935126a319d13a2aa2d59fda2a506a40
parent834aa3ac2d0e9907487fc73ada8be78a196fad90 (diff)
Add generateAssertionCredentials
-rw-r--r--package-lock.json5
-rw-r--r--package.json5
-rw-r--r--src/assertion/generateAssertionCredentials.ts27
-rw-r--r--src/index.ts4
4 files changed, 39 insertions, 2 deletions
diff --git a/package-lock.json b/package-lock.json
index 675ebc9..d009b2c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1057,6 +1057,11 @@
}
}
},
+ "base64url": {
+ "version": "3.0.1",
+ "resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz",
+ "integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A=="
+ },
"bcrypt-pbkdf": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
diff --git a/package.json b/package.json
index 7adcb55..370ed08 100644
--- a/package.json
+++ b/package.json
@@ -21,5 +21,8 @@
},
"keywords": [
"webauthn"
- ]
+ ],
+ "dependencies": {
+ "base64url": "^3.0.1"
+ }
}
diff --git a/src/assertion/generateAssertionCredentials.ts b/src/assertion/generateAssertionCredentials.ts
new file mode 100644
index 0000000..fc43097
--- /dev/null
+++ b/src/assertion/generateAssertionCredentials.ts
@@ -0,0 +1,27 @@
+import base64url from 'base64url';
+
+/**
+ * Prepare credentials for user registration via navigator.credentials.get(...)
+ *
+ * @param challenge Random string the authenticator needs to sign and pass back
+ * @param credentialIDs Array of base64-encoded authenticator IDs registered by the user for
+ * assertion
+ * @param timeout How long (in ms) the user can take to complete attestation
+ */
+export default function generateAssertionCredentials(
+ challenge: string,
+ credentialIDs: string[],
+ timeout: number = 60000,
+) {
+ return {
+ publicKey: {
+ challenge: Uint8Array.from(challenge, c => c.charCodeAt(0)),
+ allowCredentials: credentialIDs.map(id => ({
+ id: base64url.toBuffer(id),
+ type: 'public-key',
+ transports: ['usb', 'ble', 'nfc'],
+ })),
+ timeout,
+ },
+ };
+}
diff --git a/src/index.ts b/src/index.ts
index bbe3ec4..d20824a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,5 +1,7 @@
-import generateAttestationCredentials from "./attestation/generateAttestationCredentials";
+import generateAttestationCredentials from './attestation/generateAttestationCredentials';
+import generateAssertionCredentials from './assertion/generateAssertionCredentials';
export {
generateAttestationCredentials,
+ generateAssertionCredentials,
};