-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 refactor: refactor the route auth as a middleware
- Loading branch information
Showing
7 changed files
with
52 additions
and
22 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
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createErrorResponse } from '@/app/api/errorResponse'; | ||
import { JWTPayload, LOBE_CHAT_AUTH_HEADER, OAUTH_AUTHORIZED } from '@/const/auth'; | ||
import { AgentRuntimeError, ChatCompletionErrorPayload } from '@/libs/agent-runtime'; | ||
import { ChatErrorType } from '@/types/fetch'; | ||
|
||
import { checkAuthMethod, getJWTPayload } from './utils'; | ||
|
||
type RequestOptions = { params: { provider: string } }; | ||
|
||
export type RequestHandler = ( | ||
req: Request, | ||
options: RequestOptions & { jwtPayload: JWTPayload }, | ||
) => Promise<Response>; | ||
|
||
export const checkAuth = | ||
(handler: RequestHandler) => async (req: Request, options: RequestOptions) => { | ||
let jwtPayload: JWTPayload; | ||
|
||
try { | ||
// get Authorization from header | ||
const authorization = req.headers.get(LOBE_CHAT_AUTH_HEADER); | ||
const oauthAuthorized = !!req.headers.get(OAUTH_AUTHORIZED); | ||
|
||
if (!authorization) throw AgentRuntimeError.createError(ChatErrorType.Unauthorized); | ||
|
||
// check the Auth With payload | ||
jwtPayload = await getJWTPayload(authorization); | ||
checkAuthMethod(jwtPayload.accessCode, jwtPayload.apiKey, oauthAuthorized); | ||
} catch (e) { | ||
const { | ||
errorType = ChatErrorType.InternalServerError, | ||
error: errorContent, | ||
...res | ||
} = e as ChatCompletionErrorPayload; | ||
|
||
const error = errorContent || e; | ||
|
||
return createErrorResponse(errorType, { error, ...res, provider: options.params?.provider }); | ||
} | ||
|
||
return handler(req, { ...options, jwtPayload }); | ||
}; |
File renamed without changes.
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