Skip to content

[server] update OIDC users on sign-in (email) – WEB-370 #17633

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

Merged
merged 2 commits into from
May 22, 2023

Conversation

AlexTugarev
Copy link
Member

Updates email address of SSO users on each sign-in.

Related Issue(s)

Fixes WEB-370

How to test

Documentation

Preview status

gitpod:summary

Build Options:

  • /werft with-werft
    Run the build with werft instead of GHA
  • leeway-no-cache
  • /werft no-test
    Run Leeway with --dont-test
Publish Options
  • /werft publish-to-npm
  • /werft publish-to-jb-marketplace
Installer Options
  • analytics=segment
  • with-dedicated-emulation
  • with-ws-manager-mk2
  • workspace-feature-flags
    Add desired feature flags to the end of the line above, space separated

Preview Environment Options:

  • /werft with-local-preview
    If enabled this will build install/preview
  • /werft with-preview
  • /werft with-large-vm
  • /werft with-gce-vm
    If enabled this will create the environment on GCE infra
  • with-integration-tests=all
    Valid options are all, workspace, webapp, ide, jetbrains, vscode, ssh

/hold

@AlexTugarev AlexTugarev marked this pull request as ready for review May 17, 2023 10:48
@AlexTugarev AlexTugarev requested a review from a team May 17, 2023 10:48
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label May 17, 2023
Comment on lines +265 to +266
expect(newEmail, "update was not called").not.to.be.undefined;
expect(newEmail).to.equal(payload.claims.email);
Copy link
Member

Choose a reason for hiding this comment

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

The first check seems redundant, given we also do the second comparison, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

First is checking if the update is actually called, second is checking if the new value is as expected.

Comment on lines +241 to +263
@test public async testSessionRequest_updates_existing_user() {
const payload: OIDCCreateSessionPayload = { ...this.payload };
payload.claims.sub = this.knownSubjectID; // `userServiceMock.findUserForLogin` will match this value

this.knownUser.identities = [
{
authId: payload.claims.sub,
authProviderId: payload.claims.aud,
authName: "Test User",
primaryEmail: "[email protected]",
},
];

let newEmail: string | undefined;
this.userServiceMock.updateUserIdentity = async (user, updatedIdentity) => {
newEmail = updatedIdentity.primaryEmail;
};

const result = await request(this.app.create())
.post("/session")
.set("Content-Type", "application/json")
.send(JSON.stringify(payload));

Copy link
Member

Choose a reason for hiding this comment

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

It feels this whole test is quite brittle. We're mostly testing that once you hit /session with some payload, the handler invokes the userServiceMock.updateUserIdentity method which is in fact a stub. So here, we're just checking that it invokes our stub, rather than it actually performs the behaviour we want.

Is it possible to instead use the real service and verify the record got updated in the DB?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is, if we make it a db-test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this way it makes assumptions about the internal paths of the code.

Copy link
Member

Choose a reason for hiding this comment

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

Up to you. I'd personally favour it as a db test as it gives us stronger guarantees but if you think it's pointless then I can live with that.

Comment on lines +148 to +157
protected async updateOIDCUserOnSign(user: User, payload: OIDCCreateSessionPayload) {
const recent = this.mapOIDCProfileToIdentity(payload);
const existing = user.identities.find((identity) => identity.authId === recent.authId);

// Update email
if (existing && !!recent.primaryEmail && existing.primaryEmail !== recent.primaryEmail) {
existing.primaryEmail = recent.primaryEmail;
await this.userService.updateUserIdentity(user, existing);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could this whole method be moved into the userService such that it can be more easily re-used in other places? This would be particularly useful as we try to align with the service oriented architecture.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thought about that, but opted to not push the mapping in there.

See, userService.updateUserIdentity does the actual update. On top, here is the mapping of the profiles, which very well could be pulled out to be reusable.

Copy link
Member

@geropl geropl left a comment

Choose a reason for hiding this comment

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

Tests run, code LGTM: ✔️

@AlexTugarev
Copy link
Member Author

/unhold

@roboquat roboquat merged commit 75a8fb3 into main May 22, 2023
@roboquat roboquat deleted the at/update-sso-users branch May 22, 2023 09:54
@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production size/M team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants