Repository navigation
Make Git tokens reusable for API and MCP with configurable expiration #8
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import {authenticatePersonalToken} from '../utils/personalTokens'; | ||
| import { ui } from '../i18n/server'; | ||
| import { createMiddleware } from 'hono/factory'; | ||
| import { getCookie } from 'hono/cookie'; | ||
|
|
@@ -28,6 +29,13 @@ export const rbacMiddleware = createMiddleware<Env>(async (c, next) => { | |
| const SESSION_CACHE_TTL = 1800; // KV 캐시 TTL: 30분 | ||
|
|
||
| export const sessionMiddleware = createMiddleware<Env>(async (c, next) => { | ||
| const authorization = c.req.header('Authorization') || ''; | ||
| if (c.req.path.startsWith('/api/') && /^Bearer (?:wiki|git)_/i.test(authorization)) { | ||
| const user = await authenticatePersonalToken(c.env, authorization.slice(7).trim()); | ||
| if (!user) return c.json({ error: 'Invalid or expired API token' }, 401, { 'Cache-Control': 'no-store' }); | ||
| c.set('user', user); c.set('apiTokenAuthenticated', true); | ||
| return next(); | ||
|
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because this authenticates personal tokens for every Useful? React with 👍 / 👎. |
||
| } | ||
| const sessionId = getCookie(c, 'wiki_session'); | ||
|
|
||
| if (!sessionId) { | ||
|
|
@@ -176,3 +184,9 @@ export function requirePermission(permission: string) { | |
| return next(); | ||
| }); | ||
| } | ||
|
|
||
| /** Credential management is reserved for browser sessions, never personal bearer tokens. */ | ||
| export const requireBrowserSession = createMiddleware<Env>(async (c, next) => { | ||
| if (c.get('apiTokenAuthenticated')) return c.json({error:ui('tokens.sessionRequired')},403); | ||
| return next(); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an expired, revoked, or malformed
wiki_/git_token is sent to/api/mcp, this early response preventsresolveBearerAuthfrom handling it. The MCP-specific handler deliberately returns aWWW-Authenticateheader containing the protected-resource metadata so clients can initiate reauthentication, whereas this response has no challenge header; clients using a personal token will therefore stop with a generic 401 when it expires instead of entering the MCP authentication flow. Skip personal-token session handling for/api/mcpand let its existing bearer resolver validate these tokens.Useful? React with 👍 / 👎.