Skip to content

[server] try to fix org membership on login #18399

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
Aug 1, 2023
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
35 changes: 24 additions & 11 deletions components/server/src/iam/iam-session-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { UserAuthentication } from "../user/user-authentication";
import { OIDCCreateSessionPayload } from "./iam-oidc-create-session-payload";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { Identity, User } from "@gitpod/gitpod-protocol";
import { BUILTIN_INSTLLATION_ADMIN_USER_ID } from "@gitpod/gitpod-db/lib";
import { reportJWTCookieIssued } from "../prometheus-metrics";
import { ApplicationError } from "@gitpod/gitpod-protocol/lib/messaging/error";
import { OrganizationService } from "../orgs/organization-service";
Expand Down Expand Up @@ -98,17 +97,31 @@ export class IamSessionApp {
let existingUser = await this.userAuthentication.findUserForLogin({
candidate: this.mapOIDCProfileToIdentity(payload),
});
if (existingUser) {
return existingUser;
if (!existingUser) {
// Organizational account lookup by email address
existingUser = await this.userAuthentication.findOrgOwnedUser({
organizationId: payload.organizationId,
email: payload.claims.email,
});
if (existingUser) {
log.info("Found Org-owned user by email.", { email: payload?.claims?.email });
}
}

// Organizational account lookup by email address
existingUser = await this.userAuthentication.findOrgOwnedUser({
organizationId: payload.organizationId,
email: payload.claims.email,
});
if (existingUser) {
log.info("Found Org-owned user by email.", { email: payload?.claims?.email });
if (existingUser?.organizationId) {
const members = await this.orgService.listMembers(existingUser.id, existingUser.organizationId);
if (!members.some((m) => m.userId === existingUser?.id)) {
// In case `createNewOIDCUser` failed to create a membership for this user,
// let's try to fix the situation on the fly.
// Also, if that step repeatedly fails, it would fail the login process earlier but
// in a more consistent state.
await this.orgService.addOrUpdateMember(
undefined,
existingUser.organizationId,
existingUser.id,
"member",
);
}
}

return existingUser;
Expand Down Expand Up @@ -144,7 +157,7 @@ export class IamSessionApp {
},
});

await this.orgService.addOrUpdateMember(BUILTIN_INSTLLATION_ADMIN_USER_ID, organizationId, user.id, "member");
await this.orgService.addOrUpdateMember(undefined, organizationId, user.id, "member");
return user;
}
}
6 changes: 4 additions & 2 deletions components/server/src/orgs/organization-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ export class OrganizationService {
}

public async addOrUpdateMember(
userId: string,
userId: string | undefined, // undefined means it is a system call, not a user call
orgId: string,
memberId: string,
role: OrgMemberRole,
): Promise<void> {
await this.auth.checkPermissionOnOrganization(userId, "write_members", orgId);
if (userId) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@svenefftinge, now thinking about that change. This could actually be the fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Just curious, why this is not happening on dogfood?

Copy link
Member

Choose a reason for hiding this comment

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

The featureflag is disabled. So these checks don't throw

await this.auth.checkPermissionOnOrganization(userId, "write_members", orgId);
}
if (role !== "owner") {
const members = await this.teamDB.findMembersByTeam(orgId);
if (!members.some((m) => m.userId !== memberId && m.role === "owner")) {
Expand Down