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

authn: Resave an on-the-fly updated user object back to the stored session #480

Merged
merged 1 commit into from
Mar 8, 2022

Conversation

tsibley
Copy link
Member

@tsibley tsibley commented Feb 23, 2022

Rather than perpetually updating it on-the-fly with every request.

Resolves a downside of "authn: Upgrade existing sessions which pre-date
addition of authzRoles" (272ed09) and was simpler to implement than I
thought!

Related issue(s)

Related to #479 and #452.

Testing

I tested locally (which uses a file-based session store) by first acquiring an "old" session (i.e. logging in on an earlier version of the codebase), which contained a user object like:

$ jq '.passport.user | fromjson' sessions/lMG7Ra3e79V5LuefCy2ObeMzucT4q5u6.json  
{
  "username": "trs",
  "groups": [
    "test/owners",
    "blab-private",
    "seattleflu",
    "trs-test/owners",
    "test-private/editors",
    "blab",
    "inrb-drc"
  ]
}

then I restarted my local server with the code in this PR and made another request, after which my session contained the expected updates:

$ jq '.passport.user | fromjson' sessions/lMG7Ra3e79V5LuefCy2ObeMzucT4q5u6.json  
{
  "username": "trs",
  "groups": [
    "test",
    "blab-private",
    "seattleflu",
    "trs-test",
    "test-private",
    "blab",
    "inrb-drc"
  ],
  "authzRoles": [
    "test/owners",
    "blab-private/viewers",
    "seattleflu/viewers",
    "trs-test/owners",
    "test-private/editors",
    "blab/viewers",
    "inrb-drc/viewers"
  ]
}

…ssion

Rather than perpetually updating it on-the-fly with every request.

Resolves a downside of "authn: Upgrade existing sessions which pre-date
addition of authzRoles" (272ed09) and was simpler to implement than I
thought!
@tsibley tsibley force-pushed the trs/resave-updated-user-to-session branch from 0d2a4ea to 4225fcf Compare February 23, 2022 23:27
@tsibley tsibley temporarily deployed to nextstrain-s-trs-resave-b8zho3 February 23, 2022 23:27 Inactive
@tsibley tsibley requested a review from a team February 23, 2022 23:28
Copy link
Contributor

@joverlee521 joverlee521 left a comment

Choose a reason for hiding this comment

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

Very cool!

// the user object in the session via req.login(). The session will detect
// its been changed and save itself to the store at the end of the request.
app.use((req, res, next) => {
if (req.user?.[RESAVE_TO_SESSION]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, didn't know optional chaining was a thing! 🤓

Copy link
Member Author

Choose a reason for hiding this comment

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

We couldn't use it here until the recent version bump from Node 13 → 14, so it's a nice perk of that! In the same family is also ??, the "nullish coalescing operator".

@tsibley tsibley merged commit 029c0e6 into master Mar 8, 2022
@tsibley tsibley deleted the trs/resave-updated-user-to-session branch March 8, 2022 18:18
@tsibley
Copy link
Member Author

tsibley commented Mar 8, 2022

Deployed and working:

Mar 08 10:50:03 nextstrain-server app/web.3 [verbose]	Upgrading groups and authzRoles of user object for trs
Mar 08 10:50:03 nextstrain-server app/web.3 [verbose]	Resaving user object for trs back to the session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

2 participants