-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
78 lines (65 loc) · 2.69 KB
/
worker.js
File metadata and controls
78 lines (65 loc) · 2.69 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
export default {
async fetch(request, env) {
const { pathname } = new URL(request.url)
const uuid = pathname.split('/')[1]
if (pathname === `/success`) {
// check if request is to base_url/link/success
const code = new URL(request.url).searchParams.get('code')
const state = new URL(request.url).searchParams.get('state')
// exchange oauth code for access token
const tokenResponse = await fetch(`${env.INTRA_BASE_URL}/oauth/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
redirect_uri: env.INTRA_REDIRECT_URI,
client_id: env.INTRA_CLIENT_ID,
client_secret: env.INTRA_CLIENT_SECRET
}),
})
const tokenData = await tokenResponse.json()
if (!tokenData.access_token) {
// if access token is not returned, return 401
return new Response(null, { status: 401 })
}
// get user's username from intra API using access token
const userDataResponse = await fetch(`${env.INTRA_BASE_URL}/v2/me`, {
headers: { Authorization: `Bearer ${tokenData.access_token}` },
})
const userData = await userDataResponse.json()
if (!userData.login) {
// if username is not returned, return 401
return new Response(null, { status: 401 })
}
// store uuid and username in worker KV
if (await env.ACCOUNTS.get(state) == userData.login)
return new Response('This minecraft account is already linked to your intra account')
await env.ACCOUNTS.put(state, userData.login)
return new Response('Successfully linked account')
}
if (pathname === `/${uuid}`) {
// check if request is to base_url/uuid
const value = await env.ACCOUNTS.get(uuid)
if (value) {
// if value exists, return 200 with body
return new Response(JSON.stringify({ intra_account: value }), {
headers: { 'Content-Type': 'application/json' },
})
} else {
// if value does not exist, return 401
return new Response(null, { status: 401 })
}
}
if (pathname === `/${uuid}/link`) {
// check if request is to base_url/uuid/link
// redirect user to 42 intra oauth page
const redirectUrl = `${env.INTRA_BASE_URL}/oauth/authorize?client_id=${env.INTRA_CLIENT_ID}&response_type=code&redirect_uri=${encodeURIComponent(env.INTRA_REDIRECT_URI)}&state=${uuid}`
return Response.redirect(redirectUrl, 302)
}
// if request does not match any of the above, return 404
return new Response(null, { status: 404 })
}
}