index.js
6 days ago
user-login-ajax.js
6 days ago
user-login.js
6 days ago
user-profile-ajax.js
6 days ago
user-profile.js
6 days ago
user-profile.js
160 lines
| 1 | import { browserSupportsWebAuthn, startRegistration } from "./index.js"; |
| 2 | |
| 3 | /** |
| 4 | * Create Passkey Registration. |
| 5 | */ |
| 6 | async function createRegistration( isUsb = false ) { |
| 7 | let attResp; |
| 8 | |
| 9 | try { |
| 10 | const response = await wp.apiFetch( { |
| 11 | path: '/wp-2fa-passkeys/v1/register/request', |
| 12 | method: 'POST', |
| 13 | data: { |
| 14 | 'is_usb': isUsb, |
| 15 | }, |
| 16 | } ); |
| 17 | |
| 18 | attResp = await startRegistration(response); |
| 19 | } catch (error) { |
| 20 | throw error; |
| 21 | } |
| 22 | |
| 23 | const passkeyName = await window.wp2faOpenPasskeyPrompt(); |
| 24 | |
| 25 | // POST the response to the endpoint that calls. |
| 26 | try { |
| 27 | const response = await wp.apiFetch( { |
| 28 | path: '/wp-2fa-passkeys/v1/register/response', |
| 29 | method: 'POST', |
| 30 | data: { |
| 31 | attResp, |
| 32 | 'passkey_name': passkeyName, |
| 33 | }, |
| 34 | } ); |
| 35 | |
| 36 | if ( response.status === 'verified' ) { |
| 37 | window.location.reload(); |
| 38 | } |
| 39 | } catch ( error ) { |
| 40 | throw error; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Passkey Registration Handler via custom event from modal. |
| 46 | */ |
| 47 | wp.domReady( () => { |
| 48 | // Hide register button if browser doesn't support WebAuthn. |
| 49 | if ( ! browserSupportsWebAuthn() ) { |
| 50 | const addBtn = document.querySelector( '[data-open-configure-2fa-wizard-passkey]' ); |
| 51 | if ( addBtn ) { |
| 52 | addBtn.style.display = 'none'; |
| 53 | } |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Listen for registration events from the new modal. |
| 58 | document.addEventListener( 'wp2fa-passkey-register', async ( e ) => { |
| 59 | const { isUsb } = e.detail; |
| 60 | try { |
| 61 | await createRegistration( isUsb ); |
| 62 | } catch ( error ) { |
| 63 | if ( error.name === 'InvalidStateError' ) { |
| 64 | window.wp2faShowPasskeySetupError( |
| 65 | wp.i18n.__( 'Error: Authenticator was probably already registered by you', 'wp-2fa' ) |
| 66 | ); |
| 67 | } else { |
| 68 | window.wp2faShowPasskeySetupError( `Error: ${ error.message }` ); |
| 69 | } |
| 70 | } |
| 71 | } ); |
| 72 | } ); |
| 73 | |
| 74 | /** |
| 75 | * Revoke Passkey. |
| 76 | * |
| 77 | * @param {Event} event The event. |
| 78 | */ |
| 79 | async function revokePasskey( event ) { |
| 80 | event.preventDefault(); |
| 81 | |
| 82 | if ( |
| 83 | // eslint-disable-next-line no-alert |
| 84 | ! window.confirm( |
| 85 | wp.i18n.__( 'Are you sure you want to revoke this passkey? This action cannot be undone.', 'wp-2fa' ), |
| 86 | ) |
| 87 | ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | const revokeButton = event.target; |
| 92 | const fingerprint = revokeButton.dataset.id; |
| 93 | |
| 94 | try { |
| 95 | const response = await wp.apiFetch( { |
| 96 | path: '/wp-2fa-passkeys/v1/register/revoke', |
| 97 | method: 'POST', |
| 98 | data: { |
| 99 | fingerprint, |
| 100 | }, |
| 101 | } ); |
| 102 | |
| 103 | if ( response.status === 'success' ) { |
| 104 | window.location.reload(); |
| 105 | } |
| 106 | } catch ( error ) { |
| 107 | throw error; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Enable/Disable Passkey. |
| 113 | * |
| 114 | * @param {Event} event The event. |
| 115 | */ |
| 116 | async function enableDisablePasskey(event) { |
| 117 | event.preventDefault(); |
| 118 | |
| 119 | const enableButton = event.target; |
| 120 | const fingerprint = enableButton.dataset.id; |
| 121 | const user_id = enableButton.dataset.userid; |
| 122 | |
| 123 | try { |
| 124 | const response = await wp.apiFetch( { |
| 125 | path: '/wp-2fa-passkeys/v1/register/enable', |
| 126 | method: 'POST', |
| 127 | data: { |
| 128 | 'info': { 'fingerprint': fingerprint, 'user_id': user_id } |
| 129 | }, |
| 130 | } ); |
| 131 | |
| 132 | if (response.status === 'success') { |
| 133 | window.location.reload(); |
| 134 | } |
| 135 | } catch (error) { |
| 136 | throw error; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Passkey Revoke handler. |
| 142 | */ |
| 143 | wp.domReady( () => { |
| 144 | const revokeButtons = document.querySelectorAll( '.wp-2fa-passkey-list-table button.delete' ); |
| 145 | |
| 146 | if (revokeButtons) { |
| 147 | revokeButtons.forEach(revokeButton => { |
| 148 | revokeButton.addEventListener('click', revokePasskey); |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | const enableButtons = document.querySelectorAll('.wp-2fa-passkey-list-table button.disable'); |
| 153 | |
| 154 | if (enableButtons) { |
| 155 | enableButtons.forEach(enableButton => { |
| 156 | enableButton.addEventListener('click', enableDisablePasskey); |
| 157 | }); |
| 158 | } |
| 159 | }); |
| 160 |