Download src/http/core/auth.ts from xjackkx/s2: direct link, hf CLI and curl.
- Browser
- Download file 935 Bytes
-
https://huggingface.co/spaces/xjackkx/s2/resolve/main/src/http/core/auth.ts
- Command line
-
hf download hf://spaces/xjackkx/s2/src/http/core/auth.ts
-
curl -L -o auth.ts https://huggingface.co/spaces/xjackkx/s2/resolve/main/src/http/core/auth.ts
935 Bytes
| import { timingSafeStringEqual } from "../../shared/crypto"; | |
| type AuthConfig = { api_keys?: readonly string[] | null | undefined }; | |
| // Accept the caller key from Bearer, x-api-key, x-goog-api-key, or ?key=. | |
| export function authorized( | |
| request: Request, | |
| url: URL, | |
| cfg: AuthConfig, | |
| ): boolean { | |
| const keys = cfg.api_keys || []; | |
| if (!keys.length) return true; | |
| const h = request.headers; | |
| const auth = h.get("authorization") || ""; | |
| const bearer = /^\s*Bearer\s+(.+?)\s*$/i.exec(auth); | |
| const candidates = [ | |
| bearer?.[1] ? bearer[1] : null, | |
| h.get("x-api-key"), | |
| h.get("x-goog-api-key"), | |
| url.searchParams.get("key"), | |
| ]; | |
| for (const raw of candidates) { | |
| const candidate = String(raw || "").trim(); | |
| if (!candidate) continue; | |
| let matched = false; | |
| for (const configured of keys) { | |
| matched = | |
| timingSafeStringEqual(candidate, String(configured || "")) || matched; | |
| } | |
| if (matched) return true; | |
| } | |
| return false; | |
| } | |