-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.mjs
More file actions
154 lines (148 loc) · 4.45 KB
/
Copy pathgithub.mjs
File metadata and controls
154 lines (148 loc) · 4.45 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
import {
GITHUB_CLIENT_ID,
GITHUB_REDIRECT_URI,
GITHUB_CLIENT_SECRET,
} from '../config.mjs'
import fetch from 'node-fetch'
import { catchError } from '../catchError.mjs'
import { Octokit } from 'octokit'
const GITHUB_URL = process.env.GITHUB_URL ?? 'https://github.com'
const GITHUB_API_URL = process.env.GITHUB_URL ?? 'https://api.github.com'
export default ({ app, wsApp, db, ceremony }) => {
app.get(
'/oauth/github',
catchError(async (req, res) => {
const { token } = req.query
const auth = await db.findOne('Auth', {
where: { token },
})
if (!auth) return res.status(401).json({ error: 'unauthorized' })
const state = await db.create('OAuthState', {
type: 'github',
redirectDestination: req.query.redirectDestination,
userId: auth.userId,
})
const url = new URL('/login/oauth/authorize', GITHUB_URL)
url.searchParams.set('client_id', GITHUB_CLIENT_ID)
url.searchParams.set('redirect_uri', GITHUB_REDIRECT_URI)
url.searchParams.set('scope', 'gist')
url.searchParams.set('state', state._id)
url.searchParams.set('allow_signup', 'false')
res.redirect(url.toString())
})
)
app.get(
'/oauth/github/callback',
catchError(async (req, res) => {
const { code, state, error } = req.query
const _state = await db.findOne('OAuthState', {
where: { _id: state },
})
if (!_state) {
res.status(401).json({
error: 'Invalid state',
})
return
}
await db.delete('OAuthState', {
where: {
_id: state,
},
})
if (error) {
// access was denied
const url = new URL(_state.redirectDestination)
url.searchParams.set('error', 'There was a problem authenticating you')
res.redirect(url.toString())
return
}
const url = new URL('/login/oauth/access_token', GITHUB_URL)
url.searchParams.set('client_id', GITHUB_CLIENT_ID)
url.searchParams.set('client_secret', GITHUB_CLIENT_SECRET)
url.searchParams.set('code', code)
const auth = await fetch(url.toString(), {
method: 'POST',
headers: {
accept: 'application/json',
},
})
const { access_token, scope, token_type } = await auth.json()
const apiUrl = new URL('/user', GITHUB_API_URL)
const user = await fetch(apiUrl.toString(), {
headers: {
authorization: `token ${access_token}`,
},
}).then((r) => r.json())
if (!user.id) {
const _url = new URL(_state.redirectDestination)
_url.searchParams.append('error', 'Unknown problem')
res.redirect(_url.toString())
return
}
// end oauth logic
const signupId = `github-${user.id}`
const existingAuth = await db.findOne('OAuth', {
where: {
_id: signupId,
},
})
if (existingAuth) {
await db.update('CeremonyQueue', {
where: {
userId: existingAuth.userId,
completedAt: null,
},
update: {
completedAt: +new Date(),
prunedAt: +new Date(),
},
})
await db.delete('OAuth', {
where: {
_id: signupId,
},
})
}
const signupAt = new Date(user.created_at)
await db.create('OAuth', {
_id: signupId,
userId: _state.userId,
accountAgeMs: Math.max(0, +new Date() - +signupAt),
type: 'github',
})
if (!_state.redirectDestination) {
res.status(204).end()
} else {
const _url = new URL(_state.redirectDestination)
_url.searchParams.set('github_access_token', access_token)
_url.searchParams.set('name', `Github#${user.login}`)
res.redirect(_url.toString())
}
})
)
app.get(
'/post/github',
catchError(async (req, res) => {
const { access_token, content } = req.query
const octokit = new Octokit({
request: {
fetch: fetch,
},
auth: access_token,
})
const filename = `unirep-trusted-setup-${+new Date()}.log.md`
const data = {
description: 'Post of Unirep trusted setup',
files: {},
headers: {
'x-github-api-version': '2022-11-28',
},
}
data.files[filename] = {
content,
}
const response = await octokit.request('POST /gists', data)
res.json(response)
})
)
}