fix(auth): derive Orchest identity from numeric seqids (JWT carries seqids not claim strings)

The JWT `permissions` claim is an array of numeric seqids at runtime
(see authentication.guard.ts / authentication.decorator.ts), not claim
strings. deriveOrchestIdentity previously matched claim strings against
this numeric array, so roles[]/modules[] were always empty for every
real user.

- deriveOrchestIdentity now takes number[] | undefined and matches
  seqids sourced from PERMISSIONS_GROUPS (permissions.enum.ts) instead
  of hand-copied literals.
- permissions is translated back to claim strings via a full
  seqid->claim catalog built once from PERMISSIONS_GROUPS; unknown
  seqids are dropped (auth-server ignores permissions[] in v1).
- auth.controller.ts's api-key branch literal is now annotated
  `: UserDTO` so tsc enforces the three fields there.
- Both spec files re-fixtured with numeric seqid inputs, including a
  mixed admin+module case and an exact claim-string translation
  assertion.

Co-Authored-By: WOZCODE <contact@withwoz.com>
This commit is contained in:
Rafael
2026-08-24 16:03:20 -03:00
co-authored by WOZCODE
parent a16fefe691
commit a5a685ee3f
4 changed files with 68 additions and 30 deletions
+20 -12
View File
@@ -1,29 +1,37 @@
import { deriveOrchestIdentity } from './orchest-identity';
// JWT permissions claim carries numeric seqids (not claim strings) — see
// src/decorators/authentication.decorator.ts and
// src/authentication/authentication.guard.ts. 34 = users:admin,
// 31 = intelligence:open, 43 = process:open.
describe('deriveOrchestIdentity', () => {
it('passes permissions through verbatim', () => {
expect(deriveOrchestIdentity(['intelligence:open']).permissions).toEqual([
it('translates seqids to claim strings in permissions', () => {
expect(deriveOrchestIdentity([31]).permissions).toEqual([
'intelligence:open',
]);
});
it('marks super-admin from users:admin', () => {
expect(deriveOrchestIdentity(['users:admin']).roles).toContain('super-admin');
it('marks super-admin from the users:admin seqid (34)', () => {
expect(deriveOrchestIdentity([34]).roles).toContain('super-admin');
});
it('omits super-admin when users:admin absent', () => {
expect(deriveOrchestIdentity(['intelligence:open']).roles).not.toContain(
'super-admin',
);
it('omits super-admin when the admin seqid is absent', () => {
expect(deriveOrchestIdentity([31]).roles).not.toContain('super-admin');
});
it('maps intelligence:open and process:open to module keys', () => {
const { modules } = deriveOrchestIdentity(['intelligence:open', 'process:open']);
it('maps intelligence and process seqids to module keys', () => {
const { modules } = deriveOrchestIdentity([31, 43]);
expect(modules.sort()).toEqual(['intelligence', 'process']);
});
it('ignores non-module permissions in modules', () => {
expect(deriveOrchestIdentity(['users:admin']).modules).toEqual([]);
it('ignores non-module seqids in modules', () => {
expect(deriveOrchestIdentity([34]).modules).toEqual([]);
});
it('handles a mixed real-shape seqid array (admin + intelligence)', () => {
const { roles, modules } = deriveOrchestIdentity([34, 31]);
expect(roles).toEqual(['super-admin']);
expect(modules).toEqual(['intelligence']);
});
it('handles undefined/empty permissions', () => {