-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.ts
More file actions
512 lines (422 loc) · 12.7 KB
/
Copy pathworker.ts
File metadata and controls
512 lines (422 loc) · 12.7 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import configData from './config.json'
import { Hono } from 'hono'
import * as jose from 'jose'
interface AppConfig {
clientId: string
clientSecret: string
redirectUrls: string[]
serversToCheckRolesFor?: string[]
cacheRoles: boolean
includeEmail?: boolean
fallbackEmail?: string
}
interface StoredKeyPair {
publicKey: JsonWebKey
privateKey: JsonWebKey
}
interface DiscordTokenSuccessResponse {
access_token: string
token_type: string
expires_in: number
refresh_token?: string
scope: string
}
interface DiscordErrorResponse {
error?: string
error_description?: string
message?: string
}
interface DiscordUser {
id: string
email?: string | null
verified?: boolean
[key: string]: unknown
}
interface DiscordGuildSummary {
id: string
[key: string]: unknown
}
interface DiscordGuildMember {
roles?: string[]
user?: {
id?: string
}
[key: string]: unknown
}
type RoleCacheRecord = Record<string, string[]>
type RoleClaims = Record<`roles:${string}`, string[]>
type ScopeMode = 'identify' | 'email' | 'guilds' | 'roles'
type AppEnv = {
Bindings: Env
}
const config: AppConfig = configData
const app = new Hono<AppEnv>()
const SIGNING_ALGORITHM: RsaHashedKeyGenParams = {
name: 'RSASSA-PKCS1-v1_5',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-256' },
}
const IMPORT_ALGORITHM: RsaHashedImportParams = {
name: 'RSASSA-PKCS1-v1_5',
hash: { name: 'SHA-256' },
}
const DISCORD_USER_AGENT =
'DiscordBot (https://github.com/Aiko-IT-Systems/cloudflare-discord-oidc-worker, v4.0.0)'
function isScopeMode(value: string): value is ScopeMode {
return value === 'identify' || value === 'email' || value === 'guilds' || value === 'roles'
}
function shouldIncludeEmail(): boolean {
return config.includeEmail ?? true
}
function getFallbackEmail(): string {
return config.fallbackEmail ?? 'oauth@discord.com'
}
function getScopesForMode(mode: ScopeMode): string {
const scopes = ['identify']
if (shouldIncludeEmail()) {
scopes.push('email')
}
switch (mode) {
case 'identify':
case 'email':
return scopes.join(' ')
case 'guilds':
scopes.push('guilds')
return scopes.join(' ')
case 'roles':
scopes.push('guilds', 'guilds.members.read')
return scopes.join(' ')
}
}
function getConfiguredGuildIds(): string[] {
return Array.isArray(config.serversToCheckRolesFor) ? config.serversToCheckRolesFor : []
}
function isCryptoKeyPair(value: CryptoKey | CryptoKeyPair): value is CryptoKeyPair {
return 'publicKey' in value && 'privateKey' in value
}
function getStringValue(
value: FormDataEntryValue | File | string | null | undefined,
): string | null {
if (typeof value === 'string' && value.length > 0) {
return value
}
return null
}
function getDiscordHeaders(token: string, kind: 'Bearer' | 'Bot'): HeadersInit {
return {
Authorization: `${kind} ${token}`,
'User-Agent': DISCORD_USER_AGENT,
}
}
async function readJson<T>(response: Response): Promise<T> {
return (await response.json()) as T
}
function logDiscordError(prefix: string, payload: DiscordErrorResponse | null): void {
if (!payload) {
console.error(prefix)
return
}
console.error(prefix, {
error: payload.error,
error_description: payload.error_description,
message: payload.message,
})
}
async function loadOrGenerateKeyPair(kv: KVNamespace): Promise<CryptoKeyPair> {
const storedKeyPair = await kv.get<StoredKeyPair>('keys', 'json')
if (storedKeyPair) {
return {
publicKey: await crypto.subtle.importKey(
'jwk',
storedKeyPair.publicKey,
IMPORT_ALGORITHM,
true,
['verify'],
),
privateKey: await crypto.subtle.importKey(
'jwk',
storedKeyPair.privateKey,
IMPORT_ALGORITHM,
true,
['sign'],
),
}
}
const generatedKeys = await crypto.subtle.generateKey(
SIGNING_ALGORITHM,
true,
['sign', 'verify'],
)
if (!isCryptoKeyPair(generatedKeys)) {
throw new Error('Expected crypto.subtle.generateKey() to return a CryptoKeyPair')
}
await kv.put(
'keys',
JSON.stringify({
privateKey: await crypto.subtle.exportKey('jwk', generatedKeys.privateKey),
publicKey: await crypto.subtle.exportKey('jwk', generatedKeys.publicKey),
} satisfies StoredKeyPair),
)
return generatedKeys
}
async function getRolesFromCacheFor(
env: Env,
guildId: string,
memberId: string,
): Promise<string[] | null> {
const memberRoleCache = await env.KV.get<RoleCacheRecord>(`roles:${guildId}`, 'json')
if (memberRoleCache && memberId in memberRoleCache) {
return memberRoleCache[memberId] ?? null
}
return null
}
function getRetryDelayMs(response: Response): number {
const retryAfterHeader = response.headers.get('Retry-After')
if (!retryAfterHeader) {
return 10_000
}
const retryAfterSeconds = Number(retryAfterHeader)
return Number.isFinite(retryAfterSeconds) ? retryAfterSeconds * 1000 : 10_000
}
async function sleep(ms: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, ms))
}
async function fetchGuildMemberPage(
env: Env,
guildId: string,
after?: string,
): Promise<DiscordGuildMember[]> {
for (let attempt = 1; attempt <= 2; attempt += 1) {
const params = new URLSearchParams({ limit: '1000' })
if (after) {
params.set('after', after)
}
const response = await fetch(
`https://discord.com/api/guilds/${guildId}/members?${params.toString()}`,
{
headers: getDiscordHeaders(env.DISCORD_TOKEN ?? '', 'Bot'),
},
)
if (response.ok) {
return readJson<DiscordGuildMember[]>(response)
}
if (attempt < 2 && (response.status === 429 || response.status >= 500)) {
const delayMs = getRetryDelayMs(response)
console.log(`Guild member fetch for ${guildId} hit ${response.status}, retrying in ${delayMs}ms`)
await sleep(delayMs)
continue
}
const errorPayload = await readJson<DiscordErrorResponse | null>(response).catch(() => null)
logDiscordError(`Failed to fetch guild members for ${guildId}`, errorPayload)
throw new Error(`Discord guild member fetch failed with status ${response.status}`)
}
throw new Error(`Discord guild member fetch failed for ${guildId}`)
}
async function cacheRoles(_controller: ScheduledController, env: Env): Promise<void> {
console.log('Triggered cacheRoles')
if (!config.cacheRoles || !env.DISCORD_TOKEN || getConfiguredGuildIds().length === 0) {
console.log('Skipping cacheRoles')
return
}
console.log('Executing cacheRoles')
const memberRoleCache: Record<string, RoleCacheRecord> = {}
await Promise.all(
getConfiguredGuildIds().map(async (guildId) => {
const tempMemberList: DiscordGuildMember[] = []
let lastMemberId: string | undefined
while (true) {
const members = await fetchGuildMemberPage(env, guildId, lastMemberId)
console.log(`Got ${members.length} members for ${guildId}`)
if (members.length === 0) {
break
}
tempMemberList.push(...members)
lastMemberId = members.at(-1)?.user?.id
if (!lastMemberId) {
throw new Error(`Received malformed member payload while caching roles for ${guildId}`)
}
}
const guildRoleCache: RoleCacheRecord = {}
for (const member of tempMemberList) {
const memberId = member.user?.id
if (!memberId) {
continue
}
guildRoleCache[memberId] = Array.isArray(member.roles) ? member.roles : []
}
memberRoleCache[guildId] = guildRoleCache
await env.KV.put(`roles:${guildId}`, JSON.stringify(guildRoleCache), {
expirationTtl: 3600,
})
console.log(`Cached roles for ${Object.keys(guildRoleCache).length} members in ${guildId}`)
}),
)
console.log(`Cached roles for ${Object.keys(memberRoleCache).length} servers`)
}
app.get('/authorize/:scopemode', async (c) => {
const clientId = c.req.query('client_id')
const redirectUri = c.req.query('redirect_uri')
const scopeModeParam = c.req.param('scopemode')
if (
clientId !== config.clientId ||
!redirectUri ||
!config.redirectUrls.includes(redirectUri) ||
!isScopeMode(scopeModeParam)
) {
return c.text('Bad request.', 400)
}
const params = new URLSearchParams({
client_id: config.clientId,
redirect_uri: redirectUri,
response_type: 'code',
scope: getScopesForMode(scopeModeParam),
state: c.req.query('state') ?? '',
prompt: 'none',
})
return c.redirect(`https://discord.com/oauth2/authorize?${params.toString()}`)
})
app.post('/token', async (c) => {
const body = await c.req.parseBody()
const code = getStringValue(body.code)
const redirectUri = getStringValue(body.redirect_uri)
if (!code || !redirectUri || !config.redirectUrls.includes(redirectUri)) {
return c.text('Bad request.', 400)
}
const tokenParams = new URLSearchParams({
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: redirectUri,
code,
grant_type: 'authorization_code',
})
const tokenResponse = await fetch('https://discord.com/api/oauth2/token', {
method: 'POST',
body: tokenParams.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': DISCORD_USER_AGENT,
},
})
if (!tokenResponse.ok) {
const errorPayload = await readJson<DiscordErrorResponse | null>(tokenResponse).catch(() => null)
logDiscordError('Discord token exchange failed', errorPayload)
return c.text('Bad request.', 400)
}
const tokenData = await readJson<DiscordTokenSuccessResponse>(tokenResponse)
const returnedScopes = new Set(tokenData.scope.split(' ').filter(Boolean))
const userInfoResponse = await fetch('https://discord.com/api/users/@me', {
headers: getDiscordHeaders(tokenData.access_token, 'Bearer'),
})
if (!userInfoResponse.ok) {
const errorPayload = await readJson<DiscordErrorResponse | null>(userInfoResponse).catch(() => null)
logDiscordError('Discord user fetch failed', errorPayload)
return c.text('Bad request.', 400)
}
const userInfo = await readJson<DiscordUser>(userInfoResponse)
if (shouldIncludeEmail() && !userInfo.verified) {
console.error('User is not verified')
return c.text('Bad request.', 400)
}
let guildIds: string[] = []
if (returnedScopes.has('guilds')) {
const guildResponse = await fetch('https://discord.com/api/users/@me/guilds', {
headers: getDiscordHeaders(tokenData.access_token, 'Bearer'),
})
if (guildResponse.ok) {
const guilds = await readJson<DiscordGuildSummary[]>(guildResponse)
guildIds = guilds.map((guild) => guild.id)
}
}
const roleClaims: RoleClaims = {}
const configuredGuildIds = getConfiguredGuildIds()
if (config.cacheRoles) {
await Promise.all(
configuredGuildIds.map(async (guildId) => {
const roleCache = await getRolesFromCacheFor(c.env, guildId, userInfo.id)
if (roleCache) {
roleClaims[`roles:${guildId}`] = roleCache
}
}),
)
} else if (returnedScopes.has('guilds.members.read')) {
await Promise.all(
configuredGuildIds.map(async (guildId) => {
if (!guildIds.includes(guildId)) {
return
}
const memberResponse = await fetch(
`https://discord.com/api/users/@me/guilds/${guildId}/member`,
{
headers: getDiscordHeaders(tokenData.access_token, 'Bearer'),
},
)
if (!memberResponse.ok) {
return
}
const member = await readJson<DiscordGuildMember>(memberResponse)
roleClaims[`roles:${guildId}`] = Array.isArray(member.roles) ? member.roles : []
}),
)
} else if (c.env.DISCORD_TOKEN) {
const botToken = c.env.DISCORD_TOKEN
await Promise.all(
configuredGuildIds.map(async (guildId) => {
if (!guildIds.includes(guildId)) {
return
}
const memberResponse = await fetch(
`https://discord.com/api/guilds/${guildId}/members/${userInfo.id}`,
{
headers: getDiscordHeaders(botToken, 'Bot'),
},
)
if (!memberResponse.ok) {
return
}
const member = await readJson<DiscordGuildMember>(memberResponse)
roleClaims[`roles:${guildId}`] = Array.isArray(member.roles) ? member.roles : []
}),
)
}
const idTokenPayload: jose.JWTPayload & Record<string, unknown> = {
iss: 'https://cloudflare.com',
aud: config.clientId,
...userInfo,
...roleClaims,
email: shouldIncludeEmail() ? (userInfo.email ?? undefined) : getFallbackEmail(),
guilds: guildIds,
}
const signingKeys = await loadOrGenerateKeyPair(c.env.KV)
const idToken = await new jose.SignJWT(idTokenPayload)
.setProtectedHeader({ alg: 'RS256' })
.setExpirationTime('1h')
.setAudience(config.clientId)
.sign(signingKeys.privateKey)
return c.json({
...tokenData,
scope: tokenData.scope,
id_token: idToken,
})
})
app.get('/jwks.json', async (c) => {
const publicKey = (await loadOrGenerateKeyPair(c.env.KV)).publicKey
return c.json({
keys: [
{
alg: 'RS256',
kid: 'jwtRS256',
...(await crypto.subtle.exportKey('jwk', publicKey)),
},
],
})
})
const worker: ExportedHandler<Env> = {
async fetch(request, env, ctx) {
return app.fetch(request, env, ctx)
},
async scheduled(controller, env, ctx) {
ctx.waitUntil(cacheRoles(controller, env))
},
}
export default worker