|
| 1 | +// Passkeys frontend implementation |
| 2 | +document.addEventListener('DOMContentLoaded', function() { |
| 3 | + |
| 4 | + // Registration Flow |
| 5 | + const registerBtn = document.getElementById('two-factor-passkey-register-btn'); |
| 6 | + if (registerBtn) { |
| 7 | + registerBtn.addEventListener('click', async function() { |
| 8 | + try { |
| 9 | + registerBtn.disabled = true; |
| 10 | + registerBtn.textContent = 'Registering...'; |
| 11 | + |
| 12 | + // 1. Get creation options from server |
| 13 | + const optionsRes = await fetch(twoFactorPasskeyData.restUrl + 'passkeys/options?action=register', { |
| 14 | + method: 'GET', |
| 15 | + headers: { 'X-WP-Nonce': twoFactorPasskeyData.nonce } |
| 16 | + }); |
| 17 | + const options = await optionsRes.json(); |
| 18 | + if (!optionsRes.ok) throw new Error(options.message || 'Failed to get options'); |
| 19 | + |
| 20 | + // 2. Decode options (Base64Url to Uint8Array) |
| 21 | + const createArgs = recursiveBase64StrToArrayBuffer(options); |
| 22 | + |
| 23 | + // 3. Prompt user to create passkey |
| 24 | + const credential = await navigator.credentials.create(createArgs); |
| 25 | + |
| 26 | + // 4. Encode response |
| 27 | + const data = { |
| 28 | + id: credential.id, |
| 29 | + rawId: arrayBufferToBase64(credential.rawId), |
| 30 | + type: credential.type, |
| 31 | + response: { |
| 32 | + attestationObject: arrayBufferToBase64(credential.response.attestationObject), |
| 33 | + clientDataJSON: arrayBufferToBase64(credential.response.clientDataJSON) |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + // 5. Send to server to register |
| 38 | + const regRes = await fetch(twoFactorPasskeyData.restUrl + 'passkeys/register', { |
| 39 | + method: 'POST', |
| 40 | + headers: { |
| 41 | + 'Content-Type': 'application/json', |
| 42 | + 'X-WP-Nonce': twoFactorPasskeyData.nonce |
| 43 | + }, |
| 44 | + body: JSON.stringify(data) |
| 45 | + }); |
| 46 | + |
| 47 | + const regData = await regRes.json(); |
| 48 | + if (!regRes.ok) throw new Error(regData.message || 'Registration failed'); |
| 49 | + |
| 50 | + alert('Passkey registered successfully!'); |
| 51 | + window.location.reload(); |
| 52 | + |
| 53 | + } catch (err) { |
| 54 | + console.error(err); |
| 55 | + alert('Passkey registration failed: ' + err.message); |
| 56 | + } finally { |
| 57 | + registerBtn.disabled = false; |
| 58 | + registerBtn.textContent = 'Register New Passkey'; |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + // Login Flow (Option 1 - Passwordless) |
| 64 | + // For passwordless, we need to intercept the standard login form. |
| 65 | + const loginForm = document.getElementById('loginform'); |
| 66 | + if (loginForm && !document.getElementById('twofactorform')) { |
| 67 | + const userLoginInput = document.getElementById('user_login'); |
| 68 | + |
| 69 | + // Add a "Login with Passkey" button |
| 70 | + const passkeyLoginBtn = document.createElement('button'); |
| 71 | + passkeyLoginBtn.type = 'button'; |
| 72 | + passkeyLoginBtn.className = 'button button-secondary button-large'; |
| 73 | + passkeyLoginBtn.style.marginTop = '10px'; |
| 74 | + passkeyLoginBtn.style.width = '100%'; |
| 75 | + passkeyLoginBtn.textContent = 'Login with Passkey'; |
| 76 | + |
| 77 | + // Insert after the submit button |
| 78 | + const submitP = loginForm.querySelector('.submit'); |
| 79 | + if (submitP) { |
| 80 | + submitP.appendChild(passkeyLoginBtn); |
| 81 | + } |
| 82 | + |
| 83 | + passkeyLoginBtn.addEventListener('click', async function(e) { |
| 84 | + e.preventDefault(); |
| 85 | + const username = userLoginInput.value.trim(); |
| 86 | + if (!username) { |
| 87 | + alert('Please enter your username or email address first.'); |
| 88 | + userLoginInput.focus(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + passkeyLoginBtn.disabled = true; |
| 94 | + passkeyLoginBtn.textContent = 'Authenticating...'; |
| 95 | + |
| 96 | + const optionsRes = await fetch(twoFactorPasskeyData.restUrl + 'passkeys/options?action=authenticate&username=' + encodeURIComponent(username)); |
| 97 | + const options = await optionsRes.json(); |
| 98 | + if (!optionsRes.ok) throw new Error(options.message || 'Failed to get options'); |
| 99 | + |
| 100 | + const sessionId = options.session_id; |
| 101 | + const getArgs = recursiveBase64StrToArrayBuffer(options.args); |
| 102 | + const credential = await navigator.credentials.get(getArgs); |
| 103 | + |
| 104 | + const assertion = { |
| 105 | + id: credential.id, |
| 106 | + rawId: arrayBufferToBase64(credential.rawId), |
| 107 | + type: credential.type, |
| 108 | + session_id: sessionId, |
| 109 | + response: { |
| 110 | + authenticatorData: arrayBufferToBase64(credential.response.authenticatorData), |
| 111 | + clientDataJSON: arrayBufferToBase64(credential.response.clientDataJSON), |
| 112 | + signature: arrayBufferToBase64(credential.response.signature), |
| 113 | + userHandle: credential.response.userHandle ? arrayBufferToBase64(credential.response.userHandle) : null |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + let assertionInput = document.getElementById('two_factor_passkey_assertion'); |
| 118 | + if (!assertionInput) { |
| 119 | + assertionInput = document.createElement('input'); |
| 120 | + assertionInput.type = 'hidden'; |
| 121 | + assertionInput.id = 'two_factor_passkey_assertion'; |
| 122 | + assertionInput.name = 'two_factor_passkey_assertion'; |
| 123 | + loginForm.appendChild(assertionInput); |
| 124 | + } |
| 125 | + assertionInput.value = JSON.stringify(assertion); |
| 126 | + |
| 127 | + HTMLFormElement.prototype.submit.call(loginForm); |
| 128 | + |
| 129 | + } catch (err) { |
| 130 | + console.error(err); |
| 131 | + alert('Passkey login failed: ' + err.message); |
| 132 | + passkeyLoginBtn.disabled = false; |
| 133 | + passkeyLoginBtn.textContent = 'Login with Passkey'; |
| 134 | + } |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + // Login Flow (Option 2 - 2FA Interstitial) |
| 139 | + const authBtn2FA = document.getElementById('two-factor-passkey-auth-btn'); |
| 140 | + if (authBtn2FA) { |
| 141 | + const twoFactorForm = authBtn2FA.closest('form'); |
| 142 | + authBtn2FA.addEventListener('click', async function(e) { |
| 143 | + e.preventDefault(); |
| 144 | + const username = authBtn2FA.getAttribute('data-username'); |
| 145 | + if (!username) return; |
| 146 | + |
| 147 | + try { |
| 148 | + authBtn2FA.disabled = true; |
| 149 | + authBtn2FA.textContent = 'Authenticating...'; |
| 150 | + |
| 151 | + const optionsRes = await fetch(twoFactorPasskeyData.restUrl + 'passkeys/options?action=authenticate&username=' + encodeURIComponent(username)); |
| 152 | + const options = await optionsRes.json(); |
| 153 | + if (!optionsRes.ok) throw new Error(options.message || 'Failed to get options'); |
| 154 | + |
| 155 | + const sessionId = options.session_id; |
| 156 | + const getArgs = recursiveBase64StrToArrayBuffer(options.args); |
| 157 | + const credential = await navigator.credentials.get(getArgs); |
| 158 | + |
| 159 | + const assertion = { |
| 160 | + id: credential.id, |
| 161 | + rawId: arrayBufferToBase64(credential.rawId), |
| 162 | + type: credential.type, |
| 163 | + session_id: sessionId, |
| 164 | + response: { |
| 165 | + authenticatorData: arrayBufferToBase64(credential.response.authenticatorData), |
| 166 | + clientDataJSON: arrayBufferToBase64(credential.response.clientDataJSON), |
| 167 | + signature: arrayBufferToBase64(credential.response.signature), |
| 168 | + userHandle: credential.response.userHandle ? arrayBufferToBase64(credential.response.userHandle) : null |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + let assertionInput = document.getElementById('two_factor_passkey_assertion'); |
| 173 | + if (!assertionInput) { |
| 174 | + assertionInput = document.createElement('input'); |
| 175 | + assertionInput.type = 'hidden'; |
| 176 | + assertionInput.id = 'two_factor_passkey_assertion'; |
| 177 | + assertionInput.name = 'two_factor_passkey_assertion'; |
| 178 | + twoFactorForm.appendChild(assertionInput); |
| 179 | + } |
| 180 | + assertionInput.value = JSON.stringify(assertion); |
| 181 | + |
| 182 | + HTMLFormElement.prototype.submit.call(twoFactorForm); |
| 183 | + |
| 184 | + } catch (err) { |
| 185 | + console.error(err); |
| 186 | + alert('Passkey authentication failed: ' + err.message); |
| 187 | + authBtn2FA.disabled = false; |
| 188 | + authBtn2FA.textContent = 'Use Passkey'; |
| 189 | + } |
| 190 | + }); |
| 191 | + } |
| 192 | + |
| 193 | + // Helpers for Base64Url |
| 194 | + function arrayBufferToBase64(buffer) { |
| 195 | + let binary = ''; |
| 196 | + const bytes = new Uint8Array(buffer); |
| 197 | + const len = bytes.byteLength; |
| 198 | + for (let i = 0; i < len; i++) { |
| 199 | + binary += String.fromCharCode(bytes[i]); |
| 200 | + } |
| 201 | + return window.btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); |
| 202 | + } |
| 203 | + |
| 204 | + function base64ToArrayBuffer(base64) { |
| 205 | + base64 = base64.replace(/-/g, "+").replace(/_/g, "/"); |
| 206 | + const padLen = (4 - (base64.length % 4)) % 4; |
| 207 | + base64 += "=".repeat(padLen); |
| 208 | + const binary_string = window.atob(base64); |
| 209 | + const len = binary_string.length; |
| 210 | + const bytes = new Uint8Array(len); |
| 211 | + for (let i = 0; i < len; i++) { |
| 212 | + bytes[i] = binary_string.charCodeAt(i); |
| 213 | + } |
| 214 | + return bytes.buffer; |
| 215 | + } |
| 216 | + |
| 217 | + function recursiveBase64StrToArrayBuffer(obj) { |
| 218 | + let prefix = '=?BINARY?B?'; |
| 219 | + let suffix = '?='; |
| 220 | + if (typeof obj === 'object' && obj !== null) { |
| 221 | + for (let key in obj) { |
| 222 | + if (typeof obj[key] === 'string') { |
| 223 | + let str = obj[key]; |
| 224 | + if (str.substring(0, prefix.length) === prefix && str.substring(str.length - suffix.length) === suffix) { |
| 225 | + str = str.substring(prefix.length, str.length - suffix.length); |
| 226 | + obj[key] = base64ToArrayBuffer(str); |
| 227 | + } |
| 228 | + } else { |
| 229 | + recursiveBase64StrToArrayBuffer(obj[key]); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + return obj; |
| 234 | + } |
| 235 | +}); |
0 commit comments