makes gatekeeper compatible with sillytavern claude impl

This commit is contained in:
nai-degen 2023-06-06 20:53:50 -05:00
parent ace32c7e6e
commit a6a0c21f7d
1 changed files with 23 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import type { RequestHandler } from "express";
import type { Request, RequestHandler } from "express";
import { config } from "../../config";
import { authenticate, getUser } from "./user-store";
@ -6,9 +6,29 @@ const GATEKEEPER = config.gatekeeper;
const PROXY_KEY = config.proxyKey;
const ADMIN_KEY = config.adminKey;
function getProxyAuthorizationFromRequest(req: Request): string | undefined {
// Anthropic's API uses x-api-key instead of Authorization. Some clients will
// pass the _proxy_ key in this header too, instead of providing it as a
// Bearer token in the Authorization header. So we need to check both.
// Prefer the Authorization header if both are present.
if (req.headers.authorization) {
const token = req.headers.authorization?.slice("Bearer ".length);
delete req.headers.authorization;
return token;
}
if (req.headers["x-api-key"]) {
const token = req.headers["x-api-key"]?.toString();
delete req.headers["x-api-key"];
return token;
}
return undefined;
}
export const gatekeeper: RequestHandler = (req, res, next) => {
const token = req.headers.authorization?.slice("Bearer ".length);
delete req.headers.authorization;
const token = getProxyAuthorizationFromRequest(req);
// TODO: Generate anonymous users based on IP address for public or proxy_key
// modes so that all middleware can assume a user of some sort is present.