Fix #644: Expose existing 2FA revalidation flow to third-party plugins via action hook - #924
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This hook allows third-party plugins to easily trigger a 2FA re-authentication flow for sensitive actions. Fixes WordPress#644.
48c33a9 to
24d111a
Compare
There was a problem hiding this comment.
Pull request overview
This PR exposes the plugin’s existing “revalidate 2FA session” login flow to third-party code by introducing a public action hook that can enforce a “recent 2FA” requirement before sensitive operations.
Changes:
- Adds a new
two_factor_revalidate_sessionaction hook wired toTwo_Factor_Core::action_revalidate_session(). - Implements the revalidation check and redirects to the existing
revalidate_2fascreen when the 2FA session is outside a caller-provided time window. - Adds PHPUnit coverage for “redirect when stale” and “bypass when recent” scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| class-two-factor-core.php | Registers the new action hook and implements action_revalidate_session() to enforce a recent 2FA session via redirect to the existing revalidate flow. |
| tests/class-two-factor-core.php | Adds unit tests covering redirect vs. bypass behavior for the new revalidation action. |
Comments suppressed due to low confidence (1)
tests/class-two-factor-core.php:2751
update_current_user_session()depends on an auth cookie session token; without settingwp_set_auth_cookie( $user_id )this test will always seeis_current_user_session_two_factor()return false and will incorrectly redirect even for a recent session.
$user_id = self::factory()->user->create();
wp_set_current_user( $user_id );
update_user_meta( $user_id, Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY, array( 'Two_Factor_Dummy' ) );
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| $reauth_url = self::get_user_two_factor_revalidate_url(); | ||
| $reauth_url = add_query_arg( 'redirect_to', urlencode( $redirect_to ), $reauth_url ); |
| } | ||
|
|
||
| $last_2fa = self::is_current_user_session_two_factor(); | ||
| $is_recent = $last_2fa && ( time() - $last_2fa < (int) $time_window ); |
| $user_id = self::factory()->user->create(); | ||
| wp_set_current_user( $user_id ); | ||
| update_user_meta( $user_id, Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY, array( 'Two_Factor_Dummy' ) ); | ||
| update_user_meta( $user_id, Two_Factor_Core::PROVIDER_USER_META_KEY, 'Two_Factor_Dummy' ); |
|
Hi @masteradhoc , |
What?
Introduces the two_factor_revalidate_session action hook which allows developers to programmatically trigger a 2FA re-authentication check.
Fixes #644
Why?
There are cases where other plugins or custom site logic need to verify that an authenticated user has recently passed a 2FA check before allowing them to perform a sensitive action (such as accessing a billing portal or publishing a high-profile post). This PR exposes the core plugin's existing re-authentication flow through a standardized, easy-to-use action hook.
How?
Use of AI Tools
AI Assistance: No
Testing Instructions
add_action( 'admin_init', function() { global $pagenow; if ( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) { // Force re-auth if the 2FA session is older than 60 seconds do_action( 'two_factor_revalidate_session', 60 ); } } );Changelog Entry
Added - two_factor_revalidate_session action hook to allow third-party code to trigger a 2FA revalidation flow for an existing user session.