forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-key-auth.ts
More file actions
68 lines (56 loc) · 1.96 KB
/
api-key-auth.ts
File metadata and controls
68 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { Context, MiddlewareHandler } from "hono"
import { HTTPException } from "hono/http-exception"
import { state } from "./state"
/**
* Retrieve an API key from the incoming request.
*
* Checks common locations where clients supply keys (Authorization Bearer header, `x-api-key` header, or `apiKey` query parameter) and returns the first one found.
*
* @returns The extracted API key, or `undefined` if no key is present.
*/
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
}
// Fallback: query parameter, for extra compatibility of `/usage` or `/token` route
const queryKey = c.req.query("apiKey")
if (queryKey) {
return queryKey
}
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:
"API key required. Please provide a valid API key in the Authorization header (Bearer token) or x-api-key header.",
})
}
// Check if the provided key matches any of the configured keys
const isValidKey = state.apiKeys.includes(providedKey)
if (!isValidKey) {
throw new HTTPException(401, {
message: "Invalid API key. Please provide a valid API key.",
})
}
// Key is valid, continue with the request
await next()
}