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

fix/enhancement - Fix slow auth tokens. #164

Merged
merged 1 commit into from
Aug 15, 2019
Merged

fix/enhancement - Fix slow auth tokens. #164

merged 1 commit into from
Aug 15, 2019

Conversation

jwag956
Copy link
Collaborator

@jwag956 jwag956 commented Aug 14, 2019

Current auth tokens are slow because they use the user's password (hashed) as a uniquifier (the
user id isn't really enough since it might be reused). This requires checking the (hashed) password against
what is in the token on EVERY request - however hashing is (on purpose) slow. So this can add almost a whole second
to every request!

This PR introduces a new UserModel field - fs_uniquifier - that if present in the UserModel will be populated and used rather than the password. This results in 50x reduction in time when authenticating via token.

Furthermore, the actual token verification has been moved from request_loader into the UserMixin - so
that it could be overridden (creating the auth_token already was in the UserMixin).

Note that this does require a DB migration to add the field. The fsqla model has been updated, and docs
describing at least one way to migrate the DB have been added.

2 new backwards compatibility configurations have been added that can revert some new default behavior.

First - in the past- the auth token was included always in JSON responses to login, reset and change -
even if the caller was a browser. This is
really not great since auth tokens may have very long expire times (or none) and it shouldn't even be sent if not needed.
Now, by default, the auth token is NEVER returned - the caller may request is during login, reset, or change by adding the
'include_auth_token' query param.

Second, since auth tokens used to be checked against the hashed password - changing a user's password meant that any
outstanding auth tokens would be invalidated. That seems like strange behavior - so by default, tokens that are verified
with the new fs_uniquifier won't be invalidated just because the user's password changes. The
BACKWARDS_COMPAT_AUTH_TOKEN_INVALID config variable will cause the fs_uniquifier to be changed whenever the user's password changes, thus restoring the older behavior.

closes: #156

@jwag956 jwag956 self-assigned this Aug 14, 2019
@jwag956 jwag956 force-pushed the tokenhash branch 2 times, most recently from 97cadd5 to 22eb548 Compare August 15, 2019 04:21
Current auth tokens are slow because they use the user's password (hashed) as a uniquifier (the
user id isn't really enough since it might be reused). This requires checking the (hashed) password against
what is in the token on EVERY request - however hashing is (on purpose) slow. So this can add almost a whole second
to every request!

This PR introduces a new UserModel field - fs_uniquifier - that if present in the UserModel will be populated and used rather than the password. This results in 50x reduction in time when authenticating via token.

Furthermore, the actual token verification has been moved from request_loader into the UserMixin - so
that it could be overridden (creating the auth_token already was in the UserMixin).

Note that this does require a DB migration to add the field. The fsqla model has been updated, and docs
describing at least one way to migrate the DB have been added.

2 new backwards compatibility configurations have been added that can revert some new default behavior.

First - in the past- the auth token was included always in JSON responses to login, reset and change -
even if the caller was a browser. This is
really not great since auth tokens may have very long expire times (or none) and it shouldn't even be sent if not needed.
Now, by default, the auth token is NEVER returned - the caller may request is during login, reset, or change by adding the
'include_auth_token' query param.

Second, since auth tokens used to be checked against the hashed password - changing a user's password meant that any
outstanding auth tokens would be invalidated. That seems like strange behavior - so by default, tokens that are verified
with the new fs_uniquifier won't be invalidated just because the user's password changes. The
BACKWARDS_COMPAT_AUTH_TOKEN_INVALID config variable will cause the fs_uniquifier to be changed whenever the user's password changes, thus restoring the older behavior.

closes: #156
@jwag956 jwag956 merged commit ae027a5 into master Aug 15, 2019
@jwag956 jwag956 deleted the tokenhash branch August 15, 2019 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Authtokens are slow
1 participant