import { OAUTH_CLIENT_ALLOWED_SCOPES } from '../../db/json-columns'; import { decodeJson } from '../../utils/json-column'; import { logWarn } from '../audit/audit-log.logger'; import { AuditService } from '../audit/audit.service'; import { getClientIp } from '../audit/client-ip'; import { Public } from '../auth-core/public.decorator'; import { RateLimitService } from '../common/rate-limit.service'; import { OauthService } from './oauth.service'; import { Controller, Get, Headers, HttpCode, Post, Req, Res } from '@nestjs/common'; import type { Request, Response } from 'express'; const MIN = 60_000; /** Drops every trailing slash from a resource indicator, exactly like the `/\/+$/` replace * it stands in for — as a scan, because that pattern re-walks the slash run from every * start position of a `resource` value the caller controls. */ function stripTrailingSlashes(value: string): string { let end = value.length; while (end > 0 && value.charCodeAt(end - 1) === 47 /* '/' */) end--; return value.slice(0, end); } /** * Public OAuth 2.1 endpoints (no session) — byte-identical to the legacy * oauthPublicRouter (server/src/routes/oauth.ts): MCP-addon gated (404), the * per-(ip,client) token / per-ip revoke rate limits, no-store cache headers on * /token, the WWW-Authenticate challenge on /userinfo, the three grant types * and the RFC 7009 always-200 revoke. Uses @Res directly because every branch * sets headers + a specific status the way the spec requires. */ @Public('the OAuth 2.1 endpoints a client reaches before it has a session') @Controller('oauth') export class OauthPublicController { constructor( private readonly oauth: OauthService, private readonly rl: RateLimitService, private readonly audit: AuditService, ) {} @Post('token') @HttpCode(200) // token success uses res.json without an explicit status; Express defaults to 200 (Nest POST would default to 201). async token(@Req() req: Request, @Res() res: Response): Promise { if (!(await this.oauth.mcpEnabled())) { res.status(404).end(); return; } const body: Record = typeof req.body === 'object' && req.body ? req.body : {}; if (!(await this.rl.check('oauth_token', `${req.ip}|${body.client_id ?? ''}`, 30, MIN, Date.now()))) { res .status(429) .json({ error: 'too_many_requests', error_description: 'Too many attempts. Please try again later.' }); return; } res.set('Cache-Control', 'no-store'); res.set('Pragma', 'no-cache'); const { grant_type, code, redirect_uri, client_id, client_secret, code_verifier, refresh_token, resource } = body; const ip = getClientIp(req); if (!client_id) { res.status(401).json({ error: 'invalid_client', error_description: 'client_id is required' }); return; } if (grant_type === 'authorization_code') { if (!code || !redirect_uri || !code_verifier) { res .status(400) .json({ error: 'invalid_request', error_description: 'code, redirect_uri, and code_verifier are required' }); return; } const pending = await this.oauth.consumeAuthCode(code); const invalidGrant = async (reason: string, userId: number | null) => { await this.audit.writeAudit({ userId, action: 'oauth.token.grant_failed', details: { client_id, reason }, ip }); res.status(400).json({ error: 'invalid_grant', error_description: 'Authorization grant is invalid.' }); }; if (!pending) return invalidGrant('code_invalid_or_expired', null); if (pending.clientId !== client_id) return invalidGrant('client_id_mismatch', pending.userId); if (pending.redirectUri !== redirect_uri) return invalidGrant('redirect_uri_mismatch', pending.userId); if (pending.resource && resource && pending.resource !== stripTrailingSlashes(resource)) return invalidGrant('resource_mismatch', pending.userId); if (!(await this.oauth.authenticateClient(client_id, client_secret))) { logWarn(`[OAuth] Invalid client credentials for client_id=${client_id} ip=${ip ?? '-'}`); await this.audit.writeAudit({ userId: pending.userId, action: 'oauth.token.client_auth_failed', details: { client_id }, ip, }); res.status(401).json({ error: 'invalid_client', error_description: 'Invalid client credentials' }); return; } if (!this.oauth.verifyPKCE(code_verifier, pending.codeChallenge)) return invalidGrant('pkce_failed', pending.userId); const tokens = await this.oauth.issueTokens( client_id, pending.userId, pending.scopes, null, pending.resource ?? null, ); await this.audit.writeAudit({ userId: pending.userId, action: 'oauth.token.issue', details: { client_id, scopes: pending.scopes, audience: pending.resource ?? null }, ip, }); res.json(tokens); return; } if (grant_type === 'refresh_token') { if (!refresh_token) { res.status(400).json({ error: 'invalid_request', error_description: 'refresh_token is required' }); return; } const result = await this.oauth.refreshTokens(refresh_token, client_id, client_secret, ip); if (result.error) { if (result.error === 'invalid_client') logWarn(`[OAuth] Invalid client credentials on refresh for client_id=${client_id} ip=${ip ?? '-'}`); res.status(result.status || 400).json({ error: result.error, error_description: result.error === 'invalid_client' ? 'Invalid client credentials' : 'Refresh token is invalid or expired', }); return; } res.json(result.tokens); return; } if (grant_type === 'client_credentials') { if (!client_secret) { res.status(401).json({ error: 'invalid_client', error_description: 'client_secret is required for client_credentials grant', }); return; } const client = await this.oauth.authenticateClient(client_id, client_secret); if (!client) { logWarn(`[OAuth] Invalid client credentials for client_id=${client_id} ip=${ip ?? '-'}`); await this.audit.writeAudit({ userId: null, action: 'oauth.token.client_auth_failed', details: { client_id }, ip, }); res.status(401).json({ error: 'invalid_client', error_description: 'Invalid client credentials' }); return; } if (client.is_public || !client.allows_client_credentials || client.user_id == null) { await this.audit.writeAudit({ userId: client.user_id ?? null, action: 'oauth.token.grant_failed', details: { client_id, reason: 'unauthorized_client' }, ip, }); res.status(400).json({ error: 'unauthorized_client', error_description: 'This client is not authorized for the client_credentials grant', }); return; } const allowedScopes = decodeJson(OAUTH_CLIENT_ALLOWED_SCOPES, client.allowed_scopes, `client ${client_id}`); let grantedScopes: string[]; if (body.scope) { const requested = body.scope.split(' ').filter(Boolean); const invalid = requested.filter((s) => !allowedScopes.includes(s)); if (invalid.length > 0) { res.status(400).json({ error: 'invalid_scope', error_description: `Scopes not allowed for this client: ${invalid.join(', ')}`, }); return; } grantedScopes = requested; } else { grantedScopes = allowedScopes; } const audience = resource ? stripTrailingSlashes(resource) : `${stripTrailingSlashes(this.oauth.mcpSafeUrl())}/mcp`; const tokens = await this.oauth.issueClientCredentialsToken(client_id, client.user_id, grantedScopes, audience); await this.audit.writeAudit({ userId: client.user_id, action: 'oauth.token.issue', details: { client_id, scopes: grantedScopes, audience, grant: 'client_credentials' }, ip, }); res.json(tokens); return; } res .status(400) .json({ error: 'unsupported_grant_type', error_description: `Unsupported grant_type: ${grant_type}` }); } @Get('userinfo') async userinfo(@Headers('authorization') auth: string | undefined, @Res() res: Response): Promise { if (!(await this.oauth.mcpEnabled())) { res.status(404).end(); return; } if (!auth || !auth.toLowerCase().startsWith('bearer ')) { res.set('WWW-Authenticate', 'Bearer realm="TREK MCP"'); res.status(401).json({ error: 'invalid_token' }); return; } const info = await this.oauth.getUserByAccessToken(auth.slice(7)); if (!info) { res.set('WWW-Authenticate', 'Bearer realm="TREK MCP", error="invalid_token"'); res.status(401).json({ error: 'invalid_token' }); return; } res.json({ sub: String(info.user.id), email: info.user.email, email_verified: info.emailVerified, preferred_username: info.user.username, }); } @Post('revoke') async revoke(@Req() req: Request, @Res() res: Response): Promise { if (!(await this.oauth.mcpEnabled())) { res.status(404).end(); return; } if (!(await this.rl.check('oauth_revoke', req.ip || 'unknown', 10, MIN, Date.now()))) { res .status(429) .json({ error: 'too_many_requests', error_description: 'Too many attempts. Please try again later.' }); return; } const body: Record = typeof req.body === 'object' && req.body ? req.body : {}; const { token, client_id, client_secret } = body; const ip = getClientIp(req); if (!token || !client_id) { res.status(400).json({ error: 'invalid_request', error_description: 'token and client_id are required' }); return; } if (!(await this.oauth.authenticateClient(client_id, client_secret))) { logWarn(`[OAuth] Invalid client credentials on revoke for client_id=${client_id} ip=${ip ?? '-'}`); await this.audit.writeAudit({ userId: null, action: 'oauth.token.client_auth_failed', details: { client_id, endpoint: 'revoke' }, ip, }); res.status(401).json({ error: 'invalid_client', error_description: 'Invalid client credentials' }); return; } await this.oauth.revokeToken(token, client_id, undefined, ip); res.status(200).json({}); } }