Skip to content

Commit de29a28

Browse files
committed
separate code jwt input and output schemas
1 parent 16b7591 commit de29a28

5 files changed

Lines changed: 75 additions & 32 deletions

File tree

packages/app/control/src/app/(auth)/(oauth)/(authorize)/_lib/authorize.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import { z } from 'zod';
33
import { nanoid } from 'nanoid';
44

55
import { auth } from '@/auth';
6-
import { authCodeJwtBodySchema, createAuthCodeJwt } from './code';
6+
import { authCodeJwtInputSchema, createAuthCodeJwt } from './code';
77

8-
export const authorizeParamsSchema = authCodeJwtBodySchema
8+
export const authorizeParamsSchema = authCodeJwtInputSchema
99
.omit({
10-
code: true,
11-
exp: true,
12-
iat: true,
1310
user_id: true,
1411
})
1512
.extend({

packages/app/control/src/app/(auth)/(oauth)/(authorize)/_lib/code.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { addSeconds, getUnixTime } from 'date-fns';
1010
const AUTH_CODE_TTL = 300; // 5 minutes
1111
const JWT_SECRET = new TextEncoder().encode(env.OAUTH_CODE_SIGNING_JWT_SECRET);
1212

13-
export const authCodeJwtBodySchema = z.object({
13+
export const authCodeJwtInputSchema = z.object({
1414
client_id: z.uuid('client_id must be a valid UUID'),
1515
user_id: z.uuid('user_id must be a valid UUID'),
1616
redirect_uri: z.url('redirect_uri must be a valid URL'),
@@ -25,20 +25,26 @@ export const authCodeJwtBodySchema = z.object({
2525
error: 'Only S256 code challenge method is supported',
2626
}),
2727
scope: z.string('missing scope').default('llm:invoke offline_access'),
28-
code: z.string().default(nanoid(32)),
29-
exp: z
30-
.number()
31-
.default(() => getUnixTime(addSeconds(new Date(), AUTH_CODE_TTL))),
32-
iat: z.number().default(() => getUnixTime(new Date())),
3328
});
3429

3530
export const createAuthCodeJwt = async (
36-
payloadInput: z.input<typeof authCodeJwtBodySchema>
31+
payloadInput: z.input<typeof authCodeJwtInputSchema>
3732
) => {
38-
const payload = authCodeJwtBodySchema.parse(payloadInput);
39-
const authCodeJwt = await new SignJWT(payload)
33+
const input = authCodeJwtInputSchema.parse(payloadInput);
34+
const authCodeJwt = await new SignJWT({
35+
...input,
36+
code: nanoid(32),
37+
})
4038
.setProtectedHeader({ alg: 'HS256' })
39+
.setExpirationTime(getUnixTime(addSeconds(new Date(), AUTH_CODE_TTL)))
40+
.setIssuedAt(getUnixTime(new Date()))
4141
.sign(JWT_SECRET);
4242

4343
return authCodeJwt;
4444
};
45+
46+
export const authCodeJwtPayloadSchema = authCodeJwtInputSchema.extend({
47+
code: z.string(),
48+
exp: z.number(),
49+
iat: z.number(),
50+
});

packages/app/control/src/app/(auth)/(oauth)/api/oauth/token/_lib/create-jwt.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { addSeconds, getUnixTime } from 'date-fns';
88

99
import { env } from '@/env';
1010
import { logger } from '@/logger';
11-
import { Prisma } from '@/generated/prisma';
1211

1312
const API_ECHO_ACCESS_JWT_SECRET = new TextEncoder().encode(
1413
env.API_ECHO_ACCESS_JWT_SECRET

packages/app/control/src/app/(auth)/(oauth)/api/oauth/token/_lib/issue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import z from 'zod';
33
import { createHash } from 'crypto';
44
import { jwtVerify } from 'jose';
55

6-
import { authCodeJwtBodySchema } from '@/app/(auth)/(oauth)/(authorize)/_lib/code';
6+
import { authCodeJwtPayloadSchema } from '@/app/(auth)/(oauth)/(authorize)/_lib/code';
77
import { isValidRedirectUri } from '@/app/(auth)/(oauth)/_lib/redirect-uri';
88

99
import { db } from '@/lib/db';
@@ -57,7 +57,7 @@ export async function handleIssueToken(
5757
code_challenge,
5858
user_id,
5959
scope,
60-
} = authCodeJwtBodySchema.parse(payload);
60+
} = authCodeJwtPayloadSchema.parse(payload);
6161

6262
/* 4️⃣ Validate the authorization code data */
6363
if (codeClientId !== client_id || codeRedirectUri !== redirect_uri) {

pnpm-lock.yaml

Lines changed: 56 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)