chore: use hardcoded dummy API key
- Replace env-based API_KEY with sk-dummy-key hardcoded - All entry points use same key: Authorization: Bearer sk-dummy-key - Simplifies usage for library/client consumption Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
c0a02279bd
commit
47b85fb396
+1
-2
@@ -41,10 +41,9 @@ const RELAY_VERSION = "1.0.0";
|
|||||||
|
|
||||||
// ─── API Key Authentication ─────────────────────────────────────────────────────
|
// ─── API Key Authentication ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
const API_KEY = process.env.API_KEY ?? "";
|
const API_KEY = "sk-dummy-key";
|
||||||
|
|
||||||
function requireAuth(req: Request): Response | null {
|
function requireAuth(req: Request): Response | null {
|
||||||
if (!API_KEY) return null;
|
|
||||||
const header = req.headers.get("authorization") ?? req.headers.get("x-api-key") ?? "";
|
const header = req.headers.get("authorization") ?? req.headers.get("x-api-key") ?? "";
|
||||||
const key = header.replace(/^Bearer\s+/i, "").trim();
|
const key = header.replace(/^Bearer\s+/i, "").trim();
|
||||||
if (key === API_KEY) return null;
|
if (key === API_KEY) return null;
|
||||||
|
|||||||
+1
-7
@@ -45,15 +45,9 @@ const RELAY_VERSION = "1.0.0";
|
|||||||
|
|
||||||
// ─── API Key Authentication ─────────────────────────────────────────────────────
|
// ─── API Key Authentication ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
const API_KEY = process.env.API_KEY ?? "";
|
const API_KEY = "sk-dummy-key";
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a request is authorized.
|
|
||||||
* Returns a 401 Response if unauthorized, or null if allowed.
|
|
||||||
* When API_KEY is empty, all requests pass through.
|
|
||||||
*/
|
|
||||||
function requireAuth(req: Request): Response | null {
|
function requireAuth(req: Request): Response | null {
|
||||||
if (!API_KEY) return null; // auth disabled
|
|
||||||
const header = req.headers.get("authorization") ?? req.headers.get("x-api-key") ?? "";
|
const header = req.headers.get("authorization") ?? req.headers.get("x-api-key") ?? "";
|
||||||
const key = header.replace(/^Bearer\s+/i, "").trim();
|
const key = header.replace(/^Bearer\s+/i, "").trim();
|
||||||
if (key === API_KEY) return null;
|
if (key === API_KEY) return null;
|
||||||
|
|||||||
+5
-6
@@ -70,11 +70,10 @@ function getClientIP(req: Request): string {
|
|||||||
|
|
||||||
// ─── Auth Helper ─────────────────────────────────────────────────────────────────
|
// ─── Auth Helper ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function requireAuth(req: Request, apiKey: string | undefined): Response | null {
|
function requireAuth(req: Request): Response | null {
|
||||||
if (!apiKey) return null; // auth disabled
|
|
||||||
const header = req.headers.get("authorization") ?? req.headers.get("x-api-key") ?? "";
|
const header = req.headers.get("authorization") ?? req.headers.get("x-api-key") ?? "";
|
||||||
const key = header.replace(/^Bearer\s+/i, "").trim();
|
const key = header.replace(/^Bearer\s+/i, "").trim();
|
||||||
if (key === apiKey) return null;
|
if (key === "sk-dummy-key") return null;
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ error: { message: "Unauthorized", type: "auth_error" } }),
|
JSON.stringify({ error: { message: "Unauthorized", type: "auth_error" } }),
|
||||||
{ status: 401, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } },
|
{ status: 401, headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" } },
|
||||||
@@ -411,7 +410,7 @@ export default {
|
|||||||
if (url.pathname === "/v1/chat/completions") {
|
if (url.pathname === "/v1/chat/completions") {
|
||||||
if (req.method === "OPTIONS") return createCorsPreflightResponse();
|
if (req.method === "OPTIONS") return createCorsPreflightResponse();
|
||||||
if (req.method !== "POST") return new Response("Method Not Allowed", { status: 405 });
|
if (req.method !== "POST") return new Response("Method Not Allowed", { status: 405 });
|
||||||
const authErr = requireAuth(req, env.API_KEY);
|
const authErr = requireAuth(req);
|
||||||
if (authErr) return authErr;
|
if (authErr) return authErr;
|
||||||
try {
|
try {
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
@@ -428,7 +427,7 @@ export default {
|
|||||||
if (url.pathname === "/v1/messages") {
|
if (url.pathname === "/v1/messages") {
|
||||||
if (req.method === "OPTIONS") return createCorsPreflightResponse();
|
if (req.method === "OPTIONS") return createCorsPreflightResponse();
|
||||||
if (req.method !== "POST") return new Response("Method Not Allowed", { status: 405 });
|
if (req.method !== "POST") return new Response("Method Not Allowed", { status: 405 });
|
||||||
const authErr = requireAuth(req, env.API_KEY);
|
const authErr = requireAuth(req);
|
||||||
if (authErr) return authErr;
|
if (authErr) return authErr;
|
||||||
try {
|
try {
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
@@ -443,7 +442,7 @@ export default {
|
|||||||
|
|
||||||
// Models list
|
// Models list
|
||||||
if (url.pathname === "/v1/models" && req.method === "GET") {
|
if (url.pathname === "/v1/models" && req.method === "GET") {
|
||||||
const authErr = requireAuth(req, env.API_KEY);
|
const authErr = requireAuth(req);
|
||||||
if (authErr) return authErr;
|
if (authErr) return authErr;
|
||||||
const models = listModels().map((id) => ({
|
const models = listModels().map((id) => ({
|
||||||
id,
|
id,
|
||||||
|
|||||||
Reference in New Issue
Block a user