Skip to content

Commit a2d93a8

Browse files
committed
remove SuperTokens and OIDC user guards
1 parent 5309511 commit a2d93a8

File tree

5 files changed

+49
-80
lines changed

5 files changed

+49
-80
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { UserResolvers } from './../../../__generated__/types';
22

33
export const User: Pick<UserResolvers, 'canSwitchOrganization'> = {
4-
canSwitchOrganization: user => !user.oidcIntegrationId,
4+
canSwitchOrganization: () => true,
55
};

packages/services/api/src/modules/organization/providers/organization-manager.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,11 @@ export class OrganizationManager {
297297
user: {
298298
id: string;
299299
superTokensUserId: string | null;
300-
oidcIntegrationId: string | null;
301300
};
302301
}) {
303302
const { slug, user } = input;
304303
this.logger.info('Creating an organization (input=%o)', input);
305304

306-
if (user.oidcIntegrationId) {
307-
this.logger.debug(
308-
'Failed to create organization as oidc user is not allowed to do so (input=%o)',
309-
input,
310-
);
311-
throw new HiveError('Cannot create organization with OIDC user.');
312-
}
313-
314305
const result = await this.storage.createOrganization({
315306
slug,
316307
userId: user.id,
@@ -652,13 +643,9 @@ export class OrganizationManager {
652643
async joinOrganization({ code }: { code: string }): Promise<Organization | { message: string }> {
653644
this.logger.info('Joining an organization (code=%s)', code);
654645

655-
const user = await this.session.getViewer();
656-
const isOIDCUser = user.oidcIntegrationId !== null;
657-
658-
if (isOIDCUser) {
659-
return {
660-
message: `You cannot join an organization with an OIDC account.`,
661-
};
646+
const actor = await this.session.getActor();
647+
if (actor.type !== 'user') {
648+
throw new Error('Only users can join organizations');
662649
}
663650

664651
const organization = await this.getOrganizationByInviteCode({
@@ -674,9 +661,10 @@ export class OrganizationManager {
674661
organizationId: organization.id,
675662
});
676663

677-
if (oidcIntegration?.oidcUserAccessOnly && !isOIDCUser) {
664+
if (oidcIntegration?.oidcUserAccessOnly && actor.oidcIntegrationId !== oidcIntegration.id) {
678665
return {
679-
message: 'Non-OIDC users are not allowed to join this organization.',
666+
message:
667+
'The user is not authorized through the OIDC integration required for the organization',
680668
};
681669
}
682670
}
@@ -685,7 +673,7 @@ export class OrganizationManager {
685673

686674
await this.storage.addOrganizationMemberViaInvitationCode({
687675
code,
688-
userId: user.id,
676+
userId: actor.user.id,
689677
organizationId: organization.id,
690678
});
691679

@@ -701,7 +689,7 @@ export class OrganizationManager {
701689
eventType: 'USER_JOINED',
702690
organizationId: organization.id,
703691
metadata: {
704-
inviteeEmail: user.email,
692+
inviteeEmail: actor.user.email,
705693
},
706694
}),
707695
]);

packages/services/api/src/modules/organization/resolvers/Query/myDefaultOrganization.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Session } from '../../../auth/lib/authz';
2-
import { OIDCIntegrationsProvider } from '../../../oidc-integrations/providers/oidc-integrations.provider';
32
import { IdTranslator } from '../../../shared/providers/id-translator';
43
import { OrganizationManager } from '../../providers/organization-manager';
54
import type { QueryResolvers } from './../../../../__generated__/types';
@@ -12,27 +11,6 @@ export const myDefaultOrganization: NonNullable<QueryResolvers['myDefaultOrganiz
1211
const user = await injector.get(Session).getViewer();
1312
const organizationManager = injector.get(OrganizationManager);
1413

15-
// For an OIDC Integration User we want to return the linked organization
16-
if (user?.oidcIntegrationId) {
17-
const oidcIntegration = await injector.get(OIDCIntegrationsProvider).getOIDCIntegrationById({
18-
oidcIntegrationId: user.oidcIntegrationId,
19-
});
20-
if (oidcIntegration.type === 'ok') {
21-
const org = await organizationManager.getOrganization({
22-
organizationId: oidcIntegration.organizationId,
23-
});
24-
25-
return {
26-
selector: {
27-
organizationSlug: org.slug,
28-
},
29-
organization: org,
30-
};
31-
}
32-
33-
return null;
34-
}
35-
3614
// This is the organization that got stored as an cookie
3715
// We make sure it actually exists before directing to it.
3816
if (previouslyVisitedOrganizationSlug) {

packages/services/api/src/shared/entities.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ export interface User {
348348
provider: AuthProviderType;
349349
superTokensUserId: string | null;
350350
isAdmin: boolean;
351-
oidcIntegrationId: string | null;
352351
zendeskId: string | null;
353352
}
354353

packages/services/storage/src/index.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -444,32 +444,50 @@ export async function createStorage(
444444

445445
return UserModel.parse(record);
446446
},
447+
getUserById: batch(async (input: { id: string }[]) => {
448+
const userIds = input.map(i => i.id);
449+
const records = await pool.any<unknown>(sql`/* getUserById */
450+
SELECT
451+
${userFields(sql`"users".`, sql`"stu".`)}
452+
FROM
453+
"users"
454+
LEFT JOIN "supertokens_thirdparty_users" AS "stu"
455+
ON ("stu"."user_id" = "users"."supertoken_user_id")
456+
WHERE
457+
"users"."id" = ANY(${sql.array(userIds, 'uuid')})
458+
`);
459+
460+
const mappings = new Map<string, UserType>();
461+
for (const record of records) {
462+
const user = UserModel.parse(record);
463+
mappings.set(user.id, user);
464+
}
465+
466+
return userIds.map(async id => mappings.get(id) ?? null);
467+
}),
447468
async createUser(
448469
{
449-
superTokensUserId,
450470
email,
451471
fullName,
452472
displayName,
453-
oidcIntegrationId,
454473
}: {
455-
superTokensUserId: string;
456474
email: string;
457475
fullName: string;
458476
displayName: string;
459-
oidcIntegrationId: string | null;
460477
},
461478
connection: Connection,
462479
) {
463-
await connection.query<unknown>(
480+
const { id } = await connection.oneFirst<{ id: string }>(
464481
sql`/* createUser */
465482
INSERT INTO users
466-
("email", "supertoken_user_id", "full_name", "display_name", "oidc_integration_id")
483+
("email", "full_name", "display_name")
467484
VALUES
468-
(${email}, ${superTokensUserId}, ${fullName}, ${displayName}, ${oidcIntegrationId})
485+
(${email}, ${fullName}, ${displayName})
486+
RETURNING id
469487
`,
470488
);
471489

472-
const user = await this.getUserBySuperTokenId({ superTokensUserId }, connection);
490+
const user = await shared.getUserById({ id });
473491
if (!user) {
474492
throw new Error('Something went wrong.');
475493
}
@@ -559,9 +577,7 @@ export async function createStorage(
559577
};
560578

561579
function buildUserData(input: {
562-
superTokensUserId: string;
563580
email: string;
564-
oidcIntegrationId: string | null;
565581
firstName: string | null;
566582
lastName: string | null;
567583
}) {
@@ -572,11 +588,9 @@ export async function createStorage(
572588
: input.email.split('@')[0].slice(0, 25).padEnd(2, '1');
573589

574590
return {
575-
superTokensUserId: input.superTokensUserId,
576591
email: input.email,
577592
displayName: name,
578593
fullName: name,
579-
oidcIntegrationId: input.oidcIntegrationId,
580594
};
581595
}
582596

@@ -626,9 +640,7 @@ export async function createStorage(
626640
if (!internalUser) {
627641
internalUser = await shared.createUser(
628642
buildUserData({
629-
superTokensUserId,
630643
email,
631-
oidcIntegrationId: oidcIntegration?.id ?? null,
632644
firstName,
633645
lastName,
634646
}),
@@ -637,6 +649,16 @@ export async function createStorage(
637649
action = 'created';
638650
}
639651

652+
if (users.length === 1 && internalUser.superTokensUserId != null) {
653+
await pool.query(sql`
654+
UPDATE "users"
655+
SET
656+
"supertoken_user_id" = NULL,
657+
"oidc_integration_id" = NULL
658+
WHERE "id" = ${internalUser.id};
659+
`);
660+
}
661+
640662
if (oidcIntegration !== null) {
641663
// Add user to OIDC linked integration
642664
await shared.addOrganizationMemberViaOIDCIntegrationId(
@@ -657,27 +679,9 @@ export async function createStorage(
657679
async getUserBySuperTokenId({ superTokensUserId }) {
658680
return shared.getUserBySuperTokenId({ superTokensUserId }, pool);
659681
},
660-
getUserById: batch(async input => {
661-
const userIds = input.map(i => i.id);
662-
const records = await pool.any<unknown>(sql`/* getUserById */
663-
SELECT
664-
${userFields(sql`"users".`, sql`"stu".`)}
665-
FROM
666-
"users"
667-
LEFT JOIN "supertokens_thirdparty_users" AS "stu"
668-
ON ("stu"."user_id" = "users"."supertoken_user_id")
669-
WHERE
670-
"users"."id" = ANY(${sql.array(userIds, 'uuid')})
671-
`);
672-
673-
const mappings = new Map<string, UserType>();
674-
for (const record of records) {
675-
const user = UserModel.parse(record);
676-
mappings.set(user.id, user);
677-
}
678-
679-
return userIds.map(async id => mappings.get(id) ?? null);
680-
}),
682+
async getUserById({ id }) {
683+
return shared.getUserById({ id });
684+
},
681685
async updateUser({ id, displayName, fullName }) {
682686
await pool.one<users>(sql`/* updateUser */
683687
UPDATE "users"
@@ -5410,7 +5414,7 @@ export const UserModel = zod.object({
54105414
createdAt: zod.string(),
54115415
displayName: zod.string(),
54125416
fullName: zod.string(),
5413-
superTokensUserId: zod.string(),
5417+
superTokensUserId: zod.string().nullable(),
54145418
isAdmin: zod
54155419
.boolean()
54165420
.nullable()

0 commit comments

Comments
 (0)