11import { __unsafePrisma } from "@/prisma" ;
22import { hasEntitlement } from "@/lib/entitlements" ;
3- import { SINGLE_TENANT_ORG_ID } from "@/lib/constants" ;
43import { hashSecret , SCIM_TOKEN_PREFIX , createLogger } from "@sourcebot/shared" ;
54import { Org , PrismaClient } from "@sourcebot/db" ;
65import { NextRequest } from "next/server" ;
@@ -10,8 +9,6 @@ const logger = createLogger('scim-auth');
109
1110export type ScimAuthContext = {
1211 org : Org ;
13- // SCIM acts on behalf of the IdP integration for the whole org — there is
14- // no user, so we use the unscoped client rather than the user-scoped one.
1512 prisma : PrismaClient ;
1613} ;
1714
@@ -43,6 +40,7 @@ export const withScimAuth = async (
4340
4441 const scimToken = await __unsafePrisma . scimToken . findUnique ( {
4542 where : { hash : hashSecret ( secret ) } ,
43+ include : { org : true } ,
4644 } ) ;
4745 if ( ! scimToken ) {
4846 return scimError ( 401 , "Invalid SCIM token" ) ;
@@ -54,12 +52,6 @@ export const withScimAuth = async (
5452 return scimError ( 403 , "SCIM provisioning is not available in your current plan" ) ;
5553 }
5654
57- const org = await __unsafePrisma . org . findUnique ( {
58- where : { id : SINGLE_TENANT_ORG_ID } ,
59- } ) ;
60- if ( ! org ) {
61- return scimError ( 500 , "Organization not found" ) ;
62- }
6355
6456 // Best-effort usage tracking; never block the request on it.
6557 __unsafePrisma . scimToken . update ( {
@@ -68,7 +60,10 @@ export const withScimAuth = async (
6860 } ) . catch ( ( ) => { /* ignore */ } ) ;
6961
7062 try {
71- return await fn ( { org, prisma : __unsafePrisma } ) ;
63+ return await fn ( {
64+ org : scimToken . org ,
65+ prisma : __unsafePrisma
66+ } ) ;
7267 } catch ( error ) {
7368 logger . error ( `Unhandled SCIM error: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
7469 return scimError ( 500 , "Internal server error" ) ;
0 commit comments