-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathroute.ts
More file actions
58 lines (50 loc) · 1.86 KB
/
route.ts
File metadata and controls
58 lines (50 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { apiHandler } from '@/lib/apiHandler';
import { requestBodySchemaValidationError, serviceErrorResponse } from '@/lib/serviceError';
import { prisma } from '@/prisma';
import { hasEntitlement } from '@sourcebot/shared';
import { NextRequest } from 'next/server';
import { z } from 'zod';
// RFC 7591: OAuth 2.0 Dynamic Client Registration
// @see: https://datatracker.ietf.org/doc/html/rfc7591
const registerRequestSchema = z.object({
client_name: z.string().min(1),
redirect_uris: z.array(z.string().url()).min(1),
logo_uri: z.string().nullish(),
});
export const POST = apiHandler(async (request: NextRequest) => {
if (!hasEntitlement('oauth')) {
return Response.json(
{ error: 'access_denied', error_description: 'OAuth is not available on this plan.' },
{ status: 403 }
);
}
const body = await request.json();
const parsed = registerRequestSchema.safeParse(body);
if (!parsed.success) {
return serviceErrorResponse(requestBodySchemaValidationError(parsed.error));
}
const { client_name, redirect_uris, logo_uri } = parsed.data;
// Reject wildcard redirect URIs per security best practices
if (redirect_uris.some((uri) => uri.includes('*'))) {
return Response.json(
{ error: 'invalid_redirect_uri', error_description: 'Wildcard redirect URIs are not allowed.' },
{ status: 400 }
);
}
const client = await prisma.oAuthClient.create({
data: {
name: client_name,
logoUri: logo_uri ?? null,
redirectUris: redirect_uris,
},
});
return Response.json(
{
client_id: client.id,
client_name: client.name,
...(client.logoUri && { logo_uri: client.logoUri }),
redirect_uris: client.redirectUris,
},
{ status: 201 }
);
});