Skip to content

fix(wp-native-auth): browser handoff honors challenge policies and the minting session - #125

Merged
chubes4 merged 1 commit into
mainfrom
fix/handoff-challenge-policy
Nov 17, 2026
Merged

chubes4 merged 1 commit into
mainfrom
fix/handoff-challenge-policy

Conversation

@chubes4

@chubes4 chubes4 commented Nov 17, 2026

Copy link
Copy Markdown
Member

Fixes #124

A browser handoff can no longer turn a session that skipped a challenge policy into a web session. The receiver now checks the policies and the session that minted the token before it sets a cookie, and fires wp_login when it does.

Change

Mint (wp_native_auth_create_handoff_token( $user_id, ?string $device_id = null )): the token records the minting device session. It defaults to the current bearer session when that session belongs to the user. The wp-native/auth-browser-handoff ability gets this with no code change. A token minted without a bearer session records device_id: null.

Consume (wp_native_auth_process_browser_handoff(), called by the init receiver), through the new wp_native_auth_check_browser_handoff():

  1. If a minting session was recorded, it must still be active. Otherwise: handoff_session_inactive.
  2. Registered challenge policies are probed in registration order, with reason and phase set to browser_handoff. As on login, the first policy that would challenge decides. The minting session must have challenge_policy equal to that policy and a non-null challenge_verified_at. Otherwise: handoff_challenge_required (with data.challenge_policy). A policy WP_Error refuses the handoff.
  3. New filter wp_native_auth_pre_browser_handoff (null|WP_Error, WP_User, {device_id, session, challenge_policy}). It runs only after the policies allow the handoff, so it can refuse one but never override a policy refusal.
Before After
Allowed handoff cookie set, no wp_login cookie set, then wp_login, then wp_native_auth_after_browser_handoff
Policy challenges the user, minting session verified by that policy cookie cookie
Policy challenges the user, session never verified, verified by another policy, or no session cookie (bypass) no cookie, redirect to wp_login_url( $destination )
Minting session revoked within the TTL cookie no cookie, redirect to login
Invalid or expired token wp_die() 403 screen no cookie, redirect to login
Refused, but the browser is already signed in as that user n/a straight to the destination, no new session

Refusals fire wp_native_auth_browser_handoff_refused ($user_id, WP_Error). The login redirect can be changed with wp_native_auth_browser_handoff_refused_redirect. The destination is now absolute (site origin + request path, minus the handoff param), so login redirects can carry it.

Notes for integrations

  • Challenge callbacks are now also called as a probe with reason: 'browser_handoff'. A policy should only report whether it would challenge, and should not deliver anything (for example, send a code) for that reason. This is documented in SCHEMAS.md.
  • wp_native_auth_validate_handoff_token() keeps its signature and return value. wp_native_auth_consume_handoff_token() returns the full handoff (user_id, device_id, created_at).
  • No policy registered: behavior is unchanged except for the wp_login parity and the redirect on an invalid token. Tokens minted before deploy have no device_id and are treated as "no session". The TTL is 60 seconds.

Tests

tests/test-browser-handoff-challenge.php (16 tests) mints real tokens from real bearer sessions (password login, optionally continued through a test policy) and consumes them in a signed-out request. It covers each row of the table above, plus: the device is recorded for the bearer's own user only, single use, probe context, policy error, filter can refuse but not allow, filterable refusal redirect, and the destination drops the handoff param.

  • homeboy review test: 163 tests, 162 passed, 1 skipped (pre-existing skip). main: 147 tests, 146 passed, 1 skipped. The +16 are the new tests.
  • homeboy review lint: PHPCS 0 errors (warnings are the existing baseline), PHPStan level 7 clean, no drift.
  • The diff names no specific second-factor plugin.

Written directly by Extra Chill Bot (AI agent) for Chris Huber.

…e minting session

Handoff tokens record the device session that minted them. Before the
receiver sets a cookie, the first challenge policy that would challenge
the user must have verified that session; a revoked session or a policy
error refuses the handoff. A generic wp_native_auth_pre_browser_handoff
filter can refuse further. Successful handoffs now fire wp_login; refused
or invalid ones redirect to wp_login_url() instead of wp_die().

Fixes #124
@chubes4
chubes4 merged commit 91c7479 into main Nov 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp-native-auth: browser handoff sets a cookie without checking challenge policies or the minting session

1 participant