-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
232 lines (200 loc) · 7.57 KB
/
route.ts
File metadata and controls
232 lines (200 loc) · 7.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { type NextRequest } from 'next/server'
import { cookies } from 'next/headers'
import { db } from '@/lib/db/client'
import { users, accounts, tasks, connectors, keys } from '@/lib/db/schema'
import { eq, and } from 'drizzle-orm'
import { getAppBaseUrl, getGitHubClientId } from '@/lib/auth/oauth'
import { createGitHubSession, saveSession } from '@/lib/session/create-github'
import { encrypt } from '@/lib/crypto'
import { generateId } from '@/lib/utils/id'
import {
GITHUB_AUTH_ERROR_MESSAGE_TYPE,
GITHUB_AUTH_POPUP_COOKIE,
GITHUB_AUTH_POPUP_VALUE,
GITHUB_AUTH_SUCCESS_MESSAGE_TYPE,
} from '@/lib/auth/github-popup-contract'
const GITHUB_AUTH_COOKIES = [
GITHUB_AUTH_POPUP_COOKIE,
'github_auth_state',
'github_auth_redirect_to',
'github_auth_mode',
'github_auth_user_id',
'github_oauth_state',
'github_oauth_redirect_to',
'github_oauth_user_id',
] as const
type CookieStore = Awaited<ReturnType<typeof cookies>>
type PopupStatus = 'success' | 'error'
function cleanupGitHubAuthCookies(cookieStore: CookieStore): void {
for (const cookieName of GITHUB_AUTH_COOKIES) {
cookieStore.delete(cookieName)
}
}
function createGitHubPopupResponse(req: NextRequest, status: PopupStatus, responseInit?: ResponseInit): Response {
const origin = new URL(getAppBaseUrl(req)).origin
const messageType = status === 'success' ? GITHUB_AUTH_SUCCESS_MESSAGE_TYPE : GITHUB_AUTH_ERROR_MESSAGE_TYPE
const html = `<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>GitHub Authentication</title>
</head>
<body>
<script>
window.opener?.postMessage({ type: ${JSON.stringify(messageType)}, status: ${JSON.stringify(status)} }, ${JSON.stringify(origin)});
window.close();
</script>
</body>
</html>`
return new Response(html, {
status: responseInit?.status ?? 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-store',
...responseInit?.headers,
},
})
}
export async function GET(req: NextRequest): Promise<Response> {
const code = req.nextUrl.searchParams.get('code')
const state = req.nextUrl.searchParams.get('state')
const cookieStore = await cookies()
const popupCookie = cookieStore.get(GITHUB_AUTH_POPUP_COOKIE)?.value ?? null
if (popupCookie !== GITHUB_AUTH_POPUP_VALUE) {
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 400 })
}
const authMode = cookieStore.get('github_auth_mode')?.value ?? null
const isSignInFlow = authMode === 'signin'
const isConnectFlow = authMode === 'connect'
const storedState = cookieStore.get('github_auth_state')?.value ?? null
const storedRedirectTo = cookieStore.get('github_auth_redirect_to')?.value ?? null
const storedUserId = cookieStore.get('github_auth_user_id')?.value ?? null
if (code === null || state === null || storedState !== state || storedRedirectTo === null) {
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 400 })
}
if ((!isSignInFlow && !isConnectFlow) || (isConnectFlow && storedUserId === null)) {
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 400 })
}
const clientId = getGitHubClientId()
const clientSecret = process.env.GITHUB_CLIENT_SECRET
if (!clientId || !clientSecret) {
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 500 })
}
try {
console.info('GitHub OAuth callback started')
const tokenResponse = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
code: code,
}),
})
if (!tokenResponse.ok) {
console.error('GitHub OAuth token exchange failed')
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 400 })
}
const tokenData = (await tokenResponse.json()) as {
access_token?: string
scope?: string
token_type?: string
error?: string
error_description?: string
}
if (!tokenData.access_token) {
console.error('GitHub OAuth access token missing')
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 400 })
}
const userResponse = await fetch('https://api.github.com/user', {
headers: {
Authorization: `Bearer ${tokenData.access_token}`,
Accept: 'application/vnd.github.v3+json',
},
})
if (!userResponse.ok) {
console.error('GitHub OAuth user fetch failed')
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 400 })
}
const githubUser = (await userResponse.json()) as {
login: string
id: number
}
if (isSignInFlow) {
const session = await createGitHubSession(tokenData.access_token, tokenData.scope)
if (!session) {
console.error('GitHub OAuth session creation failed')
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 500 })
}
const response = createGitHubPopupResponse(req, 'success')
await saveSession(response, session)
cleanupGitHubAuthCookies(cookieStore)
return response
}
const encryptedToken = encrypt(tokenData.access_token)
const existingAccount = await db
.select()
.from(accounts)
.where(and(eq(accounts.provider, 'github'), eq(accounts.externalUserId, `${githubUser.id}`)))
.limit(1)
if (existingAccount.length > 0) {
const connectedUserId = existingAccount[0].userId
if (connectedUserId !== storedUserId) {
console.info('GitHub OAuth account merge started')
await db.update(tasks).set({ userId: storedUserId! }).where(eq(tasks.userId, connectedUserId))
await db.update(connectors).set({ userId: storedUserId! }).where(eq(connectors.userId, connectedUserId))
await db.update(accounts).set({ userId: storedUserId! }).where(eq(accounts.userId, connectedUserId))
await db.update(keys).set({ userId: storedUserId! }).where(eq(keys.userId, connectedUserId))
await db.delete(users).where(eq(users.id, connectedUserId))
console.info('GitHub OAuth account merge completed')
await db
.update(accounts)
.set({
userId: storedUserId!,
accessToken: encryptedToken,
scope: tokenData.scope,
username: githubUser.login,
updatedAt: new Date(),
})
.where(eq(accounts.id, existingAccount[0].id))
} else {
await db
.update(accounts)
.set({
accessToken: encryptedToken,
scope: tokenData.scope,
username: githubUser.login,
updatedAt: new Date(),
})
.where(eq(accounts.id, existingAccount[0].id))
}
} else {
await db.insert(accounts).values({
id: generateId(21),
userId: storedUserId!,
provider: 'github',
externalUserId: `${githubUser.id}`,
accessToken: encryptedToken,
scope: tokenData.scope,
username: githubUser.login,
})
}
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'success')
} catch {
console.error('GitHub OAuth callback failed')
cleanupGitHubAuthCookies(cookieStore)
return createGitHubPopupResponse(req, 'error', { status: 500 })
}
}