Skip to content

Commit 70140b7

Browse files
committed
fix tests
1 parent cca96e7 commit 70140b7

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

epicshop/epic-me/app/routes/introspect.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,37 @@ import { invariantResponse } from '@epic-web/invariant'
22
import { type Token } from '#types/helpers'
33
import { 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
4437
async function generateTokenId(token: string) {
4538
const encoder = new TextEncoder()

epicshop/epic-me/workers/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const defaultHandler = {
1919
} satisfies ExportedHandler<Env>
2020

2121
const oauthProvider = new OAuthProvider({
22-
apiRoute: ['/whoami', '/db-api', '/introspect'],
22+
apiRoute: ['/whoami', '/db-api'],
2323
// @ts-expect-error these types are wrong...
2424
apiHandler: defaultHandler,
2525
// @ts-expect-error these types are wrong...

0 commit comments

Comments
 (0)