-
Notifications
You must be signed in to change notification settings - Fork 2
/
webauthn.test.tsx
89 lines (78 loc) · 2.52 KB
/
webauthn.test.tsx
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
89
// An HTML page that experiments with the WebAuthn API.
import { f, h, render, signal } from "./easy-jsx.js"
const [emailEl, setEmailEl] = signal<HTMLInputElement>(null!)
const body = (
<>
<input type="text" autocomplete="email webauthn" use={setEmailEl} />
<button
on:click={async () => {
if (typeof PublicKeyCredential !== "undefined") {
try {
const challenge = new Uint8Array(32)
window.crypto.getRandomValues(challenge)
const response = await navigator.credentials.create({
publicKey: {
challenge,
pubKeyCredParams: [
{ alg: -7, type: "public-key" },
{ alg: -257, type: "public-key" },
],
rp: {
name: "webauthn test",
id: "localhost",
},
user: {
displayName: "Zachary Sakowitz",
id: Uint8Array.from("[email protected]", (x) =>
x.charCodeAt(0),
),
name: "[email protected]",
},
authenticatorSelection: {
authenticatorAttachment: "cross-platform",
},
},
})
console.log(response)
} catch (err) {
console.error("Error with conditional UI:", err)
}
}
}}
>
create new credentials
</button>
<button
on:click={async () => {
if (
typeof PublicKeyCredential !== "undefined" &&
"isConditionalMediationAvailable" in PublicKeyCredential &&
typeof PublicKeyCredential.isConditionalMediationAvailable ==
"function"
) {
const available: boolean =
await PublicKeyCredential.isConditionalMediationAvailable()
if (available) {
try {
const challenge = new Uint8Array(32)
window.crypto.getRandomValues(challenge)
const autoFillResponse = await navigator.credentials.get({
publicKey: {
challenge,
rpId: "localhost",
userVerification: "required",
},
})
console.log(autoFillResponse)
} catch (err) {
console.error("Error with conditional UI:", err)
}
}
}
}}
>
get existing credentials
</button>
</>
)
render(document.body, body)