PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 4.0.0
WP 2FA – Two-factor authentication for WordPress v4.0.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Admin / Methods / passkeys / assets / js / user-profile.js
wp-2fa / includes / classes / Admin / Methods / passkeys / assets / js Last commit date
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