Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
danylopisarev06 committed Dec 24, 2023
1 parent 235b922 commit d0f0bff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 5 additions & 8 deletions app/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function getIP(req: NextRequest) {

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

return {
accessCode: isOpenAiKey ? "" : token.slice(ACCESS_CODE_PREFIX.length),
apiKey: isOpenAiKey ? token : "",
accessCode: isApiKey ? "" : token.slice(ACCESS_CODE_PREFIX.length),
apiKey: isApiKey ? token : "",
};
}

Expand Down Expand Up @@ -49,7 +49,7 @@ export function auth(req: NextRequest) {
if (serverConfig.hideUserApiKey && !!apiKey) {
return {
error: true,
msg: "you are not allowed to access openai with your own api key",
msg: "you are not allowed to access with your own api key",
};
}

Expand All @@ -64,10 +64,7 @@ export function auth(req: NextRequest) {

if (systemApiKey) {
console.log("[Auth] use system api key");
req.headers.set(
"Authorization",
`Bearer ${systemApiKey}`,
);
req.headers.set("Authorization", `Bearer ${systemApiKey}`);
} else {
console.log("[Auth] admin did not provide an api key");
}
Expand Down
13 changes: 13 additions & 0 deletions app/api/google/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,22 @@ async function handle(
10 * 60 * 1000,
);

const authResult = auth(req);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
}

const bearToken = req.headers.get("Authorization") ?? "";
const token = bearToken.trim().replaceAll("Bearer ", "").trim();

console.log(
bearToken,
serverConfig.googleApiKey,
token ? token : serverConfig.googleApiKey,
);

const key = token ? token : serverConfig.googleApiKey;
if (!key) {
return NextResponse.json(
Expand Down

0 comments on commit d0f0bff

Please sign in to comment.