Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2816] Split user/password auth from token verification #1449

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

swrichards
Copy link
Contributor

Previously, the UserModelEmailBackend was overloaded to support both username/password authentication, as well as TOTP validation. It did this, in part, by verifying the 2FA flag on the site configuration.

This was both confusing (an authentication backend should do one thing only, multiple logics means multiple backends), as well as mixing concerns (the view should decide which arguments to pass to to authentication backend based on the site configuration, authentication backends should only authenticate).

This commit separates both concerns into independent backends, and adds some tests to ensure that they are properly invoked.

@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch from a657993 to 624ce24 Compare October 16, 2024 16:21
@codecov-commenter
Copy link

codecov-commenter commented Oct 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.61%. Comparing base (0ee4d7d) to head (d9a06c0).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1449      +/-   ##
===========================================
+ Coverage    94.59%   94.61%   +0.02%     
===========================================
  Files         1071     1071              
  Lines        39704    39794      +90     
===========================================
+ Hits         37557    37653      +96     
+ Misses        2147     2141       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch from a8a696b to de8eb28 Compare October 16, 2024 17:12
@swrichards swrichards marked this pull request as ready for review October 16, 2024 17:13
@swrichards swrichards requested a review from pi-sigma October 16, 2024 18:07
@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch 3 times, most recently from 13d404b to e1899ae Compare October 17, 2024 08:37
Copy link
Contributor

@pi-sigma pi-sigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, couple stylistic suggestiions

src/open_inwoner/accounts/backends.py Show resolved Hide resolved
src/open_inwoner/accounts/backends.py Show resolved Hide resolved

cls.user = UserFactory(login_type=LoginTypeChoices.default)
cls.expires_in = getattr(settings, "ACCOUNTS_USER_TOKEN_EXPIRE_TIME", 300)
cls.make_token = lambda: totp(cls.user.seed, period=cls.expires_in)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cls.make_token = lambda: totp(cls.user.seed, period=cls.expires_in)
cls.token = lambda: totp(cls.user.seed, period=cls.expires_in)

The value for this will be the same for all tests, so it's a bit misleading to call it make_token (which to me suggests that a new token is made every time this is called)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The totp function does use the current timestamp, and not all tests freeze the time (or in the same way), so I thought it'd still be easier to reason about if it's a callable. But I will add a freeze_time to the whole class, because time-based tests should be deterministic.

@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch from afc0998 to d9a06c0 Compare October 17, 2024 13:09
@swrichards swrichards requested a review from pi-sigma October 17, 2024 15:10
@alextreme
Copy link
Member

This doesn't seem to check config.login_2fa_sms anymore? This is a PR we should discuss on monday, I may be overlooking something on how this works

@swrichards
Copy link
Contributor Author

This doesn't seem to check config.login_2fa_sms anymore? This is a PR we should discuss on monday, I may be overlooking something on how this works

That was intentional -- these are authentication backends, their task should be solely to accept some kind of credentials and return a user upon successful validation. Whether or not a token is required is a view-level concern, where the actual session is created if the right conditions are met (in this case, the username/password view doesn't actually create a session, it sends a token if the credentials are a valid, and the token verification view creates the session. I added a test to validate that behavior).

The previous code was doing too much in two senses: it was validating both a token and a username/password, and it's behavior changed depending on the config, which is a controller/view level concern.

@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch from d9a06c0 to 33b6f1a Compare October 21, 2024 08:00
Previously, the UserModelEmailBackend was overloaded to
support both username/password authentication, as well as
TOTP validation. It did this, in part, by verifying the
2FA flag on the site configuration.

This was both confusing (an authentication backend should
do one thing only, multiple logics means multiple backends),
as well as mixing concerns (the _view_ should decide which
arguments to pass to to authentication backend based on the
site configuration, authentication backends should only
authenticate).

This commit separates both concerns into independent backends,
and adds some tests to ensure that they are properly invoked.
@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch 2 times, most recently from 0d5dee3 to c3d84a6 Compare October 21, 2024 09:01
@alextreme
Copy link
Member

Discussed and change is okay after tests pass

@swrichards swrichards force-pushed the issues/2816-refactor-user-password-auth-logic branch from c3d84a6 to 9b0e8c1 Compare October 21, 2024 10:49
@swrichards swrichards marked this pull request as draft October 25, 2024 08:23
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.

4 participants