Эх сурвалжийг харах

fix: #1233 detect api key with custom prefix

Yidadaa 2 жил өмнө
parent
commit
c2e79d22d2

+ 3 - 2
app/api/auth.ts

@@ -1,6 +1,7 @@
 import { NextRequest } from "next/server";
 import { NextRequest } from "next/server";
 import { getServerSideConfig } from "../config/server";
 import { getServerSideConfig } from "../config/server";
 import md5 from "spark-md5";
 import md5 from "spark-md5";
+import { ACCESS_CODE_PREFIX } from "../constant";
 
 
 const serverConfig = getServerSideConfig();
 const serverConfig = getServerSideConfig();
 
 
@@ -17,10 +18,10 @@ function getIP(req: NextRequest) {
 
 
 function parseApiKey(bearToken: string) {
 function parseApiKey(bearToken: string) {
   const token = bearToken.trim().replaceAll("Bearer ", "").trim();
   const token = bearToken.trim().replaceAll("Bearer ", "").trim();
-  const isOpenAiKey = token.startsWith("sk-");
+  const isOpenAiKey = !token.startsWith(ACCESS_CODE_PREFIX);
 
 
   return {
   return {
-    accessCode: isOpenAiKey ? "" : token,
+    accessCode: isOpenAiKey ? "" : token.slice(ACCESS_CODE_PREFIX.length),
     apiKey: isOpenAiKey ? token : "",
     apiKey: isOpenAiKey ? token : "",
   };
   };
 }
 }

+ 2 - 0
app/constant.ts

@@ -36,3 +36,5 @@ export enum StoreKey {
 export const MAX_SIDEBAR_WIDTH = 500;
 export const MAX_SIDEBAR_WIDTH = 500;
 export const MIN_SIDEBAR_WIDTH = 230;
 export const MIN_SIDEBAR_WIDTH = 230;
 export const NARROW_SIDEBAR_WIDTH = 100;
 export const NARROW_SIDEBAR_WIDTH = 100;
+
+export const ACCESS_CODE_PREFIX = "ak-";

+ 4 - 1
app/requests.ts

@@ -8,6 +8,7 @@ import {
   useChatStore,
   useChatStore,
 } from "./store";
 } from "./store";
 import { showToast } from "./components/ui-lib";
 import { showToast } from "./components/ui-lib";
+import { ACCESS_CODE_PREFIX } from "./constant";
 
 
 const TIME_OUT_MS = 60000;
 const TIME_OUT_MS = 60000;
 
 
@@ -58,7 +59,9 @@ function getHeaders() {
     accessStore.enabledAccessControl() &&
     accessStore.enabledAccessControl() &&
     validString(accessStore.accessCode)
     validString(accessStore.accessCode)
   ) {
   ) {
-    headers.Authorization = makeBearer(accessStore.accessCode);
+    headers.Authorization = makeBearer(
+      ACCESS_CODE_PREFIX + accessStore.accessCode,
+    );
   }
   }
 
 
   return headers;
   return headers;