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

SSO adaptions #161

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion functions/src/functions/blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const beforeUserCreatedFunction = beforeUserCreated(
'Organization does not match invitation code.',
)

await userService.enrollUser(invitation, userId)
await userService.enrollUser(invitation, userId, { isSingleSignOn: true })
await factory.trigger().userEnrolled(userId)
},
)
Expand Down
2 changes: 1 addition & 1 deletion functions/src/functions/enrollUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const enrollUser = validatedOnCall(
if (invitation === undefined)
throw new https.HttpsError('not-found', 'Invitation not found')

await userService.enrollUser(invitation, userId)
await userService.enrollUser(invitation, userId, { isSingleSignOn: false })

logger.debug(
`setupUser: User '${userId}' successfully enrolled in the study with invitation code: ${invitationCode}`,
Expand Down
12 changes: 9 additions & 3 deletions functions/src/services/user/databaseUserService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ describe('DatabaseUserService', () => {

const invitation = await userService.getInvitationByCode(invitationCode)
if (!invitation) assert.fail('Invitation not found')
await userService.enrollUser(invitation, userId)
await userService.enrollUser(invitation, userId, {
isSingleSignOn: false,
})

const auth = await admin.auth().getUser(userId)
expect(auth.displayName).to.equal(displayName)
Expand Down Expand Up @@ -96,7 +98,9 @@ describe('DatabaseUserService', () => {

const invitation = await userService.getInvitationByCode(invitationCode)
if (!invitation) assert.fail('Invitation not found')
await userService.enrollUser(invitation, userId)
await userService.enrollUser(invitation, userId, {
isSingleSignOn: false,
})

const auth = await admin.auth().getUser(userId)
expect(auth.displayName).to.equal(displayName)
Expand Down Expand Up @@ -141,7 +145,9 @@ describe('DatabaseUserService', () => {

const invitation = await userService.getInvitationByCode(invitationCode)
if (!invitation) assert.fail('Invitation not found')
await userService.enrollUser(invitation, userId)
await userService.enrollUser(invitation, userId, {
isSingleSignOn: false,
})

const auth = await admin.auth().getUser(userId)
expect(auth.displayName).to.equal(displayName)
Expand Down
19 changes: 12 additions & 7 deletions functions/src/services/user/databaseUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class DatabaseUserService implements UserService {
async enrollUser(
invitation: Document<Invitation>,
userId: string,
options: { isSingleSignOn: boolean },
): Promise<void> {
logger.info(
`About to enroll user ${userId} using invitation at '${invitation.id}' with code '${invitation.content.code}'.`,
Expand All @@ -150,12 +151,14 @@ export class DatabaseUserService implements UserService {
)
}

await this.auth.updateUser(userId, {
displayName: invitation.content.auth?.displayName ?? undefined,
email: invitation.content.auth?.email ?? undefined,
phoneNumber: invitation.content.auth?.phoneNumber ?? undefined,
photoURL: invitation.content.auth?.photoURL ?? undefined,
})
if (!options.isSingleSignOn) {
await this.auth.updateUser(userId, {
displayName: invitation.content.auth?.displayName ?? undefined,
email: invitation.content.auth?.email ?? undefined,
phoneNumber: invitation.content.auth?.phoneNumber ?? undefined,
photoURL: invitation.content.auth?.photoURL ?? undefined,
})
}

logger.info(
`Updated auth information for user with id '${userId}' using invitation auth content.`,
Expand Down Expand Up @@ -214,7 +217,9 @@ export class DatabaseUserService implements UserService {
)
})

await this.updateClaims(userId)
if (!options.isSingleSignOn) {
await this.updateClaims(userId)
}
}

async deleteInvitation(invitation: Document<Invitation>): Promise<void> {
Expand Down
6 changes: 5 additions & 1 deletion functions/src/services/user/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export interface UserService {
getInvitationByCode(
invitationCode: string,
): Promise<Document<Invitation> | undefined>
enrollUser(invitation: Document<Invitation>, userId: string): Promise<void>
enrollUser(
invitation: Document<Invitation>,
userId: string,
options: { isSingleSignOn: boolean },
): Promise<void>
deleteInvitation(invitation: Document<Invitation>): Promise<void>

// Organizations
Expand Down
Loading