-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathpopup.js
More file actions
75 lines (64 loc) · 2.49 KB
/
popup.js
File metadata and controls
75 lines (64 loc) · 2.49 KB
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
document.addEventListener('DOMContentLoaded', () => {
const registerButton = document.getElementById('registerButton');
const authButton = document.getElementById('authButton');
const optionsText = document.getElementById('optionsText');
function arrayBufferToBase64(buffer) {
return btoa(String.fromCharCode(...new Uint8Array(buffer)));
}
// Handle WebAuthn registration
registerButton.addEventListener('click', async () => {
let options;
try {
options = JSON.parse(optionsText.value);
} catch (error) {
alert('Invalid JSON in text field');
return;
}
// Convert challenge (and user id if necessary)
options.challenge = Uint8Array.fromBase64(options.challenge);
options.user.id = Uint8Array.fromBase64(options.user.id);
try {
const credential = await navigator.credentials.create({ publicKey: options });
console.log('Credential created:', credential);
alert('Credential created successfully');
} catch (err) {
console.error('Error during credential creation:', err);
alert('Error during credential creation: ' + err);
}
});
// Handle WebAuthn authentication
authButton.addEventListener('click', async () => {
let options;
try {
options = JSON.parse(optionsText.value);
} catch (error) {
alert('Invalid JSON in text field');
return;
}
// Convert challenge if necessary for authentication options as well
options.challenge = Uint8Array.fromBase64(options.challenge);
if (Array.isArray(options?.allowCredentials)) {
for (const ac of options.allowCredentials) {
ac.id = Uint8Array.fromBase64(ac.id);
}
}
try {
const assertion = await navigator.credentials.get({ publicKey: options });
console.log('Assertion obtained:', assertion);
const credentialId = assertion.id;
const authenticatorData = arrayBufferToBase64(assertion.response.authenticatorData);
const clientDataJSON = new TextDecoder().decode(assertion.response.clientDataJSON);
const signature = arrayBufferToBase64(assertion.response.signature);
alert(
`Assertion obtained successfully!\n\n` +
`Credential ID: ${credentialId}\n\n` +
`Authenticator Data: ${authenticatorData}\n\n` +
`Client Data JSON: ${clientDataJSON}\n\n` +
`Signature: ${signature}`
);
} catch (err) {
console.error('Error during credential assertion:', err);
alert('Error during credential assertion: ' + err);
}
});
});