1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import type {
AuthenticationExtensionsClientInputs,
PublicKeyCredentialRequestOptionsJSON,
PublicKeyCredentialDescriptor,
UserVerificationRequirement,
} from '@simplewebauthn/typescript-types';
import base64url from 'base64url';
import generateChallenge from '../helpers/generateChallenge';
export type GenerateAuthenticationOptionsOpts = {
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 authentication
* @param timeout How long (in ms) the user can take to complete authentication
* @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 authentication
* @param rpID Valid domain name (after `https://`)
*/
export default function generateAuthenticationOptions(
options: GenerateAuthenticationOptionsOpts = {},
): 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,
};
}
|