PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 1.3.0
WP 2FA – Two-factor authentication for WordPress v1.3.0
4.1.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 / UserProfile.php
wp-2fa / includes / classes / Admin Last commit date
SettingsPage.php 6 years ago SetupWizard.php 6 years ago UserNotices.php 6 years ago UserProfile.php 6 years ago UserRegistered.php 6 years ago
UserProfile.php
1025 lines
1 <?php // phpcs:ignore
2
3 namespace WP2FA\Admin;
4
5 use \WP2FA\Authenticator\Authentication as Authentication;
6 use \WP2FA\WP2FA as WP2FA;
7 use \WP2FA\Core as Core;
8 use \WP2FA\Authenticator\BackupCodes as BackupCodes;
9
10 /**
11 * UserProfile - Class for handling user things such as profile settings and admin list views.
12 */
13 class UserProfile {
14
15 const NOTICES_META_KEY = 'wp_2fa_totp_notices';
16
17 /**
18 * Classs constructor
19 */
20 public function __construct() {
21 }
22
23 /**
24 * Add our buttons to the user profile editing screen.
25 *
26 * @param object $user User data.
27 */
28 public function user_2fa_options( $user ) {
29
30 if ( isset( $_GET['user_id'] ) ) {
31 $user_id = (int) $_GET['user_id'];
32 $user = get_user_by( 'id', $user_id );
33 } else {
34 $user = wp_get_current_user();
35 }
36
37 // Get current user, we going to need this regardless.
38 $current_user = wp_get_current_user();
39
40 // Bail if we still dont have an object.
41 if ( ! is_a( $user, '\WP_User' ) || ! is_a( $current_user, '\WP_User' ) ) {
42 return;
43 }
44
45 $roles = (array) $user->roles;
46
47 // Grab grace period UNIX time.
48 $grace_period_expired = get_user_meta( $user->ID, 'wp_2fa_user_grace_period_expired', true );
49 // Grab current time, we going to compare these later.
50 $time_now = time();
51 $is_user_excluded = WP2FA::is_user_excluded( $user->ID );
52
53 // First lets see if the user already has a token.
54 $enabled_methods = get_user_meta( $user->ID, 'wp_2fa_enabled_methods', true );
55
56 if ( ! $grace_period_expired && $current_user->ID === $user->ID && ! $is_user_excluded && ! empty( $roles ) ) {
57
58 // These are the buttons we show if a user has no enabled methods.
59 if ( ! empty( $enabled_methods && $user->ID === $current_user->ID ) ) {
60 // Create wizard link based on which 2fa methods are allowed by admin.
61 if ( ! empty( WP2FA::get_wp2fa_setting( 'enable_totp' ) ) && ! empty( WP2FA::get_wp2fa_setting( 'enable_email' ) ) ) {
62 if ( WP2FA::is_this_multisite() ) {
63 $button_link = admin_url( 'index.php?page=wp-2fa-setup&current-step=user_choose_2fa_method&wizard_type=user_2fa_config' );
64 } else {
65 $button_link = admin_url( 'options-general.php?page=wp-2fa-setup&current-step=user_choose_2fa_method&wizard_type=user_2fa_config' );
66 }
67
68 } else {
69 if ( WP2FA::is_this_multisite() ) {
70 $button_link = admin_url( 'index.php?page=wp-2fa-setup&current-step=reconfigure_method&wizard_type=user_reconfigure_config' );
71 } else {
72 $button_link = admin_url( 'options-general.php?page=wp-2fa-setup&current-step=reconfigure_method&wizard_type=user_reconfigure_config' );
73 }
74 }
75 // Create remove 2fa link.
76 $url = add_query_arg(
77 array(
78 'action' => 'remove_user_2fa',
79 'user_id' => $user->ID,
80 'wp_2fa_nonce' => wp_create_nonce( 'wp-2fa-remove-user-2fa-nonce' ),
81 ),
82 admin_url( 'user-edit.php' )
83 );
84 if ( WP2FA::is_this_multisite() ) {
85 $backup_codes_link = admin_url( 'index.php?page=wp-2fa-setup&current-step=backup_codes&wizard_type=backup_codes_config' );
86 } else {
87 $backup_codes_link = admin_url( 'options-general.php?page=wp-2fa-setup&current-step=backup_codes&wizard_type=backup_codes_config' );
88 }
89 ?>
90 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
91 <p class="description"><?php esc_html_e( 'Add two-factor authentication to strengthen the security of your WordPress user account.', 'wp-2fa' ); ?></p>
92 <table class="form-table" role="presentation">
93 <tbody>
94 <tr>
95 <th><label><?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?></label></th>
96 <td>
97 <a href="<?php echo esc_url( $button_link ); ?>" class="button button-primary"><?php esc_html_e( 'Change 2FA Settings', 'wp-2fa' ); ?></a> <a href="#" class="button button-primary remove-2fa" onclick="MicroModal.show('confirm-remove-2fa');"><?php esc_html_e( 'Remove 2FA', 'wp-2fa' ); ?></a>
98 <br />
99 <br />
100 <a href="<?php echo esc_url( $backup_codes_link ); ?>" class="button button-primary"><?php esc_html_e( 'Generate backup codes', 'wp-2fa' ); ?></a>
101 <?php
102 $codes_remaining = BackupCodes::codes_remaining_for_user( $user );
103 if ( $codes_remaining > 0 ) {
104 ?>
105 <span class="description mt-5px"><?php echo esc_attr( (int) $codes_remaining ); ?> <?php esc_html_e( 'unused backup codes remaining.', 'wp-2fa' ); ?></span>
106 <?php
107 } elseif ( 0 === $codes_remaining ) {
108 ?>
109 <a class="learn_more_link" href="https://www.wpwhitesecurity.com/2fa-backup-codes/?utm_source=plugin&utm_medium=referral&utm_campaign=wp2fa&utm_content=settings+pages" target="_blank"><?php esc_html_e( 'Learn more.', 'wp-2fa' ); ?></a>
110 <?php
111 }
112 ?>
113 </td>
114 </tr>
115 </tbody>
116 </table>
117 <div class="wp2fa-modal micromodal-slide" id="confirm-remove-2fa" aria-hidden="true">
118 <div class="modal__overlay" tabindex="-1" data-micromodal-close>
119 <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
120 <header class="modal__header">
121 <h4 class="modal__title" id="modal-1-title">
122 <?php esc_html_e( 'Remove 2FA?', 'wp-2fa' ); ?>
123 </h4>
124 <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
125 </header>
126 <main class="modal__content" id="modal-1-content">
127 <p>
128 <?php esc_html_e( 'Are you sure you want to remove two-factor authentication and lower the security of your user account?', 'wp-2fa' ); ?>
129 </p>
130 </main>
131 <footer class="modal__footer">
132 <a href="<?php echo esc_url( $url ); ?>" class="modal__btn modal__btn-primary"><?php esc_html_e( 'Yes', 'wp-2fa' ); ?></a>
133 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'No', 'wp-2fa' ); ?></button>
134 </footer>
135 </div>
136 </div>
137 </div>
138
139 <?php
140 } else {
141
142 if ( WP2FA::is_this_multisite() ) {
143 $first_time_setup_link = admin_url( 'index.php?page=wp-2fa-setup&current-step=choose_2fa_method&first_time_setup=true' );
144 } else {
145 $first_time_setup_link = admin_url( 'options-general.php?page=wp-2fa-setup&current-step=choose_2fa_method&first_time_setup=true' );
146 }
147 ?>
148 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
149 <p class="description"><?php esc_html_e( 'Add two-factor authentication to strengthen the security of your WordPress user account.', 'wp-2fa' ); ?></p>
150 <table class="form-table" role="presentation">
151 <tbody>
152 <tr>
153 <th><label><?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?></label></th>
154 <td>
155 <a href="<?php echo esc_url( $first_time_setup_link ); ?>" class="button button-primary"><?php esc_html_e( 'Configure Two-factor authentication (2FA)', 'wp-2fa' ); ?></a>
156 </td>
157 </tr>
158 </tbody>
159 </table>
160 <?php
161 }
162 } elseif ( $is_user_excluded ) {
163 ?>
164 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
165 <p class="description"><?php esc_html_e( 'Add two-factor authentication to strengthen the security of your WordPress user account.', 'wp-2fa' ); ?></p>
166 <p class="description"><?php esc_html_e( 'Your user / role is not permitted to configure 2FA. Contact your administrator for more information.', 'wp-2fa' ); ?></p>
167 <?php
168 } elseif ( $grace_period_expired && current_user_can( 'manage_options' ) ) {
169 ?>
170 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
171 <table class="form-table" role="presentation">
172 <tbody>
173 <tr>
174 <th><label><?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?></label></th>
175 <td>
176 <?php
177 $url = add_query_arg(
178 array(
179 'action' => 'unlock_account',
180 'user_id' => $user->ID,
181 'wp_2fa_nonce' => wp_create_nonce( 'wp-2fa-unlock-account-nonce' ),
182 ),
183 admin_url( 'user-edit.php' )
184 );
185
186 if ( $grace_period_expired ) {
187 ?>
188 <a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Unlock user and reset the grace period', 'wp-2fa' ); ?></a>
189 <?php
190 }
191 ?>
192 </td>
193 </tr>
194 </tbody>
195 </table>
196 <?php
197 } elseif ( ! $grace_period_expired && current_user_can( 'manage_options' ) && ! empty( $enabled_methods ) ) {
198 ?>
199 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
200 <p class="description"><?php esc_html_e( 'By resetting this user\'s 2FA configuration you disable the currently configured 2FA so the user can log back in using a usemame and a password only. *the user if enforced to setup 2FA the user will get a prompt upon logging in, from where 2FA can be configured. The user has to configure 2FA within the configured grace period.', 'wp-2fa' ); ?></p>
201 <table class="form-table" role="presentation">
202 <tbody>
203 <tr>
204 <th><label><?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?></label></th>
205 <td>
206 <?php
207 // Create remove 2fa link.
208 $url = add_query_arg(
209 array(
210 'action' => 'remove_user_2fa',
211 'user_id' => $user->ID,
212 'wp_2fa_nonce' => wp_create_nonce( 'wp-2fa-remove-user-2fa-nonce' ),
213 'admin_reset' => 'yes',
214 ),
215 admin_url( 'user-edit.php' )
216 );
217 ?>
218 <a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Reset 2FA configuration', 'wp-2fa' ); ?></a>
219 </td>
220 </tr>
221 </tbody>
222 </table>
223 <?php
224 } elseif ( empty( $roles ) ) {
225 echo $this->inline_2fa_profile_form();
226 }
227
228 }
229
230 /**
231 * Produces the 2FA configuration form for network users, or any user with no roles.
232 */
233 public function inline_2fa_profile_form( $is_shortcode = '', $show_preamble = 'true' ) {
234 if ( isset( $_GET['user_id'] ) ) {
235 $user_id = (int) $_GET['user_id'];
236 $user = get_user_by( 'id', $user_id );
237 } else {
238 $user = wp_get_current_user();
239 }
240
241 // Get current user, we going to need this regardless.
242 $current_user = wp_get_current_user();
243
244 // Bail if we still dont have an object.
245 if ( ! is_a( $user, '\WP_User' ) || ! is_a( $current_user, '\WP_User' ) ) {
246 return;
247 }
248
249 // Grab grace period UNIX time.
250 $grace_period_expired = get_user_meta( $user->ID, 'wp_2fa_grace_period_expiry', true );
251
252 // Grab current time, we going to compare these later.
253 $time_now = time();
254 $is_user_excluded = WP2FA::is_user_excluded( $user->ID );
255
256 // First lets see if the user already has a token.
257 $enabled_methods = get_user_meta( $user->ID, 'wp_2fa_enabled_methods', true );
258
259 if ( ! $is_user_excluded ) {
260
261 // Check if current user viewing the profile is the owner of the profile.
262 if ( isset( $_GET['user_id'] ) ) {
263 $user_id = (int) $_GET['user_id'];
264 $user = get_user_by( 'id', $user_id );
265 $curent_user = wp_get_current_user();
266 if ( $curent_user->ID !== $user->ID ) {
267 return;
268 }
269 }
270
271 // Show this to user who has enabled methods
272 if ( ! empty( $enabled_methods ) && $user->ID === $current_user->ID ) {
273 ?>
274 <?php if ( ! isset( $is_shortcode ) && ! $is_shortcode === 'output_shortcode' ) : ?>
275 <h2>
276 <?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?>
277 </h2>
278 <?php endif; ?>
279 <?php if ( 'true' === $show_preamble ) { ?>
280 <p class="description">
281 <?php esc_html_e( 'Add two-factor authentication to strengthen the security of your WordPress user account.', 'wp-2fa' ); ?>
282 </p>
283 <?php } ?>
284 <table class="form-table" role="presentation">
285 <tbody>
286 <tr>
287 <th>
288 <label>
289 <?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?>
290 </label>
291 </th>
292 <td>
293 <a href="#" class="button button-primary remove-2fa" onclick="MicroModal.show('configure-2fa',{
294 onShow: modal => jQuery( '.wizard-step:first-of-type' ).addClass( 'active' ),
295 onClose: modal => jQuery( '.wizard-step.active' ).removeClass( 'active' ),
296 });"><?php esc_html_e( 'Change 2FA Settings', 'wp-2fa' ); ?></a>
297
298 <a href="#" class="button button-primary remove-2fa" onclick="MicroModal.show('confirm-remove-2fa');"><?php esc_html_e( 'Remove 2FA', 'wp-2fa' ); ?></a>
299
300 <a href="#" class="button button-primary remove-2fa" onclick="MicroModal.show('configure-2fa-backup-codes',{
301 onShow: modal => jQuery( '.wizard-step:first-of-type' ).addClass( 'active' ),
302 onClose: modal => jQuery( '.wizard-step.active' ).removeClass( 'active' ),
303 });"><?php esc_html_e( 'Generate Backup Codes', 'wp-2fa' ); ?></a>
304
305 <?php
306 $codes_remaining = BackupCodes::codes_remaining_for_user( $user );
307 if ( $codes_remaining > 0 ) {
308 ?>
309 <span class="description mt-5px"><?php echo esc_attr( (int) $codes_remaining ); ?> <?php esc_html_e( 'unused backup codes remaining.', 'wp-2fa' ); ?></span>
310 <?php
311 } elseif ( 0 === $codes_remaining ) {
312 ?>
313 <a class="learn_more_link" href="https://www.wpwhitesecurity.com/2fa-backup-codes/?utm_source=plugin&utm_medium=referral&utm_campaign=wp2fa&utm_content=settings+pages" target="_blank"><?php esc_html_e( 'Learn more.', 'wp-2fa' ); ?></a>
314 <?php
315 }
316 ?>
317 <div>
318 <div class="wp2fa-modal micromodal-slide" id="confirm-remove-2fa" aria-hidden="true">
319 <div class="modal__overlay" tabindex="-1" data-micromodal-close>
320 <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
321 <header class="modal__header">
322 <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
323 </header>
324 <main class="modal__content" id="modal-1-content">
325 <div>
326 <h4>
327 <?php esc_html_e( 'Remove 2FA?', 'wp-2fa' ); ?>
328 </h4>
329 <p>
330 <?php esc_html_e( 'Are you sure you want to remove two-factor authentication and lower the security of your user account?', 'wp-2fa' ); ?>
331 </p>
332 </div>
333 </main>
334 <footer class="modal__footer">
335 <a href="#" class="modal__btn modal__btn-primary button" data-trigger-remove-2fa data-user-id="<?php echo esc_attr( $user->ID ); ?>" data-nonce="<?php echo wp_create_nonce( 'wp-2fa-remove-user-2fa-nonce' ); ?>"><?php esc_html_e( 'Yes', 'wp-2fa' ); ?></a>
336 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'No', 'wp-2fa' ); ?></button>
337 </footer>
338 </div>
339 </div>
340 </div>
341 <div class="wp2fa-modal micromodal-slide" id="configure-2fa" aria-hidden="true">
342 <div class="modal__overlay" tabindex="-1" data-micromodal-close>
343 <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
344 <header class="modal__header">
345 <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
346 </header>
347 <main class="modal__content" id="modal-1-content">
348 <?php
349 // Grab current user
350 $user = wp_get_current_user();
351
352 // Grab key from user meta
353 $key = Authentication::get_user_totp_key( $user->ID );
354
355 // If no key is present, lets make one
356 if ( empty( $key ) ) {
357 $key = Authentication::generate_key();
358 $update = update_user_meta( $user->ID, 'wp_2fa_totp_key', $key );
359 }
360
361 // Setup site information, used when generating our QR code
362 $site_name = get_bloginfo( 'name', 'display' );
363 $totp_title = apply_filters( 'wp_2fa_totp_title', $site_name . ':' . $user->user_login, $user );
364
365 // Now lets grab the users enabled 2fa methods.
366 $selected_method = get_user_meta( $user->ID, 'wp_2fa_enabled_methods', true );
367
368 // Create a nonce incase we want to reset the key
369 $nonce = wp_create_nonce( 'wp-2fa-backup-codes-generate-json-' . $user->ID );
370 $validate_nonce = wp_create_nonce( 'wp-2fa-validate-authcode' );
371 ?>
372
373 <div class="wizard-step active">
374 <fieldset>
375 <?php if ( ! empty( WP2FA::get_wp2fa_setting( 'enable_totp' ) ) ) { ?>
376 <div class="option-pill">
377 <h3>
378 <?php esc_html_e( 'Reconfigure the Google Authenticator 2FA', 'wp-2fa' ); ?>
379 </h3>
380 <p>
381 <?php esc_html_e( 'Click the below button to reconfigure the current 2FA method. Note that once reset reset you will have to re-scan the QR code on all devices you want this to work on because the previous codes will stop working.', 'wp-2fa' ); ?>
382 </p>
383 <div class="wp2fa-setup-actions">
384 <a href="#" class="button button-primary" name="next_step_setting_modal_wizard" data-trigger-reset-key="no-reload" data-nonce="<?php echo esc_attr( $nonce ); ?>" data-user-id="<?php echo esc_attr( $user->ID ); ?>" data-next-step="2fa-wizard-totp"><?php esc_html_e( 'Reset Key', 'wp-2fa' ); ?></a>
385 </div>
386 </div>
387 <?php } ?>
388 <?php if ( ! empty( WP2FA::get_wp2fa_setting( 'enable_email' ) ) ) {
389 $setupnonce = wp_create_nonce( 'wp-2fa-send-setup-email' );
390 ?>
391 <br>
392 <div class="option-pill">
393 <h3><?php esc_html_e( 'Setup the 2FA method', 'wp-2fa' ); ?></h3>
394 <p>
395 <?php esc_html_e( 'Please select the email address where the one-time code should be sent:', 'wp-2fa' ); ?>
396 </p>
397 <fieldset>
398 <label for="use_wp_email">
399 <span><?php esc_html_e( 'Type in below the new email address where you want to receive the 2FA one-time codes.', 'wp-2fa' ); ?></span>
400 <br><br>
401 <input type="email" name="custom-email-address" id="custom-email-address" class="input wide" value=""/>
402 </label>
403 </fieldset>
404 <div class="wp2fa-setup-actions">
405 <a class="button button-primary" name="next_step_setting_modal_wizard" value="<?php esc_attr_e( 'I\'m Ready', 'wp-2fa' ); ?>" data-trigger-setup-email data-user-id="<?php echo esc_attr( $user->ID ); ?>" data-nonce="<?php echo esc_attr( $setupnonce ); ?>" data-next-step="2fa-wizard-email"><?php esc_html_e( 'Change email address', 'wp-2fa' ); ?></a>
406 </div>
407 </div>
408 <?php } ?>
409 </fieldset>
410 </div>
411
412 <div class="wizard-step" id="2fa-wizard-totp">
413 <fieldset>
414
415 <div class="step-setting-wrapper active">
416 <h3><?php esc_html_e( 'Setup the 2FA method', 'wp-2fa' ); ?></h3>
417 <div class="option-pill">
418 <ol>
419 <li><?php esc_html_e( 'Download the Google Authenticator app', 'wp-2fa' ); ?></li>
420 <li><?php esc_html_e( 'Click the plus sign (add new icon)', 'wp-2fa' ); ?></li>
421 <li><?php esc_html_e( 'Select \'Scan a barcode\'', 'wp-2fa' ); ?></li>
422 <li><?php esc_html_e( 'Scan the QR code to the right.', 'wp-2fa' ); ?></li>
423 </ol>
424 <p><?php esc_html_e( 'Otherwise, select Enter a provided key and type in the key below:', 'wp-2fa' ); ?></p>
425 <code><?php echo esc_html( $key ); ?></code>
426 </div>
427 <img style="right: 21px; position: absolute; top: 103px;" src="<?php echo esc_url( Authentication::get_google_qr_code( $totp_title, $key, $site_name ) ); ?>" id="wp-2fa-totp-qrcode" />
428 <br>
429 <div class="wp2fa-setup-actions">
430 <br>
431 <a class="button button-primary" name="next_step_setting"><?php esc_html_e( 'I\'m Ready', 'wp-2fa' ); ?></a>
432 </div>
433 </div>
434
435 <div class="step-setting-wrapper">
436 <h3><?php esc_html_e( 'Almost there…', 'wp-2fa' ); ?></h3>
437 <p><?php esc_html_e( 'Please type in the one-time code from your Google Authenticator app to finalize the setup.', 'wp-2fa' ); ?></p>
438 <br>
439 <fieldset>
440 <label for="2fa-totp-authcode">
441 <?php esc_html_e( 'Authentication Code:', 'wp-2fa' ); ?>
442 <input type="tel" name="wp-2fa-totp-authcode" id="wp-2fa-totp-authcode" class="input" value="" size="20" pattern="[0-9]*" />
443 </label>
444 <div class="verification-response"></div>
445 </fieldset>
446 <br>
447 <input type="hidden" name="wp-2fa-totp-key" value="<?php echo esc_attr( $key ); ?>" />
448
449 <a href="#" class="modal__btn modal__btn-primary button" data-validate-authcode-ajax data-nonce="<?php echo esc_attr( $validate_nonce ); ?>"><?php esc_html_e( 'Validate & Save Configuration', 'wp-2fa' ); ?></a>
450 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Cancel', 'wp-2fa' ); ?></button>
451 </div>
452
453 </fieldset>
454 </div>
455
456 <div class="wizard-step" id="2fa-wizard-email">
457 <fieldset>
458
459 <div class="step-setting-wrapper active">
460 <h4><?php esc_html_e( 'Almost there…', 'wp-2fa' ); ?></h4>
461 <p><?php esc_html_e( 'Please type in the one-time code sent to your email address to finalize the setup.', 'wp-2fa' ); ?></p>
462 <br>
463 <fieldset>
464 <label for="2fa-email-authcode">
465 <?php esc_html_e( 'Authentication Code:', 'wp-2fa' ); ?>
466 <input type="tel" name="wp-2fa-email-authcode" id="wp-2fa-email-authcode" class="input" value="" size="20" pattern="[0-9]*" />
467 </label>
468 <div class="verification-response"></div>
469 </fieldset>
470
471 <input type="hidden" name="wp-2fa-totp-key" value="<?php echo esc_attr( $key ); ?>" />
472 </div>
473 <br>
474 <a href="#" class="modal__btn modal__btn-primary button" data-validate-authcode-ajax data-nonce="<?php echo esc_attr( $validate_nonce ); ?>"><?php esc_html_e( 'Validate Code', 'wp-2fa' ); ?></a>
475 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Cancel', 'wp-2fa' ); ?></button>
476 </fieldset>
477 </div>
478
479 <div class="wizard-step" id="2fa-wizard-setup-done">
480 <fieldset>
481
482 <h3><?php esc_html_e( 'Your website just got more secure!', 'wp-2fa' ); ?></h3>
483 <p><?php esc_html_e( 'Congratulations! You have enabled two-factor authentication for your user. You’ve just helped towards making this website more secure!', 'wp-2fa' ); ?></p>
484
485 <p><?php esc_html_e( 'You can exit this wizard now or continue to configure the plugin’s general settings. ', 'wp-2fa' ); ?></p>
486
487 <p class="description"><?php esc_html_e( 'Note: all the settings can be configured from the Settings > Two-factor Authentication entry of your WordPress menu.', 'wp-2fa' ); ?></p>
488
489 </fieldset>
490 <a href="#" class="modal__btn modal__btn-primary button" name="next_step_setting_modal_wizard" data-next-step><?php esc_html_e( 'Next Step', 'wp-2fa' ); ?></a>
491 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Close', 'wp-2fa' ); ?></button>
492 </div>
493 </main>
494 </div>
495 </div>
496 </div>
497
498 <div class="wp2fa-modal micromodal-slide" id="configure-2fa-backup-codes" aria-hidden="true">
499 <div class="modal__overlay" tabindex="-1" data-micromodal-close>
500 <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
501 <header class="modal__header">
502 <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
503 </header>
504 <main class="modal__content" id="modal-1-content">
505 <?php
506 // Grab current user.
507 $user = wp_get_current_user();
508 // Create a nonce for use in ajax call to generate codes.
509 $nonce = wp_create_nonce( 'wp-2fa-backup-codes-generate-json-' . $user->ID );
510 ?>
511 <div class="step-setting-wrapper active">
512 <h3><?php esc_html_e( 'Generate backup codes', 'wp-2fa' ); ?></h3>
513 <p><?php esc_html_e( 'It is recommended to generate and print some backup codes in case you lose access to your primary 2FA method. ', 'wp-2fa' ); ?></p>
514
515 <div class="wp2fa-setup-actions">
516 <button class="button button-primary" name="next_step_setting" value="<?php esc_attr_e( 'Generate backup codes', 'wp-2fa' ); ?>" data-trigger-generate-backup-codes data-nonce="<?php echo esc_attr( $nonce ); ?>" data-user-id="<?php echo esc_attr( $user->ID ); ?>">
517 <?php esc_html_e( 'Generate backup codes', 'wp-2fa' ); ?>
518 </button>
519 <a href="<?php echo esc_url( admin_url() ); ?>" class="button button-secondary" type="submit" name="save_step" value="<?php esc_attr_e( 'I’ll generate them later', 'wp-2fa' ); ?>">
520 <?php esc_html_e( 'I’ll generate them later', 'wp-2fa' ); ?>
521 </a>
522 </div>
523 </div>
524
525 <div class="step-setting-wrapper align-center">
526 <h3><?php esc_html_e( 'Backup codes generated', 'wp-2fa' ); ?></h3>
527 <p><?php esc_html_e( 'Here are your backup codes:', 'wp-2fa' ); ?></p>
528 <code id="backup-codes-wrapper"></code>
529 <div class="wp2fa-setup-actions">
530 <button class="button button-primary" type="submit" value="<?php esc_attr_e( 'Download', 'wp-2fa' ); ?>" data-trigger-backup-code-download data-user="<?php echo esc_attr( $user->display_name ); ?>" data-website-url="<?php echo esc_attr( get_home_url() ); ?>">
531 <?php esc_html_e( 'Download', 'wp-2fa' ); ?>
532 </button>
533 <button class="button button-secondary" type="submit" value="<?php esc_attr_e( 'Print', 'wp-2fa' ); ?>" data-trigger-print data-nonce="<?php echo esc_attr( $nonce ); ?>" data-user-id="<?php echo esc_attr( $user->display_name ); ?>" data-website-url="<?php echo esc_attr( get_home_url() ); ?>">
534 <?php esc_html_e( 'Print', 'wp-2fa' ); ?>
535 </button>
536 </div>
537 </div>
538
539 </main>
540 </div>
541 </div>
542 </div>
543 </div>
544 </td>
545 </tr>
546 </tbody>
547 </table>
548 <?php
549
550 } else {
551 ?>
552 <?php if ( ! isset( $is_shortcode ) && ! $is_shortcode === 'output_shortcode' ) : ?>
553 <h2>
554 <?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?>
555 </h2>
556 <?php endif; ?>
557 <?php
558 $class = 'notice notice-info wp-2fa-nag';
559 $grace_expiry = get_user_meta( $user->ID, 'wp_2fa_grace_period_expiry', true );
560 $reconfigure_2fa = get_user_meta( $user->ID, 'wp_2fa_user_needs_to_reconfigure_2fa', true );
561 if ( $reconfigure_2fa ) {
562 $message = esc_html__( 'The 2FA method you were using is no longer allowed on this website. Please reconfigure 2FA using one of the supported methods within', 'wp-2fa' );
563 } else {
564 $message = esc_html__( 'This website’s administrator requires you to enable 2FA authentication within', 'wp-2fa' );
565 }
566
567 ?>
568 <?php if ( 'true' === $show_preamble ) { ?>
569 <?php if ( ! empty( $grace_expiry ) ) { ?>
570 <p class="description">
571 <?php echo $message; ?> <span class="grace-period-countdown" data-countdown-timestamp="<?php echo esc_attr( $grace_expiry ); ?>"></span>
572 </p>
573 <?php } ?>
574 <p class="description">
575 <?php esc_html_e( 'Add two-factor authentication to strengthen the security of your WordPress user account.', 'wp-2fa' ); ?>
576 </p>
577 <?php } ?>
578 <table class="form-table" role="presentation">
579 <tbody>
580 <tr>
581 <th>
582 <label>
583 <?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?>
584 </label>
585 </th>
586
587 <td>
588 <a href="#" class="button button-primary remove-2fa" onclick="MicroModal.show('configure-2fa',{
589 onShow: modal => jQuery( '.wizard-step:first-of-type' ).addClass( 'active' ),
590 onClose: modal => jQuery( '.wizard-step.active' ).removeClass( 'active' ),
591 });"><?php esc_html_e( 'Configure 2FA', 'wp-2fa' ); ?></a>
592
593 <div>
594 <div class="wp2fa-modal micromodal-slide" id="configure-2fa" aria-hidden="true">
595 <div class="modal__overlay" tabindex="-1" data-micromodal-close>
596 <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
597 <header class="modal__header">
598 <button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
599 </header>
600 <main class="modal__content" id="modal-1-content">
601 <?php
602 $user = wp_get_current_user();
603 $enabled_method = get_user_meta( $user->ID, 'wp_2fa_enabled_methods', true );
604
605 // Grab key from user meta.
606 $key = Authentication::get_user_totp_key( $user->ID );
607
608 // If no key is present, lets make one.
609 if ( empty( $key ) ) {
610 $key = Authentication::generate_key();
611 $update = update_user_meta( $user->ID, 'wp_2fa_totp_key', $key );
612 }
613 $setupnonce = wp_create_nonce( 'wp-2fa-send-setup-email' );
614 $validate_nonce = wp_create_nonce( 'wp-2fa-validate-authcode' );
615
616 // Setup site information, used when generating our QR code.
617 $site_name = get_bloginfo( 'name', 'display' );
618 $totp_title = apply_filters( 'wp_2fa_totp_title', $site_name . ':' . $user->user_login, $user );
619
620 if ( ! empty( WP2FA::get_wp2fa_setting( 'enable_totp' ) ) && ! empty( WP2FA::get_wp2fa_setting( 'enable_email' ) ) ) {
621 $intro_text = esc_html__( 'Choose the 2FA authentication method', 'wp-2fa' );
622 $sub_text = esc_html__( 'There are two methods available from which you can choose for 2FA:', 'wp-2fa' );
623 } else {
624 $intro_text = esc_html__( 'Choose the 2FA authentication method', 'wp-2fa' );
625 $sub_text = esc_html__( 'Only the below 2FA method is allowed on this website:', 'wp-2fa' );
626 }
627 ?>
628
629 <div class="wizard-step active">
630 <h3><?php echo sanitize_text_field( $intro_text ); ?></h3>
631 <p><?php echo sanitize_text_field( $sub_text ); ?></p>
632
633 <br>
634
635 <fieldset>
636 <?php if ( ! empty( WP2FA::get_wp2fa_setting( 'enable_totp' ) ) ) { ?>
637 <div class="option-pill">
638 <label for="basic">
639 <input id="basic" name="wp_2fa_enabled_methods" type="radio" value="totp" checked>
640 <?php esc_html_e( 'One-time code generated with the Google authenticator app (most reliable and secure)', 'wp-2fa' ); ?>
641 </label>
642 <?php
643 printf( '<p class="description">%1$s <a href="https://www.wpwhitesecurity.com/google-authenticator-app-wordpress-2fa/" target="_blank">%2$s</a></p>', esc_html__( 'Note: this method requires you to install the Google Authenticator app on a smart device. The app is available on Google Play and the Apple Appstore. For more information on this method read our', 'wp-2fa' ), esc_html__( 'our guide for Google Authenticator.', 'wp-2fa' ) );
644 ?>
645 </div>
646 <?php } ?>
647 <?php if ( ! empty( WP2FA::get_wp2fa_setting( 'enable_email' ) ) ) { ?>
648 <br>
649 <div class="option-pill">
650 <label for="geek">
651 <input id="geek" name="wp_2fa_enabled_methods" type="radio" value="email">
652 <?php esc_html_e( 'One-time code sent to you over email', 'wp-2fa' ); ?>
653 </label>
654 </div>
655 <?php } ?>
656 </fieldset>
657 <br><br>
658 <a href="#" class="modal__btn modal__btn-primary button 2fa-choose-method" name="next_step_setting_modal_wizard" data-next-step><?php esc_html_e( 'Next Step', 'wp-2fa' ); ?></a>
659 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Close', 'wp-2fa' ); ?></button>
660 </div>
661
662 <div class="wizard-step" id="2fa-wizard-totp">
663 <fieldset>
664
665 <div class="step-setting-wrapper active">
666 <h3><?php esc_html_e( 'Setup the 2FA method', 'wp-2fa' ); ?></h3>
667 <div class="option-pill">
668 <ol>
669 <li><?php esc_html_e( 'Download the Google Authenticator app', 'wp-2fa' ); ?></li>
670 <li><?php esc_html_e( 'Click the plus sign (add new icon)', 'wp-2fa' ); ?></li>
671 <li><?php esc_html_e( 'Select \'Scan a barcode\'', 'wp-2fa' ); ?></li>
672 <li><?php esc_html_e( 'Scan the QR code to the right.', 'wp-2fa' ); ?></li>
673 </ol>
674 <p><?php esc_html_e( 'Otherwise, select Enter a provided key and type in the key below:', 'wp-2fa' ); ?></p>
675 <code><?php echo esc_html( $key ); ?></code>
676 </div>
677 <img style="right: 21px; position: absolute; top: 103px;" src="<?php echo esc_url( Authentication::get_google_qr_code( $totp_title, $key, $site_name ) ); ?>" id="wp-2fa-totp-qrcode" />
678 <br>
679 <div class="wp2fa-setup-actions">
680 <br>
681 <a class="button button-primary" name="next_step_setting"><?php esc_html_e( 'I\'m Ready', 'wp-2fa' ); ?></a>
682 </div>
683 </div>
684
685 <div class="step-setting-wrapper">
686 <h3><?php esc_html_e( 'Almost there…', 'wp-2fa' ); ?></h3>
687 <p><?php esc_html_e( 'Please type in the one-time code from your Google Authenticator app to finalize the setup.', 'wp-2fa' ); ?></p>
688 <br>
689 <fieldset>
690 <label for="2fa-totp-authcode">
691 <?php esc_html_e( 'Authentication Code:', 'wp-2fa' ); ?>
692 <input type="tel" name="wp-2fa-totp-authcode" id="wp-2fa-totp-authcode" class="input" value="" size="20" pattern="[0-9]*" />
693 </label>
694 <div class="verification-response"></div>
695 </fieldset>
696 <input type="hidden" name="wp-2fa-totp-key" value="<?php echo esc_attr( $key ); ?>" />
697 <br>
698 <a href="#" class="modal__btn modal__btn-primary button" data-validate-authcode-ajax data-nonce="<?php echo esc_attr( $validate_nonce ); ?>"><?php esc_html_e( 'Validate & Save Configuration', 'wp-2fa' ); ?></a>
699 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Cancel', 'wp-2fa' ); ?></button>
700 </div>
701
702 </fieldset>
703 </div>
704
705 <div class="wizard-step" id="2fa-wizard-email">
706 <fieldset>
707 <div class="step-setting-wrapper active">
708 <h3><?php esc_html_e( 'Setup the 2FA method', 'wp-2fa' ); ?></h3>
709 <p>
710 <?php esc_html_e( 'Please select the email address where the one-time code should be sent:', 'wp-2fa' ); ?>
711 </p>
712 <fieldset>
713 <label for="use_wp_email">
714 <input type="radio" name="wp_2fa_email_address" id="use_wp_email" value="<?php echo esc_attr( $user->user_email ); ?>" checked>
715 <span><?php esc_html_e( 'Use my WordPress user email (', 'wp-2fa' ); ?><small><?php echo esc_attr( $user->user_email ); ?></small><?php esc_html_e( ')', 'wp-2fa' ); ?></span>
716 </label>
717 <br/>
718 <label for="use_custom_email">
719 <input type="radio" name="wp_2fa_email_address" id="use_custom_email" value="use_custom_email">
720 <span><?php esc_html_e( 'Use a different email address:', 'wp-2fa' ); ?></span>
721 <input type="email" name="custom-email-address" id="custom-email-address" class="input wide" value=""/>
722 </label>
723 </fieldset>
724 <p class="description"><?php esc_html_e( 'Note: you should be able to access the mailbox of the email address to complete the following step.', 'wp-2fa' ); ?></p>
725 <div class="wp2fa-setup-actions">
726 <a class="button button-primary" name="next_step_setting" value="<?php esc_attr_e( 'I\'m Ready', 'wp-2fa' ); ?>" data-trigger-setup-email data-user-id="<?php echo esc_attr( $user->ID ); ?>" data-nonce="<?php echo esc_attr( $setupnonce ); ?>" data-next-step="2fa-wizard-email"><?php esc_html_e( 'I\'m Ready', 'wp-2fa' ); ?></a>
727 </div>
728 </div>
729
730 <div class="step-setting-wrapper" id="2fa-wizard-email">
731 <h4><?php esc_html_e( 'Almost there…', 'wp-2fa' ); ?></h4>
732 <p><?php esc_html_e( 'Please type in the one-time code sent to your email address to finalize the setup.', 'wp-2fa' ); ?></p>
733 <fieldset>
734 <label for="2fa-email-authcode">
735 <?php esc_html_e( 'Authentication Code:', 'wp-2fa' ); ?>
736 <input type="tel" name="wp-2fa-email-authcode" id="wp-2fa-email-authcode" class="input" value="" size="20" pattern="[0-9]*" />
737 </label>
738 <div class="verification-response"></div>
739 </fieldset>
740 <input type="hidden" name="wp-2fa-totp-key" value="<?php echo esc_attr( $key ); ?>" />
741
742 <a href="#" class="modal__btn modal__btn-primary button" data-validate-authcode-ajax data-nonce="<?php echo esc_attr( $validate_nonce ); ?>"><?php esc_html_e( 'Validate & Save Configuration', 'wp-2fa' ); ?></a>
743 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Cancel', 'wp-2fa' ); ?></button>
744 </div>
745 </fieldset>
746 </div>
747
748 <div class="wizard-step" id="2fa-wizard-config-backup-codes">
749 <?php
750 // Grab current user.
751 $user = wp_get_current_user();
752 // Create a nonce for use in ajax call to generate codes.
753 $nonce = wp_create_nonce( 'wp-2fa-backup-codes-generate-json-' . $user->ID );
754 ?>
755 <div class="step-setting-wrapper active">
756 <h3><?php esc_html_e( 'Generate backup codes', 'wp-2fa' ); ?></h3>
757 <p><?php esc_html_e( 'It is recommended to generate and print some backup codes in case you lose access to your primary 2FA method. ', 'wp-2fa' ); ?></p>
758
759 <div class="wp2fa-setup-actions">
760 <button class="button button-primary" name="next_step_setting" value="<?php esc_attr_e( 'Generate backup codes', 'wp-2fa' ); ?>" data-trigger-generate-backup-codes data-nonce="<?php echo esc_attr( $nonce ); ?>" data-user-id="<?php echo esc_attr( $user->ID ); ?>">
761 <?php esc_html_e( 'Generate backup codes', 'wp-2fa' ); ?>
762 </button>
763 <a href="#" class="button button-secondary" name="save_step" value="<?php esc_attr_e( 'I’ll generate them later', 'wp-2fa' ); ?>">
764 <?php esc_html_e( 'I’ll generate them later', 'wp-2fa' ); ?>
765 </a>
766 </div>
767 </div>
768
769 <div class="step-setting-wrapper align-center">
770 <h3><?php esc_html_e( 'Backup codes generated', 'wp-2fa' ); ?></h3>
771 <p><?php esc_html_e( 'Here are your backup codes:', 'wp-2fa' ); ?></p>
772 <code id="backup-codes-wrapper"></code>
773 <div class="wp2fa-setup-actions">
774 <button class="button button-primary" type="submit" value="<?php esc_attr_e( 'Download', 'wp-2fa' ); ?>" data-trigger-backup-code-download data-user="<?php echo esc_attr( $user->display_name ); ?>" data-website-url="<?php echo esc_attr( get_home_url() ); ?>">
775 <?php esc_html_e( 'Download', 'wp-2fa' ); ?>
776 </button>
777 <button class="button button-secondary" type="submit" value="<?php esc_attr_e( 'Print', 'wp-2fa' ); ?>" data-trigger-print data-nonce="<?php echo esc_attr( $nonce ); ?>" data-user-id="<?php echo esc_attr( $user->display_name ); ?>" data-website-url="<?php echo esc_attr( get_home_url() ); ?>">
778 <?php esc_html_e( 'Print', 'wp-2fa' ); ?>
779 </button>
780 <button class="modal__btn" data-micromodal-close aria-label="Close this dialog window"><?php esc_html_e( 'Close wizard & refresh', 'wp-2fa' ); ?></button>
781 </div>
782 </div>
783 </div>
784 </main>
785 </div>
786 </div>
787 </div>
788 </div>
789 </td>
790 </tr>
791 </tbody>
792 </table>
793 <?php
794
795 }
796
797 } elseif ( $is_user_excluded ) {
798 ?>
799 <?php if ( ! isset( $is_shortcode ) && ! $is_shortcode === 'output_shortcode' ) : ?>
800 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
801 <?php endif; ?>
802 <?php if ( 'true' === $show_preamble ) { ?>
803 <p class="description"><?php esc_html_e( 'Add two-factor authentication to strengthen the security of your WordPress user account.', 'wp-2fa' ); ?></p>
804 <p class="description"><?php esc_html_e( 'Your user / role is not permitted to configure 2FA. Contact your administrator for more information.', 'wp-2fa' ); ?></p>
805 <?php } ?>
806 <?php
807
808 } elseif ( $grace_period_expired && current_user_can( 'manage_options' ) ) {
809 ?>
810 <?php if ( ! isset( $is_shortcode ) && ! $is_shortcode === 'output_shortcode' ) : ?>
811 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
812 <?php endif; ?>
813 <table class="form-table" role="presentation">
814 <tbody>
815 <tr>
816 <th><label><?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?></label></th>
817 <td>
818 <?php
819 $url = add_query_arg(
820 array(
821 'action' => 'unlock_account',
822 'user_id' => $user->ID,
823 'wp_2fa_nonce' => wp_create_nonce( 'wp-2fa-unlock-account-nonce' ),
824 ),
825 admin_url( 'user-edit.php' )
826 );
827
828 if ( $grace_period_expired ) {
829 ?>
830 <a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Unlock user and reset the grace period', 'wp-2fa' ); ?></a>
831 <?php
832 }
833 ?>
834 </td>
835 </tr>
836 </tbody>
837 </table>
838 <?php
839 } elseif ( ! $grace_period_expired && current_user_can( 'manage_options' ) && ! empty( $enabled_methods ) ) {
840 ?>
841 <?php if ( ! isset( $is_shortcode ) && ! $is_shortcode === 'output_shortcode' ) : ?>
842 <h2><?php esc_html_e( 'WP 2FA Settings', 'wp-2fa' ); ?></h2>
843 <?php endif; ?>
844 <p class="description"><?php esc_html_e( 'By resetting this user\'s 2FA configuration you disable the currently configured 2FA so the user can log back in using a usemame and a password only. *the user if enforced to setup 2FA the user will get a prompt upon logging in, from where 2FA can be configured. The user has to configure 2FA within the configured grace period.', 'wp-2fa' ); ?></p>
845 <table class="form-table" role="presentation">
846 <tbody>
847 <tr>
848 <th><label><?php esc_html_e( '2-Factor authentication', 'wp-2fa' ); ?></label></th>
849 <td>
850 <?php
851 // Create remove 2fa link.
852 $url = add_query_arg(
853 array(
854 'action' => 'remove_user_2fa',
855 'user_id' => $user->ID,
856 'wp_2fa_nonce' => wp_create_nonce( 'wp-2fa-remove-user-2fa-nonce' ),
857 'admin_reset' => 'yes',
858 ),
859 admin_url( 'user-edit.php' )
860 );
861 ?>
862 <a href="<?php echo esc_url( $url ); ?>" class="button button-primary"><?php esc_html_e( 'Reset 2FA configuration', 'wp-2fa' ); ?></a>
863 </td>
864 </tr>
865 </tbody>
866 </table>
867 <?php
868 }
869 }
870
871 /**
872 * Add custom unlock account link to user edit admin list.
873 *
874 * @param string $actions Default actions.
875 * @param object $user_object User data.
876 * @return string Appended actions.
877 */
878 public function user_2fa_row_actions( $actions, $user_object ) {
879 $nonce = wp_create_nonce( 'wp-2fa-unlock-account-nonce' );
880 $grace_period_expired = get_user_meta( $user_object->ID, 'wp_2fa_user_grace_period_expired', true );
881 $url = add_query_arg(
882 array(
883 'action' => 'unlock_account',
884 'user_id' => $user_object->ID,
885 'wp_2fa_nonce' => $nonce,
886 ),
887 admin_url( 'users.php' )
888 );
889
890 if ( $grace_period_expired ) {
891 $actions['edit_badges'] = '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Unlock user', 'wp-2fa' ) . '</a>';
892 }
893 return $actions;
894 }
895
896 /**
897 * Save user profile information.
898 */
899 public function save_user_2fa_options( $input ) {
900
901 // Ensure we have the inputs we want before we process.
902 // To avoid causing issues with the rest of the user profile.
903 if ( ! is_array( $input ) ) {
904 return;
905 }
906
907 // Assign the input to post, in case we are dealing with saving the data from another page.
908 if ( isset( $input ) ) {
909 $_POST = $input;
910 } else {
911 $_POST = $_POST;
912 }
913
914 // Grab current user.
915 $user = wp_get_current_user();
916
917 // Setup some empty arrays which will may fill later, should an error arise along the way.
918 $notices = array();
919 $errors = array();
920
921 // Grab key from the $_POST.
922 if ( isset( $_POST['wp-2fa-totp-key'] ) ) {
923 $current_key = sanitize_text_field( wp_unslash( $_POST['wp-2fa-totp-key'] ) );
924 }
925
926 // Grab authcode and ensure its a number.
927 if ( isset( $_POST['wp-2fa-totp-authcode'] ) ) {
928 $_POST['wp-2fa-totp-authcode'] = (int) $_POST['wp-2fa-totp-authcode'];
929 }
930
931 if ( ! isset( $_POST['custom-email-address'] ) || isset( $_POST['custom-email-address'] ) && empty( $_POST['custom-email-address'] ) ) {
932 if ( isset( $_POST['email'] ) ) {
933 update_user_meta( $user->ID, 'wp_2fa_nominated_email_address', $_POST['email'] );
934 } elseif ( isset( $_POST['wp_2fa_email_address'] ) ) {
935 update_user_meta( $user->ID, 'wp_2fa_nominated_email_address', $_POST['wp_2fa_email_address'] );
936 }
937
938 } elseif ( isset( $_POST['custom-email-address'] ) ) {
939 update_user_meta( $user->ID, 'wp_2fa_nominated_email_address', sanitize_email( wp_unslash( $_POST['custom-email-address'] ) ) );
940 }
941
942 // Now lets grab the users enabled 2fa methods.
943 $get_array = filter_input_array( INPUT_GET );
944 $selected_method = sanitize_text_field( $get_array['enabled_methods'] );
945 // Check its one of our options.
946 if ( isset( $_POST['wp_2fa_enabled_methods'] ) && 'totp' === $_POST['wp_2fa_enabled_methods'] || isset( $_POST['wp_2fa_enabled_methods'] ) && 'email' === $_POST['wp_2fa_enabled_methods'] ) {
947 update_user_meta( $user->ID, 'wp_2fa_enabled_methods', sanitize_text_field( wp_unslash( $_POST['wp_2fa_enabled_methods'] ) ) );
948 delete_user_meta( $user->ID, 'wp_2fa_grace_period_expiry' );
949 }
950
951 if ( isset( $_POST['wp-2fa-email-authcode'] ) && ! empty( $_POST['wp-2fa-email-authcode'] ) ) {
952 update_user_meta( $user->ID, 'wp_2fa_enabled_methods', 'email' );
953 delete_user_meta( $user->ID, 'wp_2fa_grace_period_expiry' );
954 }
955
956 if ( isset( $_POST['wp-2fa-totp-authcode'] ) && ! empty( $_POST['wp-2fa-totp-authcode'] ) ) {
957 update_user_meta( $user->ID, 'wp_2fa_enabled_methods', 'totp' );
958 delete_user_meta( $user->ID, 'wp_2fa_grace_period_expiry' );
959 }
960 }
961
962 /**
963 * Validate a user's code when setting up 2fa via the inline form.
964 *
965 * @return json result of validation.
966 */
967 public function validate_authcode_via_ajax() {
968 check_ajax_referer( 'wp-2fa-validate-authcode' );
969
970 if ( isset( $_POST['form'] ) ) {
971 $input = $_POST['form'];
972 } else {
973 return 'No form';
974 }
975
976 $user = wp_get_current_user();
977
978 // Setup some empty arrays which will may fill later, should an error arise along the way.
979 $notices = array();
980 $our_errors = '';
981
982 // Grab key from the $_POST.
983 if ( isset( $input['wp-2fa-totp-key'] ) ) {
984 $current_key = sanitize_text_field( wp_unslash( $input['wp-2fa-totp-key'] ) );
985 }
986
987 // Grab authcode and ensure its a number.
988 if ( isset( $input['wp-2fa-totp-authcode'] ) ) {
989 $input['wp-2fa-totp-authcode'] = (int) $input['wp-2fa-totp-authcode'];
990 }
991
992 // Check if we are dealing with totp or email, if totp validate and store a new secret key.
993 if ( ! empty( $input['wp-2fa-totp-authcode'] ) && ! empty( $current_key ) ) {
994 if ( Authentication::is_valid_key( $current_key ) || ! is_numeric( $input['wp-2fa-totp-authcode'] ) ) {
995 if ( ! Authentication::is_valid_authcode( $current_key, sanitize_text_field( wp_unslash( $input['wp-2fa-totp-authcode'] ) ) ) ) {
996 $our_errors = esc_html__( 'Invalid Two Factor Authentication code.', 'wp-2fa' );
997 }
998 } else {
999 $our_errors = esc_html__( 'Invalid Two Factor Authentication secret key.', 'wp-2fa' );
1000 }
1001
1002 // If its not totp, is it email.
1003 } elseif ( ! empty( $input['wp-2fa-email-authcode'] ) ) {
1004 if ( ! Authentication::validate_token( $user->ID, sanitize_text_field( wp_unslash( $input['wp-2fa-email-authcode'] ) ) ) ) {
1005 $our_errors = __( 'Invalid Email Authentication code.', 'wp-2fa' );
1006 }
1007 } else {
1008 $our_errors = __( 'Please enter the code to finalize the 2FA setup.', 'wp-2fa' );
1009 }
1010
1011 if ( ! empty( $our_errors ) ) {
1012 // Send the response.
1013 wp_send_json_error(
1014 array(
1015 'error' => $our_errors,
1016 )
1017 );
1018 } else {
1019 $this->save_user_2fa_options( $input );
1020 // Send the response.
1021 wp_send_json_success();
1022 }
1023 }
1024 }
1025