Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(control-api): per-destination credential resolution in the member-registration client

Jose Ramirez493d049807af
diff --git a/control-api/src/memberRegistrationServiceClient.ts b/control-api/src/memberRegistrationServiceClient.ts
index 002308ec3..bd4a625b0 100644
--- a/control-api/src/memberRegistrationServiceClient.ts
+++ b/control-api/src/memberRegistrationServiceClient.ts
@@ -1,28 +1,67 @@
import { config } from './config.js'
-import { signMemberRegistrationJwt } from './utils/auth/memberRegistrationSigner.js'
+import {
+ signMemberRegistrationJwt,
+ type MemberRegistrationSigningCredential,
+} from './utils/auth/memberRegistrationSigner.js'
+import {
+ ensureEnrollment,
+ normalizeEnrollmentHost,
+} from './services/memberRegistrationEnrollment.js'
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
-function buildUrl(path: string): string {
- const base = config.memberRegistrationServiceBaseUrl.replace(/\/+$/, '')
+function buildUrl(baseUrl: string, path: string): string {
+ const base = baseUrl.replace(/\/+$/, '')
const normalizedPath = path.startsWith('/') ? path : `/${path}`
return `${base}${normalizedPath}`
}
+// The single mode seam (spec §8.5): remote signs with the injected env
+// credential against the configured service; hosted resolves the credential
+// bound to the DESTINATION host (the hub 403s on any mismatch) and calls the
+// external hub. destinationBaseUrl is required so no call site can silently
+// sign with the wrong credential.
+async function resolveTarget(destinationBaseUrl: string): Promise<{
+ baseUrl: string
+ credential: MemberRegistrationSigningCredential
+}> {
+ if (config.memberRegistrationMode === 'hosted') {
+ const credential = await ensureEnrollment(normalizeEnrollmentHost(destinationBaseUrl))
+ return {
+ baseUrl: config.memberRegistrationExternalHubBaseUrl,
+ credential: {
+ secret: credential.secret,
+ kid: credential.kid,
+ tenantId: credential.tenantId,
+ },
+ }
+ }
+ return {
+ baseUrl: config.memberRegistrationServiceBaseUrl,
+ credential: {
+ secret: config.memberRegistrationServiceHmacSecret,
+ kid: config.memberRegistrationServiceHmacKid,
+ tenantId: config.memberRegistrationTenantId,
+ },
+ }
+}
+
export async function memberRegistrationServiceRequest<T>(
method: RequestMethod,
path: string,
- options?: {
+ options: {
body?: unknown
+ destinationBaseUrl: string
}
): Promise<T> {
- const response = await fetch(buildUrl(path), {
+ const { baseUrl, credential } = await resolveTarget(options.destinationBaseUrl)
+ const response = await fetch(buildUrl(baseUrl, path), {
method,
headers: {
'content-type': 'application/json',
- authorization: `Bearer ${signMemberRegistrationJwt()}`,
+ authorization: `Bearer ${signMemberRegistrationJwt(credential)}`,
},
- body: options?.body !== undefined ? JSON.stringify(options.body) : undefined,
+ body: options.body !== undefined ? JSON.stringify(options.body) : undefined,
})
const raw = await response.text()
diff --git a/control-api/src/services/controlAdminInvitationRegistrationService.ts b/control-api/src/services/controlAdminInvitationRegistrationService.ts
index 1c49057e5..31b18c0f3 100644
--- a/control-api/src/services/controlAdminInvitationRegistrationService.ts
+++ b/control-api/src/services/controlAdminInvitationRegistrationService.ts
@@ -12,6 +12,7 @@ export async function registerAndSendControlAdminInvitation(
'POST',
'/control-admin-invitations/invitations',
{
+ destinationBaseUrl: config.controlUiBaseUrl,
body: {
email,
invitationUuid,
@@ -34,6 +35,7 @@ export async function validateControlAdminInvitationToken(
email: string
invitationUuid: string
}>('POST', '/control-admin-invitations/validate', {
+ destinationBaseUrl: config.controlUiBaseUrl,
body: {
token,
email,
@@ -55,6 +57,7 @@ export async function registerAndSendControlAdminEmailConfirmation(
'POST',
'/control-admin-email-confirmations/confirmations',
{
+ destinationBaseUrl: config.controlUiBaseUrl,
body: {
email,
confirmationUuid,
@@ -76,6 +79,7 @@ export async function validateControlAdminEmailConfirmationToken(
email: string
confirmationUuid: string
}>('POST', '/control-admin-email-confirmations/validate', {
+ destinationBaseUrl: config.controlUiBaseUrl,
body: {
token,
email,
@@ -97,6 +101,7 @@ export async function registerAndSendControlAdminPasswordReset(
'POST',
'/control-admin-password-resets/resets',
{
+ destinationBaseUrl: config.controlUiBaseUrl,
body: {
email,
resetUuid,
@@ -118,6 +123,7 @@ export async function validateControlAdminPasswordResetToken(
email: string
resetUuid: string
}>('POST', '/control-admin-password-resets/validate', {
+ destinationBaseUrl: config.controlUiBaseUrl,
body: {
token,
email,
diff --git a/control-api/src/services/invitationFlowRegistrationService.ts b/control-api/src/services/invitationFlowRegistrationService.ts
index 06bd9fffe..61f81768d 100644
--- a/control-api/src/services/invitationFlowRegistrationService.ts
+++ b/control-api/src/services/invitationFlowRegistrationService.ts
@@ -16,6 +16,7 @@ export async function registerAndSendInvitation(
'POST',
'/invitations-flow/invitations',
{
+ destinationBaseUrl: config.desktopProfileUiBaseUrl,
body: {
email,
invitationUuid,
@@ -42,6 +43,7 @@ export async function validateInvitationFlowToken(
email: string
invitationUuid: string
}>('POST', '/invitations-flow/validate', {
+ destinationBaseUrl: config.desktopProfileUiBaseUrl,
body: {
token,
email,
@@ -61,6 +63,7 @@ export async function storeDesktopAuthorizationToken(
'POST',
'/invitations-flow/desktop-authorizations',
{
+ destinationBaseUrl: config.desktopProfileUiBaseUrl,
body: {
email,
authorizationToken,
diff --git a/control-api/test/memberRegistrationClient.resolution.test.ts b/control-api/test/memberRegistrationClient.resolution.test.ts
new file mode 100644
index 000000000..b247eb531
--- /dev/null
+++ b/control-api/test/memberRegistrationClient.resolution.test.ts
@@ -0,0 +1,143 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import crypto from 'node:crypto'
+
+const { cfg } = vi.hoisted(() => ({
+ cfg: {
+ memberRegistrationMode: 'hosted',
+ memberRegistrationExternalHubBaseUrl: 'https://registration.evenfire.ai/api/v1',
+ memberRegistrationServiceBaseUrl: '',
+ memberRegistrationServiceHmacSecret: '',
+ memberRegistrationServiceHmacKid: 'example-dev',
+ memberRegistrationTenantId: 'example-dev',
+ desktopProfileUiBaseUrl: 'https://profile.acme.com',
+ controlUiBaseUrl: 'https://control.acme.com',
+ desktopExternalRestApiBaseUrl: 'http://127.0.0.1:8091',
+ desktopRpcProxyBaseUrl: 'http://127.0.0.1:8094',
+ desktopAppName: 'Evenfire',
+ controlUiAppName: 'Evenfire',
+ } as Record<string, unknown>,
+}))
+vi.mock('../src/config.js', () => ({ config: cfg }))
+
+// Per-domain credentials so a transposed wrapper→host mapping is DETECTABLE
+// (spec §8.8 directional assertion).
+const CREDS: Record<string, { boundDomain: string; tenantId: string; kid: string; secret: string }> = {
+ 'profile.acme.com': {
+ boundDomain: 'profile.acme.com',
+ tenantId: 'ext-profile',
+ kid: 'kid-profile',
+ secret: 'secret-profile',
+ },
+ 'control.acme.com': {
+ boundDomain: 'control.acme.com',
+ tenantId: 'ext-control',
+ kid: 'kid-control',
+ secret: 'secret-control',
+ },
+}
+const enrollment = vi.hoisted(() => ({
+ ensureEnrollment: vi.fn(),
+ normalizeEnrollmentHost: (baseUrl: string) => new URL(baseUrl).hostname.toLowerCase(),
+}))
+vi.mock('../src/services/memberRegistrationEnrollment.js', () => enrollment)
+
+import { registerAndSendInvitation, validateInvitationFlowToken } from '../src/services/invitationFlowRegistrationService.js'
+import {
+ registerAndSendControlAdminInvitation,
+ validateControlAdminInvitationToken,
+} from '../src/services/controlAdminInvitationRegistrationService.js'
+
+function decodeAuthKid(fetchMock: ReturnType<typeof vi.fn>, callIndex: number) {
+ const [, init] = fetchMock.mock.calls[callIndex]
+ const token = String(init.headers.authorization).replace(/^Bearer /, '')
+ const [h, p, sig] = token.split('.')
+ const header = JSON.parse(Buffer.from(h, 'base64url').toString())
+ return { header, signingInput: `${h}.${p}`, sig }
+}
+
+function okJson(body: unknown): Response {
+ return new Response(JSON.stringify(body), {
+ status: 200,
+ headers: { 'content-type': 'application/json' },
+ })
+}
+
+describe('member-registration client per-host resolution', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ vi.unstubAllGlobals()
+ cfg.memberRegistrationMode = 'hosted'
+ enrollment.ensureEnrollment.mockImplementation(async (domain: string) => {
+ const cred = CREDS[domain]
+ if (!cred) throw new Error(`unexpected domain ${domain}`)
+ return cred
+ })
+ })
+
+ it('member SEND and VALIDATE sign with the profile-ui host credential', async () => {
+ const fetchMock = vi
+ .fn()
+ .mockResolvedValueOnce(okJson({ sent: true, registered: true }))
+ .mockResolvedValueOnce(okJson({ valid: true, email: '[email protected]', invitationUuid: 'u' }))
+ vi.stubGlobal('fetch', fetchMock)
+
+ await registerAndSendInvitation('[email protected]', 'uuid-1', 'team', '2026-01-01', '2026-02-01')
+ await validateInvitationFlowToken('tok', '[email protected]')
+
+ for (const i of [0, 1]) {
+ const { header, signingInput, sig } = decodeAuthKid(fetchMock, i)
+ expect(header.kid).toBe('kid-profile')
+ const expected = crypto
+ .createHmac('sha256', 'secret-profile')
+ .update(signingInput)
+ .digest('base64url')
+ expect(sig).toBe(expected)
+ }
+ // hosted mode routes through the hub base URL
+ expect(String(fetchMock.mock.calls[0][0])).toContain(
+ 'https://registration.evenfire.ai/api/v1/invitations-flow/invitations'
+ )
+ })
+
+ it('admin SEND and VALIDATE sign with the control-ui host credential', async () => {
+ const fetchMock = vi
+ .fn()
+ .mockResolvedValueOnce(okJson({ sent: true, registered: true }))
+ .mockResolvedValueOnce(okJson({ valid: true, email: '[email protected]', invitationUuid: 'u' }))
+ vi.stubGlobal('fetch', fetchMock)
+
+ await registerAndSendControlAdminInvitation('[email protected]', 'uuid-2', '2026-01-01', '2026-02-01')
+ await validateControlAdminInvitationToken('tok', '[email protected]')
+
+ for (const i of [0, 1]) {
+ const { header, signingInput, sig } = decodeAuthKid(fetchMock, i)
+ expect(header.kid).toBe('kid-control')
+ const expected = crypto
+ .createHmac('sha256', 'secret-control')
+ .update(signingInput)
+ .digest('base64url')
+ expect(sig).toBe(expected)
+ }
+ })
+
+ it('remote mode signs with the env credential, never touches enrollment or /public/tenants', async () => {
+ cfg.memberRegistrationMode = 'remote'
+ cfg.memberRegistrationServiceBaseUrl = 'http://member-registration-service.local:8092/api/v1'
+ cfg.memberRegistrationServiceHmacSecret = 'env-secret'
+ cfg.memberRegistrationServiceHmacKid = 'env-kid'
+ cfg.memberRegistrationTenantId = 'clerum-dev'
+ const fetchMock = vi.fn().mockResolvedValue(okJson({ sent: true, registered: true }))
+ vi.stubGlobal('fetch', fetchMock)
+
+ await registerAndSendInvitation('[email protected]', 'uuid-3', 'team', '2026-01-01', '2026-02-01')
+
+ expect(enrollment.ensureEnrollment).not.toHaveBeenCalled()
+ const [url] = fetchMock.mock.calls[0]
+ expect(String(url)).toBe(
+ 'http://member-registration-service.local:8092/api/v1/invitations-flow/invitations'
+ )
+ expect(String(url)).not.toContain('/public/tenants')
+ const { header } = decodeAuthKid(fetchMock, 0)
+ expect(header.kid).toBe('env-kid')
+ })
+})