From 1730ec27539e2dfaeb0b4ca81336919c25c04388 Mon Sep 17 00:00:00 2001 From: Rafael Date: Thu, 20 Aug 2026 10:05:26 -0300 Subject: [PATCH] feat(orchest-identity): permission-mapping helpers Co-Authored-By: WOZCODE --- src/modules/auth/orchest-identity.spec.ts | 33 ++++++++++++++++++++++ src/modules/auth/orchest-identity.ts | 34 +++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/modules/auth/orchest-identity.spec.ts create mode 100644 src/modules/auth/orchest-identity.ts diff --git a/src/modules/auth/orchest-identity.spec.ts b/src/modules/auth/orchest-identity.spec.ts new file mode 100644 index 0000000..f7ec418 --- /dev/null +++ b/src/modules/auth/orchest-identity.spec.ts @@ -0,0 +1,33 @@ +import { + parseAdminSeqids, + moduleGateSeqid, + isModuleAllowed, + isAdmin, +} from './orchest-identity'; + +describe('orchest-identity mapping', () => { + it('parseAdminSeqids: default, single, list, whitespace', () => { + expect(parseAdminSeqids(undefined)).toEqual([34]); + expect(parseAdminSeqids('')).toEqual([34]); + expect(parseAdminSeqids('34')).toEqual([34]); + expect(parseAdminSeqids('34,40')).toEqual([34, 40]); + expect(parseAdminSeqids(' 34 , 40 ')).toEqual([34, 40]); + }); + + it('moduleGateSeqid: default and override', () => { + expect(moduleGateSeqid(undefined)).toBe(31); + expect(moduleGateSeqid('43')).toBe(43); + }); + + it('isModuleAllowed', () => { + expect(isModuleAllowed([31, 5], 31)).toBe(true); + expect(isModuleAllowed([5, 7], 31)).toBe(false); + expect(isModuleAllowed([], 31)).toBe(false); + }); + + it('isAdmin: intersection', () => { + expect(isAdmin([31, 34], [34])).toBe(true); + expect(isAdmin([31], [34])).toBe(false); + expect(isAdmin([99], [34, 99])).toBe(true); + }); +}); diff --git a/src/modules/auth/orchest-identity.ts b/src/modules/auth/orchest-identity.ts new file mode 100644 index 0000000..cfe3248 --- /dev/null +++ b/src/modules/auth/orchest-identity.ts @@ -0,0 +1,34 @@ +/** + * Pure helpers backing `GET /auth/module-identity` (see + * dbt-to-orchest docs/superpowers/specs/2026-08-20-maestro-module-identity-design.md). + * + * Kept free of HTTP/Nest so the permission mapping and tenant-routing logic + * unit-test without a request. Authorization decisions that reach orchest-api + * live on AuthClientService; this module only maps Maestro permissions to the + * Orchest header contract and derives the tenant orchest-api host. + */ + +/** Parse ORCHEST_ADMIN_PERMISSION_SEQIDS ("34" / "34,40"); default [34]. */ +export function parseAdminSeqids(raw: string | undefined): number[] { + if (!raw || !raw.trim()) return [34]; + return raw + .split(',') + .map((s) => Number(s.trim())) + .filter((n) => Number.isInteger(n)); +} + +/** Parse ORCHEST_MODULE_PERMISSION_SEQID; default 31 (Intelligence/Orchest). */ +export function moduleGateSeqid(raw: string | undefined): number { + const n = Number(raw); + return Number.isInteger(n) && n > 0 ? n : 31; +} + +/** Whether the user's permissions include the module-access gate seqid. */ +export function isModuleAllowed(perms: number[], gateSeqid: number): boolean { + return Array.isArray(perms) && perms.includes(gateSeqid); +} + +/** Whether the user's permissions intersect the admin seqid set. */ +export function isAdmin(perms: number[], adminSeqids: number[]): boolean { + return Array.isArray(perms) && perms.some((p) => adminSeqids.includes(p)); +}