-
Notifications
You must be signed in to change notification settings - Fork 0
/
webAuthn.ts
88 lines (84 loc) · 2.42 KB
/
webAuthn.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export interface IWebAuth {
rpName: string,
rpId: string,
credentialOpt?: PublicKeyCredentialCreationOptions
assertionOpt?: PublicKeyCredentialRequestOptions
}
export interface IAssertionOpt {
challenge: string
}
export interface ICredentialOpt extends IAssertionOpt {
userId: string;
userName: string;
userDisplayName: string;
}
export function webAuthn({
rpName,
rpId,
credentialOpt,
assertionOpt
}: IWebAuth) {
const getCredential = async (
{
userId,
userName,
userDisplayName,
challenge
}: ICredentialOpt
) => {
const publicKeyCredentialCreationOptions = {
rp: {
name: rpName,
id: rpId
},
user: {
id: Uint8Array.from(
userId, c => c.charCodeAt(0)),
name: userName,
displayName: userDisplayName
},
challenge: Uint8Array.from(
challenge, c => c.charCodeAt(0)),
pubKeyCredParams: [
{
type: "public-key",
alg: -7
},
{
type: "public-key",
alg: -257
}
],
timeout: 60000,
excludeCredentials: [],
authenticatorSelection: {
residentKey: "preferred",
requireResidentKey: false,
userVerification: "required"
},
attestation: "none",
extensions: {
credProps: true
},
...credentialOpt
} as PublicKeyCredentialCreationOptions
return await navigator.credentials.create({
publicKey: publicKeyCredentialCreationOptions
});
}
const getAssertion = async ({ challenge }: IAssertionOpt) => {
const publicKeyCredentialRequestOptions = {
challenge: Uint8Array.from(
challenge, c => c.charCodeAt(0)),
allowCredentials: [],
rpId: rpId,
timeout: 60000,
userVerification: "required",
...assertionOpt
} as PublicKeyCredentialRequestOptions
return await navigator.credentials.get({
publicKey: publicKeyCredentialRequestOptions
});
}
return { getCredential, getAssertion } as const
}