Skip to content

Fixed api_access_only toggle to generate otp_secret when missing#1084

Open
philayres wants to merge 2 commits intoconsected:developfrom
hmsrc:fix-api-access-only-2fa-otp-secret-1083
Open

Fixed api_access_only toggle to generate otp_secret when missing#1084
philayres wants to merge 2 commits intoconsected:developfrom
hmsrc:fix-api-access-only-2fa-otp-secret-1083

Conversation

@philayres
Copy link
Copy Markdown
Contributor

Summary

Fixes #1083

When toggling an existing user to api_access_only = true, the handle_api_access_only_change callback now generates a missing otp_secret if one was never set. This can occur when a user was created while TwoFactorAuthDisabledForUser was true, so setup_two_factor_auth skipped secret generation at creation time.

Without this fix, such a user ends up with otp_secret = nil and otp_required_for_login = true, causing two_factor_setup_required? to return true. The before_action in UserBaseController then redirects every request — including token-authenticated API calls — to /users/show_otp.

Changes

  • app/models/concerns/standard_authentication.rb: In handle_api_access_only_change, generate otp_secret if absent before setting otp_required_for_login = true
  • spec/models/user_api_access_only_spec.rb: New test covering the gap — creates a user with 2FA disabled (no otp_secret), re-enables 2FA, toggles to api_access_only, and asserts all three conditions are correct (otp_secret present, otp_required_for_login true, two_factor_setup_required? false)

@philayres philayres force-pushed the fix-api-access-only-2fa-otp-secret-1083 branch from f8161ff to 4bd9165 Compare April 22, 2026 14:49
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.

api_access_only toggle doesn't generate otp_secret for users created with 2FA disabled

1 participant