|
| 1 | +/** |
| 2 | + * WebAuthn passkey authentication (login). |
| 3 | + */ |
| 4 | +import { getCsrfToken, getCsrfHeaderName, base64urlToBuffer, bufferToBase64url } from '/js/user/webauthn-utils.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Authenticate with passkey (discoverable credential / usernameless). |
| 8 | + */ |
| 9 | +export async function authenticateWithPasskey() { |
| 10 | + const csrfHeader = getCsrfHeaderName(); |
| 11 | + const csrfToken = getCsrfToken(); |
| 12 | + |
| 13 | + // 1. Request authentication options (challenge) from Spring Security |
| 14 | + const optionsResponse = await fetch('/webauthn/authenticate/options', { |
| 15 | + method: 'POST', |
| 16 | + headers: { |
| 17 | + 'Content-Type': 'application/json', |
| 18 | + [csrfHeader]: csrfToken |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + if (!optionsResponse.ok) { |
| 23 | + throw new Error('Failed to start authentication'); |
| 24 | + } |
| 25 | + |
| 26 | + const options = await optionsResponse.json(); |
| 27 | + |
| 28 | + // 2. Convert base64url fields to ArrayBuffer |
| 29 | + // Spring Security 7 returns options directly (not wrapped in publicKey) |
| 30 | + options.challenge = base64urlToBuffer(options.challenge); |
| 31 | + |
| 32 | + if (options.allowCredentials) { |
| 33 | + options.allowCredentials = options.allowCredentials.map(cred => ({ |
| 34 | + ...cred, |
| 35 | + id: base64urlToBuffer(cred.id) |
| 36 | + })); |
| 37 | + } |
| 38 | + |
| 39 | + // 3. Call browser WebAuthn API |
| 40 | + const assertion = await navigator.credentials.get({ |
| 41 | + publicKey: options |
| 42 | + }); |
| 43 | + |
| 44 | + if (!assertion) { |
| 45 | + throw new Error('No assertion returned from authenticator'); |
| 46 | + } |
| 47 | + |
| 48 | + // 4. Convert assertion to JSON in Spring Security's expected format |
| 49 | + const assertionJSON = { |
| 50 | + id: assertion.id, |
| 51 | + rawId: bufferToBase64url(assertion.rawId), |
| 52 | + credType: assertion.type, |
| 53 | + response: { |
| 54 | + authenticatorData: bufferToBase64url(assertion.response.authenticatorData), |
| 55 | + clientDataJSON: bufferToBase64url(assertion.response.clientDataJSON), |
| 56 | + signature: bufferToBase64url(assertion.response.signature), |
| 57 | + userHandle: assertion.response.userHandle |
| 58 | + ? bufferToBase64url(assertion.response.userHandle) |
| 59 | + : null |
| 60 | + }, |
| 61 | + clientExtensionResults: assertion.getClientExtensionResults(), |
| 62 | + authenticatorAttachment: assertion.authenticatorAttachment |
| 63 | + }; |
| 64 | + |
| 65 | + // 5. Send assertion to Spring Security |
| 66 | + const finishResponse = await fetch('/login/webauthn', { |
| 67 | + method: 'POST', |
| 68 | + headers: { |
| 69 | + 'Content-Type': 'application/json', |
| 70 | + [csrfHeader]: csrfToken |
| 71 | + }, |
| 72 | + body: JSON.stringify(assertionJSON) |
| 73 | + }); |
| 74 | + |
| 75 | + if (!finishResponse.ok) { |
| 76 | + let msg = 'Authentication failed'; |
| 77 | + try { |
| 78 | + const data = await finishResponse.json(); |
| 79 | + msg = data.message || msg; |
| 80 | + } catch { |
| 81 | + const text = await finishResponse.text(); |
| 82 | + if (text) msg = text; |
| 83 | + } |
| 84 | + throw new Error(msg); |
| 85 | + } |
| 86 | + |
| 87 | + // Spring Security returns { authenticated: true, redirectUrl: "..." } |
| 88 | + const authResponse = await finishResponse.json(); |
| 89 | + if (!authResponse || !authResponse.authenticated || !authResponse.redirectUrl) { |
| 90 | + throw new Error('Authentication failed'); |
| 91 | + } |
| 92 | + |
| 93 | + return authResponse.redirectUrl; |
| 94 | +} |
0 commit comments