-
-
Notifications
You must be signed in to change notification settings - Fork 255
feat: Support specifying multiple apiKeys #144
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
Open
ZiuChen
wants to merge
4
commits into
ericc-ch:master
Choose a base branch
from
ZiuChen:feat/api-key-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
β0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
52c9d31
feat: Support specifying multiple apiKeys
ZiuChen 666a0ec
fix: Improve API key extraction fallback for query parameters
ZiuChen fb889b9
feat: Implement constant time comparison for API key validation
ZiuChen c1d0139
fix: Remove query parameter fallback for API key extraction and simplβ¦
ZiuChen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { Context, MiddlewareHandler } from "hono" | ||
|
|
||
| import { HTTPException } from "hono/http-exception" | ||
|
|
||
| import { state } from "./state" | ||
| import { constantTimeEqual } from "./utils" | ||
|
|
||
| /** | ||
| * Extract API key from request headers | ||
| * Supports both OpenAI format (Authorization: Bearer token) and Anthropic format (x-api-key: token) | ||
| */ | ||
| function extractApiKey(c: Context): string | undefined { | ||
| // OpenAI format: Authorization header with Bearer prefix | ||
| const authHeader = c.req.header("authorization") | ||
| if (authHeader?.startsWith("Bearer ")) { | ||
| return authHeader.slice(7) // Remove 'Bearer ' prefix | ||
| } | ||
|
|
||
| // Anthropic format: x-api-key header | ||
| const anthropicKey = c.req.header("x-api-key") | ||
| if (anthropicKey) { | ||
| return anthropicKey | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * API key authentication middleware | ||
| * Validates that the request contains a valid API key if API keys are configured | ||
| */ | ||
| export const apiKeyAuthMiddleware: MiddlewareHandler = async (c, next) => { | ||
| // If no API keys are configured, skip authentication | ||
| if (!state.apiKeys || state.apiKeys.length === 0) { | ||
| await next() | ||
| return | ||
| } | ||
|
|
||
| const providedKey = extractApiKey(c) | ||
|
|
||
| // If no API key is provided, return 401 | ||
| if (!providedKey) { | ||
| throw new HTTPException(401, { | ||
| message: "Missing API key", | ||
| }) | ||
| } | ||
|
|
||
| // Check if the provided key matches any of the configured keys | ||
| const isValidKey = state.apiKeys.some((key) => | ||
| constantTimeEqual(key, providedKey), | ||
| ) | ||
|
|
||
| if (!isValidKey) { | ||
| throw new HTTPException(401, { | ||
| message: "Invalid API key", | ||
| }) | ||
| } | ||
|
|
||
| // Key is valid, continue with the request | ||
| 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
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
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.
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.
Document usage viewer behavior with API key authentication.
The API Key Authentication section is well-written and comprehensive. However, there's a documentation gap: the Usage Viewer section (lines 302-322) doesn't explain how to access the dashboard when API key authentication is enabled. Users may be confused about whether they need to provide an API key in the URL or if the endpoint remains accessible.
Consider adding a note in either the API Key Authentication section or the Usage Viewer section explaining how authentication works with the browser-based dashboard (e.g., whether the
/usageendpoint requires authentication when accessed directly vs. through the dashboard).π€ Prompt for AI Agents