Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
refactor(control-api): make the member-registration signer pure (explicit credential)
Jose Ramirez66b33c60e445
diff --git a/control-api/src/utils/auth/memberRegistrationSigner.ts b/control-api/src/utils/auth/memberRegistrationSigner.tsindex ca7ff14c3..7ca344066 100644--- a/control-api/src/utils/auth/memberRegistrationSigner.ts+++ b/control-api/src/utils/auth/memberRegistrationSigner.ts@@ -1,36 +1,39 @@import { createHmac, randomUUID } from 'node:crypto'-import { config } from '../../config.js'-// Signs the per-tenant HMAC JWT that control-api presents to the centralized-// member-registration service (on example-hub, example.com) for-// its three machine-to-machine routes. Mirrors the InternalControl-JWT-// convention used by WRC/HCC; the member-registration service verifies the-// signature against this tenant's secret (resolved by `kid`) and binds the-// request to `sub` = tenantId.+// Signs the per-tenant HMAC JWT that control-api presents to the member-+// registration service. Pure: the credential is supplied by the caller —+// remote mode passes the injected env credential, hosted mode passes the+// self-enrolled row (spec §8.5). Token shape is unchanged either way.const AUDIENCE = 'member-registration-service'const ISSUER = 'control-api'const TTL_SECONDS = 60+export interface MemberRegistrationSigningCredential {+ secret: string+ kid: string+ tenantId: string+}+function base64UrlJson(value: unknown): string {return Buffer.from(JSON.stringify(value)).toString('base64url')}-export function signMemberRegistrationJwt(now: Date = new Date()): string {- const secret = config.memberRegistrationServiceHmacSecret- const kid = config.memberRegistrationServiceHmacKid- const tenantId = config.memberRegistrationTenantId+export function signMemberRegistrationJwt(+ credential: MemberRegistrationSigningCredential,+ now: Date = new Date()+): string {const iat = Math.floor(now.getTime() / 1000)- const header = base64UrlJson({ alg: 'HS256', typ: 'JWT', kid })+ const header = base64UrlJson({ alg: 'HS256', typ: 'JWT', kid: credential.kid })const payload = base64UrlJson({iss: ISSUER,aud: AUDIENCE,- sub: tenantId,+ sub: credential.tenantId,iat,exp: iat + TTL_SECONDS,jti: randomUUID(),})const signingInput = `${header}.${payload}`- const signature = createHmac('sha256', secret).update(signingInput).digest('base64url')+ const signature = createHmac('sha256', credential.secret).update(signingInput).digest('base64url')return `${signingInput}.${signature}`}diff --git a/control-api/test/memberRegistrationSigner.test.ts b/control-api/test/memberRegistrationSigner.test.tsindex 3717d976a..713cdbe21 100644--- a/control-api/test/memberRegistrationSigner.test.ts+++ b/control-api/test/memberRegistrationSigner.test.ts@@ -1,38 +1,39 @@import { describe, expect, it } from 'vitest'import crypto from 'node:crypto'-import { config } from '../src/config.js'import { signMemberRegistrationJwt } from '../src/utils/auth/memberRegistrationSigner.js'+const CREDENTIAL = {+ secret: 'test-hmac-secret',+ kid: 'ext-abc123-deadbeef',+ tenantId: 'ext-abc123',+}+describe('signMemberRegistrationJwt', () => {- it('produces a verifiable HS256 token with the expected header + claims', () => {- const token = signMemberRegistrationJwt(new Date(1_700_000_000_000))+ it('produces a verifiable HS256 token from the supplied credential', () => {+ const token = signMemberRegistrationJwt(CREDENTIAL, new Date(1_700_000_000_000))const [h, p, sig] = token.split('.')const expected = crypto- .createHmac('sha256', config.memberRegistrationServiceHmacSecret)+ .createHmac('sha256', CREDENTIAL.secret).update(`${h}.${p}`).digest('base64url')expect(sig).toBe(expected)const header = JSON.parse(Buffer.from(h, 'base64url').toString())const payload = JSON.parse(Buffer.from(p, 'base64url').toString())- expect(header).toMatchObject({- alg: 'HS256',- typ: 'JWT',- kid: config.memberRegistrationServiceHmacKid,- })+ expect(header).toMatchObject({ alg: 'HS256', typ: 'JWT', kid: CREDENTIAL.kid })expect(payload).toMatchObject({iss: 'control-api',aud: 'member-registration-service',- sub: config.memberRegistrationTenantId,+ sub: CREDENTIAL.tenantId,})expect(payload.exp - payload.iat).toBe(60)expect(typeof payload.jti).toBe('string')})it('mints a fresh jti each call', () => {- const a = signMemberRegistrationJwt()- const b = signMemberRegistrationJwt()+ const a = signMemberRegistrationJwt(CREDENTIAL)+ const b = signMemberRegistrationJwt(CREDENTIAL)const jtiA = JSON.parse(Buffer.from(a.split('.')[1], 'base64url').toString()).jticonst jtiB = JSON.parse(Buffer.from(b.split('.')[1], 'base64url').toString()).jtiexpect(jtiA).not.toBe(jtiB)