|
22 | 22 | } |
23 | 23 |
|
24 | 24 | // Define home page JS functionality. |
25 | | - addRouteScript('/', () => { |
| 25 | + addRouteScript('/', async () => { |
| 26 | + let abortController; |
26 | 27 | const form = document.getElementById('auth-form'); |
27 | | - const usernameInput = document.getElementById('input-username'); |
28 | | - const credentialInput = document.getElementById('input-credential'); |
29 | | - const actionInput = document.getElementById('input-action'); |
30 | | - const registerInput = document.getElementById('input-register'); |
31 | | - const authenticateInput = document.getElementById('input-authenticate'); |
32 | 28 | const statusMessage = document.getElementById('status-message'); |
33 | 29 |
|
34 | | - async function submitCredential(action, credentialCallback) { |
35 | | - statusMessage.textContent = 'Submitting...'; |
| 30 | + async function fetchNewCredential(username) { |
| 31 | + if (!username) { |
| 32 | + throw new Error('Please enter a username.'); |
| 33 | + } |
| 34 | + |
| 35 | + const optionsResponse = await fetch('/attestation/options', { |
| 36 | + method: 'POST', |
| 37 | + body: JSON.stringify({ |
| 38 | + username, |
| 39 | + authenticatorSelection: { |
| 40 | + residentKey: 'preferred', |
| 41 | + } |
| 42 | + // TODO: Allow configuration of other options. |
| 43 | + }), |
| 44 | + headers: { |
| 45 | + 'Content-Type': 'application/json', |
| 46 | + }, |
| 47 | + credentials: 'include', |
| 48 | + }); |
| 49 | + const optionsJson = await optionsResponse.json(); |
| 50 | + const options = PublicKeyCredential.parseCreationOptionsFromJSON(optionsJson); |
| 51 | + abortController?.abort(); |
| 52 | + abortController = new AbortController(); |
| 53 | + return await navigator.credentials.create({ |
| 54 | + publicKey: options, |
| 55 | + signal: abortController.signal, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + async function fetchExistingCredential(username, useConditionalMediation) { |
| 60 | + // The username is optional for authentication, so we don't validate it here. |
| 61 | + const optionsResponse = await fetch('/assertion/options', { |
| 62 | + method: 'POST', |
| 63 | + body: JSON.stringify({ |
| 64 | + username, |
| 65 | + // TODO: Allow configuration of other options. |
| 66 | + }), |
| 67 | + headers: { |
| 68 | + 'Content-Type': 'application/json', |
| 69 | + }, |
| 70 | + credentials: 'include', |
| 71 | + }); |
| 72 | + const optionsJson = await optionsResponse.json(); |
| 73 | + const options = PublicKeyCredential.parseRequestOptionsFromJSON(optionsJson); |
| 74 | + abortController?.abort(); |
| 75 | + abortController = new AbortController(); |
| 76 | + return await navigator.credentials.get({ |
| 77 | + publicKey: options, |
| 78 | + mediation: useConditionalMediation ? 'conditional' : undefined, |
| 79 | + signal: abortController.signal, |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + async function fetchAndSubmitCredential(action, useConditionalMediation = false) { |
36 | 84 | try { |
37 | | - var credential = await credentialCallback(); |
| 85 | + const username = new FormData(form).get('username'); |
| 86 | + let credential; |
| 87 | + if (action === 'register') { |
| 88 | + credential = await fetchNewCredential(username); |
| 89 | + } else if (action === 'authenticate') { |
| 90 | + credential = await fetchExistingCredential(username, useConditionalMediation); |
| 91 | + } else { |
| 92 | + throw new Error('Unknown action: ' + action); |
| 93 | + } |
38 | 94 | var credentialJson = JSON.stringify(credential); |
39 | | - credentialInput.value = credentialJson; |
40 | | - actionInput.value = action; |
| 95 | + form.addEventListener('formdata', (e) => { |
| 96 | + e.formData.append('action', action); |
| 97 | + e.formData.append('credential', credentialJson); |
| 98 | + }, { once: true }); |
41 | 99 | form.submit(); |
42 | 100 | } catch (error) { |
43 | | - statusMessage.textContent = 'Error: ' + error.message; |
44 | | - throw error; |
| 101 | + // Ignore abort errors, they are expected when the user cancels the operation. |
| 102 | + if (error.name !== 'AbortError') { |
| 103 | + statusMessage.textContent = 'Error: ' + error.message; |
| 104 | + throw error; |
| 105 | + } |
45 | 106 | } |
46 | 107 | } |
47 | 108 |
|
48 | | - registerInput.addEventListener('click', async (e) => { |
49 | | - e.preventDefault(); |
50 | | - |
51 | | - await submitCredential('register', async () => { |
52 | | - const username = usernameInput.value; |
53 | | - if (!username) { |
54 | | - throw new Error('Please enter a username.'); |
55 | | - } |
56 | | - |
57 | | - const optionsResponse = await fetch('/attestation/options', { |
58 | | - method: 'POST', |
59 | | - body: JSON.stringify({ |
60 | | - username, |
61 | | - authenticatorSelection: { |
62 | | - residentKey: 'preferred', |
63 | | - } |
64 | | - // TODO: Allow configuration of other options. |
65 | | - }), |
66 | | - headers: { |
67 | | - 'Content-Type': 'application/json', |
68 | | - }, |
69 | | - credentials: 'include', |
70 | | - }); |
71 | | - const optionsJson = await optionsResponse.json(); |
72 | | - const options = PublicKeyCredential.parseCreationOptionsFromJSON(optionsJson); |
73 | | - const credential = await navigator.credentials.create({ publicKey: options }); |
74 | | - return credential; |
75 | | - }); |
| 109 | + form.addEventListener('submit', (e) => { |
| 110 | + if (e.submitter?.name == 'action') { |
| 111 | + e.preventDefault(); |
| 112 | + fetchAndSubmitCredential(e.submitter.value); |
| 113 | + } |
76 | 114 | }); |
77 | 115 |
|
78 | | - authenticateInput.addEventListener('click', async (e) => { |
79 | | - e.preventDefault(); |
80 | | - |
81 | | - await submitCredential('authenticate', async () => { |
82 | | - // The username is optional for authentication, so we don't validate it here. |
83 | | - const username = usernameInput.value; |
84 | | - |
85 | | - const optionsResponse = await fetch('/assertion/options', { |
86 | | - method: 'POST', |
87 | | - body: JSON.stringify({ |
88 | | - username, |
89 | | - // TODO: Allow configuration of other options. |
90 | | - }), |
91 | | - headers: { |
92 | | - 'Content-Type': 'application/json', |
93 | | - }, |
94 | | - credentials: 'include', |
95 | | - }); |
96 | | - const optionsJson = await optionsResponse.json(); |
97 | | - const options = PublicKeyCredential.parseRequestOptionsFromJSON(optionsJson); |
98 | | - const credential = await navigator.credentials.get({ publicKey: options }); |
99 | | - return credential; |
100 | | - }); |
101 | | - }); |
| 116 | + if (await PublicKeyCredential.isConditionalMediationAvailable()) { |
| 117 | + await fetchAndSubmitCredential('authenticate', /* useConditionalMediation */ true); |
| 118 | + } |
102 | 119 | }); |
103 | 120 |
|
104 | 121 | enableRouteScripts(); |
|
0 commit comments