summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/authentication/generateAuthenticationOptions.ts
diff options
context:
space:
mode:
authorJarrett Helton <jaydhelton@gmail.com>2021-08-23 16:28:49 -0400
committerJarrett Helton <jaydhelton@gmail.com>2021-08-23 16:28:49 -0400
commit1f0c326d0cc672de3f403f1c281257c39e3bc349 (patch)
treeb9b553f95b60ec472bac880bf169c2659254245a /packages/server/src/authentication/generateAuthenticationOptions.ts
parent100ea77af46317d815b7bf4f695144187414d5b8 (diff)
start server package movements
Diffstat (limited to 'packages/server/src/authentication/generateAuthenticationOptions.ts')
-rw-r--r--packages/server/src/authentication/generateAuthenticationOptions.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/server/src/authentication/generateAuthenticationOptions.ts b/packages/server/src/authentication/generateAuthenticationOptions.ts
new file mode 100644
index 0000000..35bf13e
--- /dev/null
+++ b/packages/server/src/authentication/generateAuthenticationOptions.ts
@@ -0,0 +1,56 @@
+import type {
+ AuthenticationExtensionsClientInputs,
+ PublicKeyCredentialRequestOptionsJSON,
+ PublicKeyCredentialDescriptor,
+ UserVerificationRequirement,
+} from '@simplewebauthn/typescript-types';
+import base64url from 'base64url';
+
+import generateChallenge from '../helpers/generateChallenge';
+
+export type GenerateAssertionOptionsOpts = {
+ allowCredentials?: PublicKeyCredentialDescriptor[];
+ challenge?: string | Buffer;
+ timeout?: number;
+ userVerification?: UserVerificationRequirement;
+ extensions?: AuthenticationExtensionsClientInputs;
+ rpID?: string;
+};
+
+/**
+ * Prepare a value to pass into navigator.credentials.get(...) for authenticator "login"
+ *
+ * @param allowCredentials Authenticators previously registered by the user, if any. If undefined
+ * the client will ask the user which credential they want to use
+ * @param challenge Random value the authenticator needs to sign and pass back
+ * user for assertion
+ * @param timeout How long (in ms) the user can take to complete assertion
+ * @param userVerification Set to `'discouraged'` when asserting as part of a 2FA flow, otherwise
+ * set to `'preferred'` or `'required'` as desired.
+ * @param extensions Additional plugins the authenticator or browser should use during assertion
+ * @param rpID Valid domain name (after `https://`)
+ */
+export default function generateAssertionOptions(
+ options: GenerateAssertionOptionsOpts = {},
+): PublicKeyCredentialRequestOptionsJSON {
+ const {
+ allowCredentials,
+ challenge = generateChallenge(),
+ timeout = 60000,
+ userVerification,
+ extensions,
+ rpID,
+ } = options;
+
+ return {
+ challenge: base64url.encode(challenge),
+ allowCredentials: allowCredentials?.map(cred => ({
+ ...cred,
+ id: base64url.encode(cred.id as Buffer),
+ })),
+ timeout,
+ userVerification,
+ extensions,
+ rpId: rpID,
+ };
+}