forked from ChatGPTNextWeb/NextChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: merge token and access code
- Loading branch information
Showing
8 changed files
with
119 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { NextRequest } from "next/server"; | ||
import { getServerSideConfig } from "../config/server"; | ||
import md5 from "spark-md5"; | ||
|
||
const serverConfig = getServerSideConfig(); | ||
|
||
function getIP(req: NextRequest) { | ||
let ip = req.ip ?? req.headers.get("x-real-ip"); | ||
const forwardedFor = req.headers.get("x-forwarded-for"); | ||
|
||
if (!ip && forwardedFor) { | ||
ip = forwardedFor.split(",").at(0) ?? ""; | ||
} | ||
|
||
return ip; | ||
} | ||
|
||
function parseApiKey(bearToken: string) { | ||
const token = bearToken.trim().replaceAll("Bearer ", "").trim(); | ||
const isOpenAiKey = token.startsWith("sk-"); | ||
|
||
return { | ||
accessCode: isOpenAiKey ? "" : token, | ||
apiKey: isOpenAiKey ? token : "", | ||
}; | ||
} | ||
|
||
export function auth(req: NextRequest) { | ||
const authToken = req.headers.get("Authorization") ?? ""; | ||
|
||
// check if it is openai api key or user token | ||
const { accessCode, apiKey: token } = parseApiKey(authToken); | ||
|
||
const hashedCode = md5.hash(accessCode ?? "").trim(); | ||
|
||
console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]); | ||
console.log("[Auth] got access code:", accessCode); | ||
console.log("[Auth] hashed access code:", hashedCode); | ||
console.log("[User IP] ", getIP(req)); | ||
console.log("[Time] ", new Date().toLocaleString()); | ||
|
||
if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !token) { | ||
return { | ||
error: true, | ||
needAccessCode: true, | ||
msg: "Please go settings page and fill your access code.", | ||
}; | ||
} | ||
|
||
// if user does not provide an api key, inject system api key | ||
if (!token) { | ||
const apiKey = serverConfig.apiKey; | ||
if (apiKey) { | ||
console.log("[Auth] use system api key"); | ||
req.headers.set("Authorization", `Bearer ${apiKey}`); | ||
} else { | ||
console.log("[Auth] admin did not provide an api key"); | ||
return { | ||
error: true, | ||
msg: "Empty Api Key", | ||
}; | ||
} | ||
} else { | ||
console.log("[Auth] use user api key"); | ||
} | ||
|
||
return { | ||
error: false, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ const nextConfig = { | |
output: "standalone", | ||
}; | ||
|
||
module.exports = nextConfig; | ||
export default nextConfig; |