feat(orchest-identity): permission-mapping helpers

Co-Authored-By: WOZCODE <contact@withwoz.com>
This commit is contained in:
Rafael
2026-08-20 10:05:26 -03:00
co-authored by WOZCODE
parent dc4609cbc5
commit 1730ec2753
2 changed files with 67 additions and 0 deletions
+33
View File
@@ -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);
});
});
+34
View File
@@ -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));
}