-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathroute.ts
More file actions
103 lines (88 loc) · 3.57 KB
/
Copy pathroute.ts
File metadata and controls
103 lines (88 loc) · 3.57 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { verifyAndExchangeCode, verifyAndRotateRefreshToken } from '@/ee/features/oauth/server';
import { oauthApiHandler } from '@/ee/features/oauth/apiHandler';
import { env, hasEntitlement } from '@sourcebot/shared';
import { NextRequest } from 'next/server';
import { OAUTH_NOT_SUPPORTED_ERROR_MESSAGE } from '@/ee/features/oauth/constants';
// OAuth 2.0 Token Endpoint
// Supports grant_type=authorization_code with PKCE (RFC 7636).
// @see: https://datatracker.ietf.org/doc/html/rfc6749#section-3.2
export const POST = oauthApiHandler(async (request: NextRequest) => {
if (!hasEntitlement('oauth')) {
return Response.json(
{ error: 'access_denied', error_description: OAUTH_NOT_SUPPORTED_ERROR_MESSAGE },
{ status: 403 }
);
}
const formData = await request.formData();
const grantType = formData.get('grant_type');
const clientId = formData.get('client_id');
const resource = formData.get('resource');
if (!clientId) {
return Response.json(
{ error: 'invalid_request', error_description: 'Missing required parameter: client_id.' },
{ status: 400 }
);
}
if (grantType === 'authorization_code') {
const code = formData.get('code');
const redirectUri = formData.get('redirect_uri');
const codeVerifier = formData.get('code_verifier');
if (!code || !redirectUri || !codeVerifier) {
return Response.json(
{ error: 'invalid_request', error_description: 'Missing required parameters: code, redirect_uri, code_verifier.' },
{ status: 400 }
);
}
const result = await verifyAndExchangeCode({
rawCode: code.toString(),
clientId: clientId.toString(),
redirectUri: redirectUri.toString(),
codeVerifier: codeVerifier.toString(),
resource: resource ? resource.toString() : null,
});
if ('error' in result) {
return Response.json(
{ error: result.error, error_description: result.errorDescription },
{ status: 400 }
);
}
return Response.json({
access_token: result.token,
refresh_token: result.refreshToken,
token_type: 'Bearer',
expires_in: env.OAUTH_ACCESS_TOKEN_TTL_SECONDS,
scope: '',
});
}
if (grantType === 'refresh_token') {
const rawRefreshToken = formData.get('refresh_token');
if (!rawRefreshToken) {
return Response.json(
{ error: 'invalid_request', error_description: 'Missing required parameter: refresh_token.' },
{ status: 400 }
);
}
const result = await verifyAndRotateRefreshToken({
rawRefreshToken: rawRefreshToken.toString(),
clientId: clientId.toString(),
resource: resource ? resource.toString() : null,
});
if ('error' in result) {
return Response.json(
{ error: result.error, error_description: result.errorDescription },
{ status: 400 }
);
}
return Response.json({
access_token: result.token,
refresh_token: result.refreshToken,
token_type: 'Bearer',
expires_in: env.OAUTH_ACCESS_TOKEN_TTL_SECONDS,
scope: '',
});
}
return Response.json(
{ error: 'unsupported_grant_type', error_description: 'Supported grant types: authorization_code, refresh_token.' },
{ status: 400 }
);
});