Skip to content

Commit a04d32b

Browse files
committed
refactor: use new introspection endpoint
1 parent 87c96b2 commit a04d32b

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

lib/auth.ts

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { CombinedGraphQLErrors } from "@apollo/client";
21
import { getIronSession } from "iron-session";
32
import { cookies } from "next/headers";
4-
import { makeClient } from "./apollo";
3+
import z from "zod";
54
import buildUri from "./build-uri";
6-
import { BASIC_USER_INFO_QUERY, type BasicUserInfo, isAdmin } from "./user";
75

86
// Constants for OAuth 2.0 PKCE flow
97
export const OAUTH_CONFIG = {
@@ -225,45 +223,77 @@ export interface AuthStatus {
225223
loggedIn: boolean;
226224

227225
role?: "admin" | "user";
228-
user?: BasicUserInfo;
226+
introspectResult?: z.infer<typeof introspectSchema>;
229227
}
230228

229+
export const introspectSchema = z.union([
230+
z.object({
231+
active: z.literal(true),
232+
scope: z.string().transform((scope) => scope.split(" ")).describe("the scopes of the token"),
233+
sub: z.string().describe("the subject of the token"),
234+
exp: z.number().describe("the time the token expires"),
235+
iat: z.number().describe("the time the token was issued"),
236+
azp: z.string().describe("the machine that is authorized to use this token"),
237+
}),
238+
z.object({
239+
active: z.literal(false),
240+
}),
241+
]);
242+
231243
export async function getAuthStatus(): Promise<AuthStatus> {
232244
const token = await getAuthToken();
233245
if (!token) {
234246
return {
235247
loggedIn: false,
248+
introspectResult: undefined,
236249
};
237250
}
238251

239252
// get user info
240-
const client = makeClient({ token });
241-
242-
try {
243-
const { data } = await client.query({
244-
query: BASIC_USER_INFO_QUERY,
245-
});
246-
if (!data) {
247-
return {
248-
loggedIn: true,
249-
};
250-
}
253+
const response = await fetch(buildUri("/api/auth/v2/introspect"), {
254+
method: "POST",
255+
headers: {
256+
"Content-Type": "application/x-www-form-urlencoded",
257+
},
258+
body: new URLSearchParams({
259+
token,
260+
token_type_hint: "access_token",
261+
}),
262+
});
251263

264+
if (!response.ok) {
265+
console.error("Error validating auth:", response.status, response.statusText);
252266
return {
253-
role: isAdmin(data?.me) ? "admin" : "user",
254-
user: data.me,
255-
loggedIn: true,
267+
loggedIn: false,
268+
introspectResult: undefined,
256269
};
257-
} catch (error) {
258-
if (CombinedGraphQLErrors.is(error) && error.message === "require authentication") {
259-
return {
260-
loggedIn: false,
261-
};
262-
}
263-
264-
console.log("Error validating auth:", error);
270+
}
271+
272+
const data = await response.json();
273+
const parsedData = introspectSchema.safeParse(data);
274+
275+
if (!parsedData.success) {
276+
console.error("Error validating auth:", parsedData.error);
265277
return {
266278
loggedIn: false,
279+
introspectResult: undefined,
267280
};
268281
}
282+
283+
if (!parsedData.data.active) {
284+
return {
285+
loggedIn: false,
286+
introspectResult: parsedData.data,
287+
};
288+
}
289+
290+
if (parsedData.data.scope.includes("*")) {
291+
return {
292+
loggedIn: true,
293+
role: "admin",
294+
introspectResult: parsedData.data,
295+
};
296+
}
297+
298+
return { loggedIn: true, role: "user", introspectResult: parsedData.data };
269299
}

lib/user.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ export const BASIC_USER_INFO_QUERY = graphql(`
1717
}
1818
}
1919
`);
20-
21-
export function isAdmin(user: BasicUserInfo): boolean {
22-
return user.group?.name === "admin";
23-
}

middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export async function middleware(request: NextRequest) {
3434
}
3535

3636
try {
37-
const { role, loggedIn } = await getAuthStatus();
37+
const { role, loggedIn, introspectResult } = await getAuthStatus();
38+
39+
console.log("introspectResult", introspectResult);
3840

3941
if (!loggedIn) {
4042
// Handle unauthenticated requests

0 commit comments

Comments
 (0)