request-tenant.ts35 lines · main
| 1 | /** |
| 2 | * Resolve briven-engine tenant from request headers. |
| 3 | * |
| 4 | * Apps send `x-briven-project-id`. We map to tenantId for Multitenancy. |
| 5 | */ |
| 6 | |
| 7 | import { mapProjectToAuthCore } from './project-map.js'; |
| 8 | |
| 9 | export type ResolvedAuthTenant = { |
| 10 | projectId: string; |
| 11 | appId: string; |
| 12 | tenantId: string; |
| 13 | engine: 'briven-engine'; |
| 14 | }; |
| 15 | |
| 16 | export function resolveAuthTenantFromHeaders( |
| 17 | getHeader: (name: string) => string | undefined, |
| 18 | ): ResolvedAuthTenant | null { |
| 19 | const projectId = |
| 20 | getHeader('x-briven-project-id') ?? |
| 21 | getHeader('x-project-id') ?? |
| 22 | getHeader('briven-project-id'); |
| 23 | if (!projectId || !projectId.trim()) return null; |
| 24 | try { |
| 25 | const map = mapProjectToAuthCore(projectId.trim()); |
| 26 | return { |
| 27 | projectId: map.projectId, |
| 28 | appId: map.appId, |
| 29 | tenantId: map.tenantId, |
| 30 | engine: 'briven-engine', |
| 31 | }; |
| 32 | } catch { |
| 33 | return null; |
| 34 | } |
| 35 | } |