Skip to content

Commit

Permalink
fix: ChatGPTNextWeb#1233 detect api key with custom prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed May 4, 2023
1 parent 596c9b1 commit c2e79d2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextRequest } from "next/server";
import { getServerSideConfig } from "../config/server";
import md5 from "spark-md5";
import { ACCESS_CODE_PREFIX } from "../constant";

const serverConfig = getServerSideConfig();

Expand All @@ -17,10 +18,10 @@ function getIP(req: NextRequest) {

function parseApiKey(bearToken: string) {
const token = bearToken.trim().replaceAll("Bearer ", "").trim();
const isOpenAiKey = token.startsWith("sk-");
const isOpenAiKey = !token.startsWith(ACCESS_CODE_PREFIX);

return {
accessCode: isOpenAiKey ? "" : token,
accessCode: isOpenAiKey ? "" : token.slice(ACCESS_CODE_PREFIX.length),
apiKey: isOpenAiKey ? token : "",
};
}
Expand Down
2 changes: 2 additions & 0 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export enum StoreKey {
export const MAX_SIDEBAR_WIDTH = 500;
export const MIN_SIDEBAR_WIDTH = 230;
export const NARROW_SIDEBAR_WIDTH = 100;

export const ACCESS_CODE_PREFIX = "ak-";
5 changes: 4 additions & 1 deletion app/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useChatStore,
} from "./store";
import { showToast } from "./components/ui-lib";
import { ACCESS_CODE_PREFIX } from "./constant";

const TIME_OUT_MS = 60000;

Expand Down Expand Up @@ -58,7 +59,9 @@ function getHeaders() {
accessStore.enabledAccessControl() &&
validString(accessStore.accessCode)
) {
headers.Authorization = makeBearer(accessStore.accessCode);
headers.Authorization = makeBearer(
ACCESS_CODE_PREFIX + accessStore.accessCode,
);
}

return headers;
Expand Down

0 comments on commit c2e79d2

Please sign in to comment.