Skip to content

Commit 1650072

Browse files
authored
Merge pull request #14 from database-playground/pan93412/dbp-88-任意使用者都能使用-ai
fix(chat): disallow users without "ai" scope to use AI
2 parents bdd0d89 + 1321a4a commit 1650072

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

app/api/chat/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ interface ChatRouteRequest {
7272
}
7373

7474
export async function POST(req: Request) {
75-
const authorized = await checkAuthorizedStatus();
75+
const authorized = await checkAuthorizedStatus(["*", "ai"]);
7676
if (!authorized) {
7777
return new NextResponse("Unauthorized", { status: 401 });
7878
}

lib/auth.rsc.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@ export async function redirectIfAuthenticated(): Promise<void> {
1717
redirect("/");
1818
}
1919

20-
export async function checkAuthorizedStatus(): Promise<boolean> {
20+
export async function checkAuthorizedStatus(requiredScopes?: string[]): Promise<boolean> {
2121
const token = await getAuthToken();
2222
if (!token) {
2323
return false;
2424
}
2525

26-
const loggedIn = await getAuthStatus(token)
27-
.then(result => result.loggedIn)
28-
.catch(() => false);
26+
const authStatus = await getAuthStatus(token);
27+
28+
if (!authStatus.loggedIn || !authStatus.introspectResult?.active) {
29+
return false;
30+
}
31+
32+
// check if the token has the required scope
33+
if (requiredScopes) {
34+
for (const scope of requiredScopes) {
35+
if (authStatus.introspectResult?.scope.includes(scope)) {
36+
return true;
37+
}
38+
}
2939

30-
if (!loggedIn) {
3140
return false;
3241
}
3342

0 commit comments

Comments
 (0)