mirror of
https://github.com/musistudio/claude-code-router.git
synced 2026-05-02 12:10:07 +00:00
move to monorepo
This commit is contained in:
parent
a7e20325db
commit
6a20b2021d
107 changed files with 5308 additions and 1118 deletions
57
packages/server/src/middleware/auth.ts
Normal file
57
packages/server/src/middleware/auth.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import { FastifyRequest, FastifyReply } from "fastify";
|
||||
|
||||
export const apiKeyAuth =
|
||||
(config: any) =>
|
||||
async (req: FastifyRequest, reply: FastifyReply, done: () => void) => {
|
||||
// Public endpoints that don't require authentication
|
||||
if (["/", "/health"].includes(req.url) || req.url.startsWith("/ui")) {
|
||||
return done();
|
||||
}
|
||||
|
||||
// Check if Providers is empty or not configured
|
||||
const providers = config.Providers || config.providers || [];
|
||||
if (!providers || providers.length === 0) {
|
||||
// No providers configured, skip authentication
|
||||
return done();
|
||||
}
|
||||
|
||||
const apiKey = config.APIKEY;
|
||||
if (!apiKey) {
|
||||
// If no API key is set, enable CORS for local
|
||||
const allowedOrigins = [
|
||||
`http://127.0.0.1:${config.PORT || 3456}`,
|
||||
`http://localhost:${config.PORT || 3456}`,
|
||||
];
|
||||
if (req.headers.origin && !allowedOrigins.includes(req.headers.origin)) {
|
||||
reply.status(403).send("CORS not allowed for this origin");
|
||||
return;
|
||||
} else {
|
||||
reply.header('Access-Control-Allow-Origin', `http://127.0.0.1:${config.PORT || 3456}`);
|
||||
reply.header('Access-Control-Allow-Origin', `http://localhost:${config.PORT || 3456}`);
|
||||
}
|
||||
return done();
|
||||
}
|
||||
|
||||
const authHeaderValue =
|
||||
req.headers.authorization || req.headers["x-api-key"];
|
||||
const authKey: string = Array.isArray(authHeaderValue)
|
||||
? authHeaderValue[0]
|
||||
: authHeaderValue || "";
|
||||
if (!authKey) {
|
||||
reply.status(401).send("APIKEY is missing");
|
||||
return;
|
||||
}
|
||||
let token = "";
|
||||
if (authKey.startsWith("Bearer")) {
|
||||
token = authKey.split(" ")[1];
|
||||
} else {
|
||||
token = authKey;
|
||||
}
|
||||
|
||||
if (token !== apiKey) {
|
||||
reply.status(401).send("Invalid API key");
|
||||
return;
|
||||
}
|
||||
|
||||
done();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue