Skip to content

Commit e593de8

Browse files
committed
fix: Update authentication logic to reference data.type for OIDC connection status and enhance README documentation
1 parent 7ac3919 commit e593de8

8 files changed

Lines changed: 53 additions & 20 deletions

File tree

‎packages/frontend/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ already bound, dev exits instead of picking another. Proxy wiring lives in
4747

4848
[`authFetch.ts`](src/authFetch.ts) wraps `fetch` and redirects to OIDC login on HTTP 401.
4949
[`authSession.ts`](src/authSession.ts) probes `/me` with a non-redirecting client so the
50-
welcome screen can show before login. Pass the auth-aware `fetch` into
50+
welcome screen can show before login. Logout chrome appears when `me().data.type` is
51+
`oidc-connected`. Pass the auth-aware `fetch` into
5152
`server={{ type: 'trueforge', fetch }}` so the built-in Harness adapter shares the same
5253
session cookies.
5354

‎packages/frontend/src/LogoutButton.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { UI_BASE_PATH } from './publicPath';
66

77
/**
88
* Icon button next to Settings (via `ShellActionsActionSlot` override).
9-
* Shown only when `auth.me()` returns `type: "oidc-connected"`.
9+
* Shown only when `auth.me()` returns `data.type: "oidc-connected"`.
1010
* Uses a module cache so remounts of the action slot do not hide the control during refetches.
1111
*/
1212
export function LogoutButton() {

‎packages/frontend/src/authSession.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export function resetOidcSessionCacheForTests(): void {
3737
}
3838

3939
/**
40-
* True when the current session is a browser OIDC login (`type: "oidc-connected"`).
40+
* True when the current session is a browser OIDC login (`data.type: "oidc-connected"`).
4141
* When auth is enabled, unauthenticated callers get HTTP 401 from `/me`.
4242
*/
4343
export async function isOidcConnectedSession(client: TrueForge = authClient): Promise<boolean> {
44-
const session = await client.auth.me();
45-
const isConnected = session.type === 'oidc-connected';
44+
const { data } = await client.auth.me();
45+
const isConnected = data.type === 'oidc-connected';
4646
cachedIsOidcConnected = isConnected;
4747
return isConnected;
4848
}

‎packages/frontend/tests/authSession.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ function createClient(params: { type?: 'default' | 'oidc-connected'; meError?: E
1515
me: async () => {
1616
if (params.meError != null) throw params.meError;
1717
return {
18-
type: params.type ?? 'default',
19-
email: 'user@example.com',
20-
role: 'user',
18+
data: {
19+
type: params.type ?? 'default',
20+
tenantId: 'default',
21+
subject: { id: 'user-1', type: 'user', displayName: 'user-1' },
22+
roles: [],
23+
},
2124
};
2225
},
2326
},

‎packages/trueforge/src/apis/schedules.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function validateManifest(manifest: Pick<ScheduleManifest, 'cron' | 'time
102102
const FORBIDDEN_SCHEDULE_ACCESS = 'Only the schedule creator can access this schedule';
103103

104104
/**
105-
* A schedule is visible to its creator, and to any admin.
105+
* A schedule is visible to its creator, and to any admin ({@link hasAdminRole}).
106106
*
107107
* Standalone auth stamps `roles: ['admin']` on the sole identity, which already
108108
* owns everything it created — so admin bypass is a no-op there.

‎packages/trueforge/src/auth/identity.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { Context } from 'hono';
22

3+
import configuration, { getTrueForgeMode, isOidcConfigured, TrueForgeMode } from '../config';
4+
5+
/** TrueFoundry Control Plane role treated as admin until mapping is settled. */
6+
export const TRUEFOUNDRY_ADMIN_ROLE = 'tenant-admin';
7+
8+
/** Standalone / default TrueForge admin role string. */
9+
export const STANDALONE_ADMIN_ROLE = 'admin';
10+
311
export interface RequestSubject {
412
id: string;
513
type: string;
@@ -21,7 +29,7 @@ export const STANDALONE_REQUEST_CONTEXT: RequestContext = {
2129
type: 'user',
2230
display_name: 'trueforge-default',
2331
},
24-
roles: ['admin'],
32+
roles: [STANDALONE_ADMIN_ROLE],
2533
user_credential: null,
2634
};
2735

@@ -41,7 +49,25 @@ export function resolveRequestContext(c: Context): RequestContext {
4149
return requestContext;
4250
}
4351

44-
/** Whether the caller holds the TrueForge admin role (`admin`). */
52+
/**
53+
* Whether the caller is treated as admin for settings, capabilities, and schedule bypass.
54+
* Mode and OIDC admin claim value come from process config ({@link getTrueForgeMode}).
55+
* - Standalone: `roles` includes `admin`
56+
* - OIDC: `roles` includes configured `OIDC_ADMIN_ROLE_VALUE`
57+
* - TrueFoundry: `roles` includes `tenant-admin` (temporary; revisit mapping later)
58+
*/
4559
export function hasAdminRole(requestContext: Pick<RequestContext, 'roles'>): boolean {
46-
return requestContext.roles.includes('admin');
60+
switch (getTrueForgeMode()) {
61+
case TrueForgeMode.TrueFoundry:
62+
// TODO: stop treating `tenant-admin` as TrueForge admin once TFY role mapping is settled.
63+
return requestContext.roles.includes(TRUEFOUNDRY_ADMIN_ROLE);
64+
case TrueForgeMode.Oidc: {
65+
const adminValue = isOidcConfigured(configuration)
66+
? configuration.OIDC.OIDC_ADMIN_ROLE_VALUE
67+
: STANDALONE_ADMIN_ROLE;
68+
return requestContext.roles.includes(adminValue);
69+
}
70+
case TrueForgeMode.Standalone:
71+
return requestContext.roles.includes(STANDALONE_ADMIN_ROLE);
72+
}
4773
}

‎packages/trueforge/src/auth/middleware.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Context, MiddlewareHandler } from 'hono';
22
import { HTTPException } from 'hono/http-exception';
33
import { jwtVerify } from 'jose';
44

5-
import { getTrueForgeMode, TrueForgeMode } from '../config';
65
import type { Authenticator } from './authenticator';
76
import { toRequestContext, type IdTokenClaims } from './claims';
87
import { hasAdminRole, type RequestContext } from './identity';
@@ -21,12 +20,7 @@ export function createAuthMiddleware(authenticator: Authenticator): MiddlewareHa
2120
export function createAdminAuthMiddleware(authenticator: Authenticator): MiddlewareHandler {
2221
return async (c, next) => {
2322
const requestContext = await authenticator.authenticate(c);
24-
if (getTrueForgeMode() === TrueForgeMode.TrueFoundry) {
25-
// TODO: stop treating `tenant-admin` as TrueForge admin once TFY role mapping is settled.
26-
if (!requestContext.roles.includes('tenant-admin')) {
27-
throw new HTTPException(403, { message: 'Admin access required' });
28-
}
29-
} else if (!hasAdminRole(requestContext)) {
23+
if (!hasAdminRole(requestContext)) {
3024
throw new HTTPException(403, { message: 'Admin access required' });
3125
}
3226
c.set('request_context', requestContext);

‎packages/trueforge/tests/unit/auth/identity.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Hono } from 'hono';
2-
import { resolveRequestContext, STANDALONE_REQUEST_CONTEXT } from '../../../src/auth/identity';
2+
import { hasAdminRole, resolveRequestContext, STANDALONE_REQUEST_CONTEXT } from '../../../src/auth/identity';
33

44
describe('STANDALONE_REQUEST_CONTEXT', () => {
55
it('has the fixed standalone identity shape', () => {
@@ -16,6 +16,15 @@ describe('STANDALONE_REQUEST_CONTEXT', () => {
1616
});
1717
});
1818

19+
describe('hasAdminRole', () => {
20+
// Unit tests run under STANDALONE=true (.env.test) → mode Standalone.
21+
it('treats admin as admin and other roles as non-admin in standalone', () => {
22+
expect(hasAdminRole({ roles: ['admin'] })).toBe(true);
23+
expect(hasAdminRole({ roles: ['everyone'] })).toBe(false);
24+
expect(hasAdminRole(STANDALONE_REQUEST_CONTEXT)).toBe(true);
25+
});
26+
});
27+
1928
describe('resolveRequestContext', () => {
2029
it('returns request_context when set by auth middleware', async () => {
2130
const app = new Hono();

0 commit comments

Comments
 (0)