|
| 1 | +import { invariantResponse } from '@epic-web/invariant' |
1 | 2 | import { type Token } from '#types/helpers' |
2 | 3 | import { type Route } from './+types/introspect' |
3 | 4 |
|
4 | | -export async function loader({ request, context }: Route.LoaderArgs) { |
5 | | - const tokenInfo = await getTokenInfo(request, context.cloudflare.env) |
6 | | - if (!tokenInfo) return new Response('Unauthorized', { status: 401 }) |
7 | | - |
8 | | - return Response.json({ |
9 | | - userId: tokenInfo.userId, |
10 | | - clientId: tokenInfo.grant.clientId, |
11 | | - scopes: tokenInfo.grant.scope, |
12 | | - expiresAt: tokenInfo.expiresAt, |
13 | | - }) |
14 | | -} |
15 | | - |
16 | | -async function getTokenInfo( |
17 | | - request: Request, |
18 | | - env: Env, |
19 | | -): Promise<Token | undefined> { |
20 | | - const token = request.headers.get('authorization')?.slice('Bearer '.length) |
21 | | - if (!token) return undefined |
22 | | - return resolveTokenInfo(token, env) |
| 5 | +export async function introspectLoader({ request, context }: Route.LoaderArgs) { |
| 6 | + const token = (await request.formData()).get('token')?.toString() |
| 7 | + console.log({ token }) |
| 8 | + invariantResponse(token, 'invalid_request') |
| 9 | + |
| 10 | + const info = await resolveTokenInfo(token, context.cloudflare.env).catch( |
| 11 | + () => undefined, |
| 12 | + ) |
| 13 | + |
| 14 | + if (!info) return { active: false } |
| 15 | + |
| 16 | + return { |
| 17 | + active: true, |
| 18 | + client_id: info.grant.clientId, |
| 19 | + scope: info.grant.scope.join(' '), |
| 20 | + sub: info.userId, |
| 21 | + exp: Math.floor(info.expiresAt / 1000), // if you store ms |
| 22 | + // aud, iss, token_type, iat ... add as useful |
| 23 | + } |
23 | 24 | } |
24 | 25 |
|
25 | 26 | async function resolveTokenInfo( |
|
0 commit comments