Skip to content

Fix login loop for teachers with two-factor logging in on My Courses - #8475

Open
benitoalba wants to merge 1 commit into
Automattic:trunkfrom
benitoalba:fix/8470-teacher-login-redirect-two-factor
Open

benitoalba wants to merge 1 commit into
Automattic:trunkfrom
benitoalba:fix/8470-teacher-login-redirect-two-factor

Conversation

@benitoalba

Copy link
Copy Markdown

Resolves #8470

Proposed Changes

  • teacher_login_redirect() no longer redirects and exits on wp_login. When a teacher (or admin) logs in on the My Courses page and the request doesn't already ask for a redirect, it sets redirect_to to the dashboard instead.
  • That lets later wp_login handlers run. Two Factor shows its code screen from one at PHP_INT_MAX, so teachers with 2FA now get the code screen instead of a login loop.
  • The teacher still ends up on the dashboard. Sensei's login handler already reads redirect_to after wp_signon(), and Two Factor carries redirect_to through its code form and redirects there once the code is accepted.

I set redirect_to here rather than moving the redirect to the login_redirect filter, as the triage suggested, because the My Courses form posts to the My Courses page, not to wp-login.php, so login_redirect never runs for it. Two Factor 0.17.0 also redirects to whatever redirect_to it was given after the code, and Sensei's form doesn't send one, so with only a filter the teacher would end up on an empty redirect after entering the code.

A redirect_to the request already asks for is kept, the same as before. Students, and teachers who log in elsewhere, are unaffected.

Testing Instructions

  1. Install and activate Two Factor (tested with 0.17.0).
  2. Create a user with the Teacher role and turn on a Two Factor provider for them (Users > Profile > Two-Factor Options; the email provider works).
  3. Log out, open the My Courses page and log in as that teacher with Sensei's login form.
  4. On trunk: you're sent to wp-admin and straight back to wp-login.php?...&reauth=1, with no code screen.
  5. On this branch: the Two Factor code screen shows. Enter the code and you land on the dashboard, logged in.
  6. Log in on the My Courses page as a teacher without 2FA: you land on the dashboard, as before.
  7. Log in on the My Courses page as a student: you stay on the My Courses page, as before.

I ran steps 3 to 7 on wp-env with Two Factor 0.17.0 (Dummy provider) in Chromium. New PHPUnit tests in Sensei_Class_Teacher_Test cover the dashboard redirect, a requested redirect being kept, students and other pages, and later wp_login handlers still running. The class passes (31 tests), and the three teacher-on-My-Courses tests fail on trunk. scripts/linter-ci reports no new issues.

teacher_login_redirect() redirected and exited on wp_login at priority 99,
so later wp_login handlers never ran. Two Factor shows its code screen from
a wp_login handler at PHP_INT_MAX, so teachers with 2FA who logged in on the
My Courses page were sent to wp-admin without a session and back to
wp-login.php in a loop.

Request the dashboard redirect instead, when the login request doesn't ask
for one. Sensei's login handler and Two Factor's code form both read
redirect_to, so the teacher still lands on the dashboard once the login
finishes.

Fixes Automattic#8470.

This branch has not been deployed

No deployments
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.

teacher_login_redirect exits before Two Factor shows the code screen

1 participant