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

Add user to context after login and signup #3088

Merged
merged 5 commits into from
Sep 2, 2022

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Aug 31, 2022

Summary

Primarily this PR sets RequestContext.Authenticated.User after login and signup. Once added to the request context it will appear in the API request log. All other requests already have this value set. We don't have any way to test our logs right now, so I tested this manually on a branch that included #3073, and I saw the userId set on the log line for login.

To accomplish this goal I did a bit of refactoring to expose the user we had already fetched from the database. I tried to split that refactor into smaller commits, so view by individual commit if the full diff doesn't read well.

As part of this change I removed the LoginMethod.RequiresUpdate method, which had a TODO on it to be removed. The issue the TODO links to is already closed.

Related Issues

Along with the changes in #3073, this PR resolves #3031

Comment on lines 10 to 11
func Login(c RequestContext, loginMethod authn.LoginMethod, keyExpiresAt time.Time, keyExtension time.Duration) (authn.LoginResult, error) {
return authn.Login(c.Request.Context(), c.DBTxn, loginMethod, keyExpiresAt, keyExtension)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would really like to remove this access.Login function, but I suspect that is a controversial change.

If we remove this function we'd have the api handler calling the authn.Login directly, which removes one hop when tracing the code. The access.Login function does nothing except pull two values out of RequestContext, which could easily be done by the caller without adding additional lines.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that's actually a good idea, login doesn't really have the concept of an "access check" anyway.

Copy link
Collaborator

@BruceMacD BruceMacD left a comment

Choose a reason for hiding this comment

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

Looks good, feel free to cut the login access layer before merging

Comment on lines 10 to 11
func Login(c RequestContext, loginMethod authn.LoginMethod, keyExpiresAt time.Time, keyExtension time.Duration) (authn.LoginResult, error) {
return authn.Login(c.Request.Context(), c.DBTxn, loginMethod, keyExpiresAt, keyExtension)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that's actually a good idea, login doesn't really have the concept of an "access check" anyway.

So that we can add it to the request context for logging.
The TODOs here suggested that we might be able to remove this field entirely, but it looks
like the associated issue was closed and we may still need it. I suspect we should keep this
field instead of trying to infer it from some other data, so that we can use it in other cases
in the future.

For example, we may have to invalidate passwords and force someone to create a new one, and this
field would work great for that purpose.

The RequiresUpdate method is removed in favor of returning the value on the existing struct. This
also removes the need to query for the user and credentials a second time.
So that logging middleware can include the userID in the log message for
the request.
@dnephin dnephin force-pushed the dnephin/add-user-to-context-after-login branch from 720c809 to 2746fdb Compare September 2, 2022 16:18
@dnephin
Copy link
Contributor Author

dnephin commented Sep 2, 2022

I pushed one more commit that adds API test coverage for login with a temp password, to make sure I did not mess up anything with the refactor.

@dnephin dnephin merged commit e057c56 into main Sep 2, 2022
@dnephin dnephin deleted the dnephin/add-user-to-context-after-login branch September 2, 2022 19:09
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.

Provide detailed logging for an user request
2 participants