From afb818858c8b99633df2dfa7d02cc1e3ec60a578 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Mon, 9 Nov 2026 11:52:43 +1100 Subject: [PATCH 1/2] Prepare dev Identity RS256 verification and issuer policies --- deploy/dev-jwt-issuer-policies.json | 47 +++++++++++++++++++++++++++++ docs/identity-rs256-dev.md | 11 +++++++ package.json | 2 +- pnpm-lock.yaml | 12 ++++---- src/auth.ts | 10 +++--- src/config.ts | 10 ++++-- test/auth-rs256.spec.ts | 8 ++--- test/auth.spec.ts | 20 ++++++++++-- test/fixtures.ts | 10 +++++- test/policies.spec.ts | 3 +- 10 files changed, 111 insertions(+), 22 deletions(-) create mode 100644 deploy/dev-jwt-issuer-policies.json create mode 100644 docs/identity-rs256-dev.md diff --git a/deploy/dev-jwt-issuer-policies.json b/deploy/dev-jwt-issuer-policies.json new file mode 100644 index 0000000..7da612a --- /dev/null +++ b/deploy/dev-jwt-issuer-policies.json @@ -0,0 +1,47 @@ +[ + { + "issuer": "https://api.topcoder-dev.com", + "algorithm": "RS256", + "audience": "https://api.topcoder-dev.com/v6", + "jwksUri": "https://api.topcoder-dev.com/v6/authorizations/.well-known/jwks.json" + }, + { + "issuer": "https://auth.topcoder-dev.com/", + "algorithm": "RS256", + "audience": "BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH", + "jwksUri": "https://auth.topcoder-dev.com/.well-known/jwks.json" + }, + { + "issuer": "https://topcoder-dev.auth0.com/", + "algorithm": "RS256", + "audience": [ + "BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH", + "https://m2m.topcoder-dev.com/" + ], + "jwksUri": "https://topcoder-dev.auth0.com/.well-known/jwks.json" + }, + { + "issuer": "https://keycloak.topcoder-dev.com/realms/topcoder-m2m-dev", + "algorithm": "RS256", + "audience": "https://m2m.topcoder-dev.com/", + "jwksUri": "https://keycloak.topcoder-dev.com/realms/topcoder-m2m-dev/protocol/openid-connect/certs" + }, + { + "issuer": "https://testsachin.topcoder-dev.com/", + "algorithm": "RS256", + "audience": [ + "BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH", + "https://m2m.topcoder-dev.com/" + ], + "jwksUri": "https://testsachin.topcoder-dev.com/.well-known/jwks.json" + }, + { + "issuer": "https://test-sachin-rs256.auth0.com/", + "algorithm": "RS256", + "audience": [ + "BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH", + "https://m2m.topcoder-dev.com/" + ], + "jwksUri": "https://test-sachin-rs256.auth0.com/.well-known/jwks.json" + } +] diff --git a/docs/identity-rs256-dev.md b/docs/identity-rs256-dev.md new file mode 100644 index 0000000..121d58e --- /dev/null +++ b/docs/identity-rs256-dev.md @@ -0,0 +1,11 @@ +# Identity RS256 preparation for dev + +This PR pins tc-core-library-js 4.0.0-rc.4 at commit `c7c8fab502b7152dd06b1ce46cdb703e0e13b357`. It requires explicit issuer policies and retains verified-email and identity-denylist enforcement. + +The public settings are staged in dev Parameter Store under `/config/identity-rs256-dev-staged/`; running services do not reference that namespace. The checked-in JSON is a reviewed configuration proposal, not an automatically applied deployment change. + +Before merging/deploying, coordinate with Identity and all token consumers. Set `JWT_ISSUER_POLICIES` to the JSON in `deploy/dev-jwt-issuer-policies.json`, retaining the service's machine audience and authorization checks. Merely adding `AUTH_SECRET` no longer enables HMAC trust. If a transition needs HS256, configure only that exact issuer with a finite UTC retirement deadline, then replace it with RS256 at the coordinated cutover. Never accept both algorithms for one issuer. + +Publish the Identity public key first; then test fresh login, expiry/refresh, logout, cross-app access, human authorization, machine authorization, wrong-audience rejection and legacy-HMAC rejection. Remove AUTH_SECRET only from public-key-only API verifiers; keep separate keys for application proofs, cursors, and independent local-login tokens. Existing browser sessions continue to use Auth0; do not put Identity-issued tokens into the Accounts connector cookie without implementing an Identity-aware refresh flow. + +Production is not changed. This dev policy intentionally does not trust the production Topcoder issuer. Do not merge this into production without its separately reviewed environment policy and key. diff --git a/package.json b/package.json index ce1a114..0d7a6f3 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "pg": "8.23.0", "reflect-metadata": "0.2.2", "rxjs": "7.8.2", - "tc-core-library-js": "https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/58de37670180e183e8378c3d203df0112a0414f6" + "tc-core-library-js": "https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c7c8fab502b7152dd06b1ce46cdb703e0e13b357" }, "devDependencies": { "@eslint/js": "9.39.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee60c90..9b43f16 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -82,8 +82,8 @@ importers: specifier: 7.8.2 version: 7.8.2 tc-core-library-js: - specifier: https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/58de37670180e183e8378c3d203df0112a0414f6 - version: https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/58de37670180e183e8378c3d203df0112a0414f6(supports-color@7.2.0) + specifier: https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c7c8fab502b7152dd06b1ce46cdb703e0e13b357 + version: https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c7c8fab502b7152dd06b1ce46cdb703e0e13b357(supports-color@7.2.0) devDependencies: '@eslint/js': specifier: 9.39.2 @@ -2647,9 +2647,9 @@ packages: swagger-ui-dist@5.32.8: resolution: {integrity: sha512-dgMdWXIgnI4zX4OPhKEdWnlDODbgm8W3AX0Ivn/BBqcUh6xZsBxhZMnvk6DJyRz1BTrj8dPxtarmEGgkz30oyA==} - tc-core-library-js@https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/58de37670180e183e8378c3d203df0112a0414f6: - resolution: {tarball: https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/58de37670180e183e8378c3d203df0112a0414f6} - version: 4.0.0-rc.2 + tc-core-library-js@https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c7c8fab502b7152dd06b1ce46cdb703e0e13b357: + resolution: {tarball: https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c7c8fab502b7152dd06b1ce46cdb703e0e13b357} + version: 4.0.0-rc.4 engines: {node: '>=22.13'} thread-stream@3.2.0: @@ -5452,7 +5452,7 @@ snapshots: dependencies: '@scarf/scarf': 1.4.0 - tc-core-library-js@https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/58de37670180e183e8378c3d203df0112a0414f6(supports-color@7.2.0): + tc-core-library-js@https://codeload.github.com/topcoder-platform/tc-core-library-js/tar.gz/c7c8fab502b7152dd06b1ce46cdb703e0e13b357(supports-color@7.2.0): dependencies: '@types/jsonwebtoken': 9.0.10 jsonwebtoken: 9.0.3 diff --git a/src/auth.ts b/src/auth.ts index 745bac8..9b08937 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -10,7 +10,10 @@ import { import { Reflector } from '@nestjs/core'; import type { Request } from 'express'; import jwt from 'jsonwebtoken'; -import { createJwtAuthenticator, type AuthenticatorRequest } from 'tc-core-library-js'; +import { + createJwtAuthenticator, + type AuthenticatorRequest, +} from 'tc-core-library-js'; import { CONFIG, type AppConfig } from './config'; export type Permission = 'public' | 'manage' | 'report'; @@ -34,9 +37,7 @@ export interface ActorRequest extends Request { /** Validates bearer JWTs and applies the v6 human-role / M2M-scope permission split. */ @Injectable() export class AuthGuard implements CanActivate { - private readonly authenticator: ReturnType< - typeof createJwtAuthenticator - >; + private readonly authenticator: ReturnType; /** * Injects configuration and route metadata for authentication. @@ -48,6 +49,7 @@ export class AuthGuard implements CanActivate { private readonly reflector: Reflector, ) { this.authenticator = createJwtAuthenticator({ + JWT_ISSUER_POLICIES: config.jwtIssuerPolicies, AUTH0_AUDIENCE: config.machineAudience, JWT_AUDIENCE: config.jwtAudience, AUTH_SECRET: config.authSecret, diff --git a/src/config.ts b/src/config.ts index bb47058..321d638 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,7 +7,8 @@ export interface AppConfig { busApi?: BusApiConfiguration; port: number; origins: string[]; - authSecret: string; + authSecret?: string; + jwtIssuerPolicies?: string; jwtAudience?: string; machineAudience?: string; issuers: string[]; @@ -33,8 +34,10 @@ export function readConfig(env: NodeJS.ProcessEnv = process.env): AppConfig { throw new Error('DATABASE_URL must be PostgreSQL using the forms schema.'); } database.searchParams.set('schema', 'forms'); - const authSecret = required(env, 'AUTH_SECRET'); - if (authSecret.length < 32) + // Public-key issuer policies do not require a shared signing secret. + // The verifier still rejects an HS256 policy without a suitable secret. + const authSecret = env.AUTH_SECRET?.trim() || undefined; + if (authSecret && authSecret.length < 32) throw new Error('AUTH_SECRET must contain at least 32 characters.'); const origins = (env.CORS_ORIGINS ?? '') .split(',') @@ -51,6 +54,7 @@ export function readConfig(env: NodeJS.ProcessEnv = process.env): AppConfig { port: integerSetting(env.PORT ?? '3000', 1, 65535), origins, authSecret, + jwtIssuerPolicies: env.JWT_ISSUER_POLICIES, jwtAudience: env.JWT_AUDIENCE, machineAudience: env.AUTH0_AUDIENCE, issuers, diff --git a/test/auth-rs256.spec.ts b/test/auth-rs256.spec.ts index 1fa27b0..816b5ac 100644 --- a/test/auth-rs256.spec.ts +++ b/test/auth-rs256.spec.ts @@ -37,6 +37,7 @@ function browserToken( iss: issuer, aud: 'BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH', sub: 'auth0|12345', + emailVerified: true, iat: now, exp: now + 300, ...overrides, @@ -75,7 +76,6 @@ beforeAll(async () => { provide: CONFIG, useValue: readConfig({ DATABASE_URL: 'postgresql://localhost/forms', - AUTH_SECRET: testSecret, VALID_ISSUERS: JSON.stringify([ issuer, 'https://api.topcoder-dev.com', @@ -112,9 +112,9 @@ describe('reports directory with the shared Topcoder authenticator', () => { expect(directory).toHaveBeenCalledTimes(2); }); - it('still accepts legacy HS256 administrators on the same deployment', async () => { + it('rejects shared-secret administrator tokens in public-key-only mode', async () => { const token = jwt.sign( - { userId: '12345', roles: ['Administrator'] }, + { userId: '12345', emailVerified: true, roles: ['Administrator'] }, testSecret, { algorithm: 'HS256', @@ -125,7 +125,7 @@ describe('reports directory with the shared Topcoder authenticator', () => { await request(app.getHttpServer()) .get('/v6/forms/reports/directory') .auth(token, { type: 'bearer' }) - .expect(200); + .expect(401); }); it.each([ diff --git a/test/auth.spec.ts b/test/auth.spec.ts index 731569f..3b04a91 100644 --- a/test/auth.spec.ts +++ b/test/auth.spec.ts @@ -9,6 +9,18 @@ import { testSecret } from './fixtures'; const config = readConfig({ DATABASE_URL: 'postgresql://localhost/forms', AUTH_SECRET: testSecret, + JWT_ISSUER_POLICIES: JSON.stringify( + [ + 'https://api.topcoder-dev.com', + 'https://api.topcoder.com', + 'https://forms.test', + ].map((issuer) => ({ + issuer, + algorithm: 'HS256', + allowMissingAudience: true, + legacyHs256Until: new Date(Date.now() + 3_600_000).toISOString(), + })), + ), VALID_ISSUERS: 'https://api.topcoder-dev.com,https://api.topcoder.com,https://forms.test', }); @@ -30,6 +42,7 @@ async function authenticate( const now = Math.floor(Date.now() / 1000); const jwt = await new SignJWT({ userId: '12345', + emailVerified: true, roles: ['Administrator'], iss: 'https://api.topcoder-dev.com', iat: now, @@ -131,9 +144,12 @@ describe('Topcoder bearer authentication', () => { sub: 'client@clients', aud: 'forms-api', gty: 'client-credentials', - userId: undefined, roles: undefined, + userId: undefined, + roles: undefined, }; await expect(authenticate(machine)).rejects.toMatchObject({ status: 401 }); - await expect(authenticate({ ...machine, scope: 'read:forms-submissions' })).rejects.toMatchObject({ status: 401 }); + await expect( + authenticate({ ...machine, scope: 'read:forms-submissions' }), + ).rejects.toMatchObject({ status: 401 }); }); }); diff --git a/test/fixtures.ts b/test/fixtures.ts index 0a1c4cb..8e979bb 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -15,6 +15,14 @@ export function testConfig() { return readConfig({ DATABASE_URL: process.env.TEST_DATABASE_URL, AUTH_SECRET: testSecret, + JWT_ISSUER_POLICIES: JSON.stringify([ + { + issuer: 'https://forms.test', + algorithm: 'HS256', + allowMissingAudience: true, + legacyHs256Until: new Date(Date.now() + 3_600_000).toISOString(), + }, + ]), VALID_ISSUERS: 'https://forms.test', THROTTLE_LIMIT: '10000', CORS_ORIGINS: 'https://www.topcoder.com', @@ -28,7 +36,7 @@ export function testConfig() { */ export async function token(claims: Record) { const { SignJWT } = await import('jose'); - return new SignJWT(claims) + return new SignJWT({ emailVerified: true, ...claims }) .setProtectedHeader({ alg: 'HS256' }) .setSubject('test-user') .setIssuer('https://forms.test') diff --git a/test/policies.spec.ts b/test/policies.spec.ts index 792f162..f1a64d0 100644 --- a/test/policies.spec.ts +++ b/test/policies.spec.ts @@ -165,7 +165,8 @@ describe('definition and deployment policies', () => { DATABASE_URL: 'postgresql://localhost/topcoder-services?schema=public', }), ).toThrow(); - expect(() => readConfig({ ...env, AUTH_SECRET: '' })).toThrow(); + expect(readConfig({ ...env, AUTH_SECRET: '' }).authSecret).toBeUndefined(); + expect(() => readConfig({ ...env, AUTH_SECRET: 'short' })).toThrow(); expect( readConfig({ ...env, VALID_ISSUERS: '["https://forms.test"]' }).issuers, ).toEqual(['https://forms.test']); From 9b53fd3911892fa1867283a7baca03ce45cec4e2 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Mon, 9 Nov 2026 11:57:27 +1100 Subject: [PATCH 2/2] Update auth test fixtures for explicit issuer policies --- test/fixtures.ts | 6 ++++++ test/forms.integration.spec.ts | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/test/fixtures.ts b/test/fixtures.ts index 8e979bb..2b4364c 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -22,6 +22,12 @@ export function testConfig() { allowMissingAudience: true, legacyHs256Until: new Date(Date.now() + 3_600_000).toISOString(), }, + { + issuer: 'https://forms-machine.test', + algorithm: 'RS256', + audience: 'forms-api', + jwksUri: 'https://forms-machine.test/.well-known/jwks.json', + }, ]), VALID_ISSUERS: 'https://forms.test', THROTTLE_LIMIT: '10000', diff --git a/test/forms.integration.spec.ts b/test/forms.integration.spec.ts index 93f5ca5..6ccd084 100644 --- a/test/forms.integration.spec.ts +++ b/test/forms.integration.spec.ts @@ -51,7 +51,7 @@ describe('forms API with real PostgreSQL', () => { algorithm: 'RS256', keyid: machineKeyId, subject: 'test-client@clients', - issuer: 'https://forms.test', + issuer: 'https://forms-machine.test', audience: 'forms-api', expiresIn: '5m', }, @@ -62,7 +62,7 @@ describe('forms API with real PostgreSQL', () => { const keys = generateKeyPairSync('rsa', { modulusLength: 2048 }); machineKey = keys.privateKey; // Mock key discovery only; the shared authenticator still verifies RSA signatures. - nock('https://forms.test') + nock('https://forms-machine.test') .get('/.well-known/jwks.json') .reply(200, { keys: [ @@ -1384,13 +1384,13 @@ describe('forms API with real PostgreSQL', () => { algorithm: 'HS256', }, { - issuer: 'https://forms.test', + issuer: 'https://forms-machine.test', audience: 'forms-api', expiration: '-1m', algorithm: 'HS256', }, { - issuer: 'https://forms.test', + issuer: 'https://forms-machine.test', audience: 'forms-api', expiration: '5m', algorithm: 'HS384',