@@ -2,44 +2,37 @@ import { invariantResponse } from '@epic-web/invariant'
22import { type Token } from '#types/helpers'
33import { type Route } from './+types/introspect'
44
5- export async function introspectLoader ( { request, context } : Route . LoaderArgs ) {
5+ export async function action ( { request, context } : Route . LoaderArgs ) {
66 const token = ( await request . formData ( ) ) . get ( 'token' ) ?. toString ( )
7- console . log ( { token } )
87 invariantResponse ( token , 'invalid_request' )
98
10- const info = await resolveTokenInfo ( token , context . cloudflare . env ) . catch (
11- ( ) => undefined ,
12- )
9+ const parts = token . split ( ':' )
10+ if ( parts . length !== 3 ) return { active : false }
11+
12+ const [ userId , grantId ] = parts
13+ const tokenId = await generateTokenId ( token )
14+ const tokenKey = `token:${ userId } :${ grantId } :${ tokenId } `
1315
14- if ( ! info ) return { active : false }
16+ const tokenData = await context . cloudflare . env . OAUTH_KV . get ( tokenKey , {
17+ type : 'json' ,
18+ } )
19+
20+ if ( ! tokenData ) return { active : false }
21+
22+ const info = tokenData as Token
23+
24+ if ( info . expiresAt < Date . now ( ) ) return { active : false }
1525
1626 return {
1727 active : true ,
1828 client_id : info . grant . clientId ,
1929 scope : info . grant . scope . join ( ' ' ) ,
2030 sub : info . userId ,
21- exp : Math . floor ( info . expiresAt / 1000 ) , // if you store ms
22- // aud, iss, token_type, iat ... add as useful
31+ exp : Math . floor ( info . expiresAt / 1000 ) ,
32+ iat : Math . floor ( info . createdAt / 1000 ) ,
2333 }
2434}
2535
26- async function resolveTokenInfo (
27- token : string ,
28- env : Env ,
29- ) : Promise < Token | undefined > {
30- const parts = token . split ( ':' )
31- if ( parts . length !== 3 ) throw new Error ( 'Invalid token format' )
32-
33- const [ userId , grantId ] = parts
34- const tokenId = await generateTokenId ( token )
35- const tokenKey = `token:${ userId } :${ grantId } :${ tokenId } `
36-
37- const tokenData = await env . OAUTH_KV . get ( tokenKey , { type : 'json' } )
38- if ( ! tokenData ) throw new Error ( 'Token not found' )
39-
40- return tokenData as Token
41- }
42-
4336// copied from @cloudflare /workers-oauth-provider
4437async function generateTokenId ( token : string ) {
4538 const encoder = new TextEncoder ( )
0 commit comments