-
Notifications
You must be signed in to change notification settings - Fork 12
feat(agent-bff): request-edge wiring (timezone + CORS + auth-mode precedence) #1731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nbouliol
merged 6 commits into
main
from
feature/prd-666-agent-nodejs-request-edge-wiring-timezone-cors-auth-mode
Jul 2, 2026
+1,495
−41
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06f0aa1
feat(agent-bff): request-edge wiring (timezone + CORS + auth-mode pre…
nbouliol cb4fa0b
fix(agent-bff): harden request edge per code review
nbouliol 4435c5e
docs(agent-bff): clarify OAuth-key scope and api-key middleware error…
nbouliol 34c3be4
test(agent-bff): cover agent-edge wiring and body timezone resolution
nbouliol 75b8fb1
fix(agent-bff): address review on timezone cache, token type, error m…
nbouliol 55db5ef
fix(agent-bff): reject bff_access tokens without an exp claim
nbouliol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { Middleware } from 'koa'; | ||
|
|
||
| import { buildAgentQuery } from './build-agent-query'; | ||
|
|
||
| export default function createAgentStubMiddleware(): Middleware { | ||
| return async function agentStubMiddleware(ctx) { | ||
| const query = buildAgentQuery({ timezone: ctx.state.timezone as string }); | ||
| ctx.state.agentQuery = query; | ||
|
|
||
| ctx.status = 501; | ||
| ctx.body = { | ||
| error: { | ||
| type: 'not_implemented', | ||
| status: 501, | ||
| message: 'Agent proxy is not implemented yet', | ||
| }, | ||
| }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| export interface AgentQuery { | ||
| timezone: string; | ||
| } | ||
|
|
||
| export interface BuildAgentQueryParams { | ||
| timezone: string; | ||
| } | ||
|
|
||
| export function buildAgentQuery({ timezone }: BuildAgentQueryParams): AgentQuery { | ||
| return { timezone }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { BffAccessTokenPayload } from '../oauth/bff-token'; | ||
| import type { Middleware } from 'koa'; | ||
|
|
||
| import jsonwebtoken from 'jsonwebtoken'; | ||
|
|
||
| import { extractBearerToken, resolveAuthMode } from './auth-mode'; | ||
| import { sessionExpired, unauthorized } from '../http/bff-http-error'; | ||
| import { BFF_ACCESS_TOKEN_TYPE } from '../oauth/bff-token'; | ||
|
|
||
| export const BFF_KEY_HEADER = 'X-Forest-Bff-Key'; | ||
|
|
||
| export interface AuthModeMiddlewareOptions { | ||
| authSecret: string; | ||
| } | ||
|
|
||
| function verifyBffAccess(token: string, authSecret: string): BffAccessTokenPayload { | ||
| let decoded: unknown; | ||
|
|
||
| // Verify the signature first but ignore expiration, so the token type can be | ||
| // checked before expiry: a wrong-typed token must be `unauthorized`, and only | ||
| // a genuine expired `bff_access` should map to `session_expired`. | ||
| try { | ||
| decoded = jsonwebtoken.verify(token, authSecret, { | ||
| algorithms: ['HS256'], | ||
| ignoreExpiration: true, | ||
| }); | ||
| } catch { | ||
| throw unauthorized(); | ||
| } | ||
|
|
||
| if ( | ||
| typeof decoded !== 'object' || | ||
| decoded === null || | ||
| (decoded as { type?: unknown }).type !== BFF_ACCESS_TOKEN_TYPE | ||
| ) { | ||
| throw unauthorized(); | ||
| } | ||
|
|
||
| const { exp } = decoded as { exp?: unknown }; | ||
|
|
||
| if (typeof exp !== 'number') { | ||
| throw unauthorized(); | ||
| } | ||
|
|
||
| if (exp * 1000 <= Date.now()) { | ||
| throw sessionExpired(); | ||
| } | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
|
|
||
| return decoded as BffAccessTokenPayload; | ||
| } | ||
|
|
||
| export default function createAuthModeMiddleware({ | ||
| authSecret, | ||
| }: AuthModeMiddlewareOptions): Middleware { | ||
| return async function authModeMiddleware(ctx, next) { | ||
| const bearer = extractBearerToken(ctx.get('Authorization')); | ||
| const apiKey = ctx.get(BFF_KEY_HEADER); | ||
|
|
||
| const mode = resolveAuthMode({ hasBearer: bearer !== undefined, hasApiKey: apiKey !== '' }); | ||
|
|
||
| ctx.state.authMode = mode; | ||
|
|
||
| if (mode === 'oauth') { | ||
| ctx.state.principal = verifyBffAccess(bearer as string, authSecret); | ||
| } | ||
|
|
||
| await next(); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { ambiguousCredentials, unauthorized } from '../http/bff-http-error'; | ||
|
|
||
| export type AuthMode = 'oauth' | 'api-key'; | ||
|
|
||
| const BEARER_PATTERN = /^Bearer[ \t]+(.+)$/i; | ||
|
|
||
| export function extractBearerToken(authorization: string | undefined): string | undefined { | ||
| if (!authorization) return undefined; | ||
|
|
||
| const match = BEARER_PATTERN.exec(authorization.trim()); | ||
| if (!match) return undefined; | ||
|
|
||
| const token = match[1].trim(); | ||
|
|
||
| return token === '' ? undefined : token; | ||
| } | ||
|
|
||
| export function resolveAuthMode({ | ||
| hasBearer, | ||
| hasApiKey, | ||
| }: { | ||
| hasBearer: boolean; | ||
| hasApiKey: boolean; | ||
| }): AuthMode { | ||
| if (hasBearer && hasApiKey) throw ambiguousCredentials(); | ||
| if (hasApiKey) return 'api-key'; | ||
| if (hasBearer) return 'oauth'; | ||
|
|
||
| throw unauthorized(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.