Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#3625 from fredliang44/main
Browse files Browse the repository at this point in the history
fix: fix auth key selection when using different model
  • Loading branch information
fredliang44 authored and H0llyW00dzZ committed Dec 24, 2023
1 parent dc7eb0d commit da944e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
21 changes: 14 additions & 7 deletions app/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest } from "next/server";
import { getServerSideConfig } from "../config/server";
import md5 from "spark-md5";
import { ACCESS_CODE_PREFIX } from "../constant";
import { ACCESS_CODE_PREFIX, ModelProvider } from "../constant";

function getIP(req: NextRequest) {
let ip = req.ip ?? req.headers.get("x-real-ip");
Expand Down Expand Up @@ -30,7 +30,7 @@ function doubleHashForDisplay(hashedCode: string): string {
return md5.hash(hashedCode);
}

export function auth(req: NextRequest) {
export function auth(req: NextRequest, modelProvider: ModelProvider) {
const authToken = req.headers.get("Authorization") ?? "";

// check if it is openai api key or user token
Expand Down Expand Up @@ -62,12 +62,19 @@ export function auth(req: NextRequest) {
// if user does not provide an api key, inject system api key
if (!apiKey) {
const serverConfig = getServerSideConfig();
const systemApiKey = serverConfig.isAzure
? serverConfig.azureApiKey
: serverConfig.isGoogle
? serverConfig.googleApiKey
: serverConfig.apiKey;

// const systemApiKey = serverConfig.isAzure
// ? serverConfig.azureApiKey
// : serverConfig.isGoogle
// ? serverConfig.googleApiKey
// : serverConfig.apiKey;

const systemApiKey =
modelProvider === ModelProvider.GeminiPro
? serverConfig.googleApiKey
: serverConfig.isAzure
? serverConfig.azureApiKey
: serverConfig.apiKey;
if (systemApiKey) {
console.log("[Auth] use system api key");
req.headers.set("Authorization", `Bearer ${systemApiKey}`);
Expand Down
6 changes: 3 additions & 3 deletions app/api/google/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { getServerSideConfig } from "@/app/config/server";
import { GEMINI_BASE_URL, Google } from "@/app/constant";
import { GEMINI_BASE_URL, Google, ModelProvider } from "@/app/constant";

async function handle(
req: NextRequest,
Expand Down Expand Up @@ -39,7 +39,7 @@ async function handle(
10 * 60 * 1000,
);

const authResult = auth(req);
const authResult = auth(req, ModelProvider.GeminiPro);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
Expand All @@ -50,6 +50,7 @@ async function handle(
const token = bearToken.trim().replaceAll("Bearer ", "").trim();

const key = token ? token : serverConfig.googleApiKey;

if (!key) {
return NextResponse.json(
{
Expand All @@ -63,7 +64,6 @@ async function handle(
}

const fetchUrl = `${baseUrl}/${path}?key=${key}`;

const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
Expand Down
28 changes: 23 additions & 5 deletions app/api/openai/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type OpenAIListModelResponse } from "@/app/client/platforms/openai";
import { getServerSideConfig } from "@/app/config/server";
import { OpenaiPath } from "@/app/constant";
import { ModelProvider, OpenaiPath } from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
Expand Down Expand Up @@ -55,7 +55,7 @@ async function handle(
}
return false;
}


const userAgent = req.headers.get("User-Agent");
const isRealDevice = isRealDevicez(userAgent);
Expand All @@ -70,9 +70,9 @@ async function handle(
status: 403,
},
);
}
}

const authResult = auth(req);
const authResult = auth(req, ModelProvider.GPT);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
Expand Down Expand Up @@ -102,4 +102,22 @@ export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = ['arn1', 'bom1', 'cdg1', 'cle1', 'cpt1', 'dub1', 'fra1', 'gru1', 'hnd1', 'iad1', 'icn1', 'kix1', 'lhr1', 'pdx1', 'sfo1', 'sin1', 'syd1'];
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

0 comments on commit da944e8

Please sign in to comment.