-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathgithub.py
More file actions
294 lines (222 loc) · 8.42 KB
/
github.py
File metadata and controls
294 lines (222 loc) · 8.42 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
import uuid
import os
import re
import requests
from flask import g, request, abort, redirect
from flask_restplus import Resource
from pyinfraboxutils import get_logger, get_root_url
from pyinfraboxutils.ibrestplus import api
from pyinfraboxutils.token import encode_user_token
logger = get_logger('github')
GITHUB_CLIENT_ID = os.environ['INFRABOX_GITHUB_CLIENT_ID']
GITHUB_CLIENT_SECRET = os.environ['INFRABOX_GITHUB_CLIENT_SECRET']
GITHUB_AUTHORIZATION_URL = os.environ['INFRABOX_GITHUB_LOGIN_URL'] + "/oauth/authorize"
GITHUB_TOKEN_URL = os.environ['INFRABOX_GITHUB_LOGIN_URL'] + "/oauth/access_token"
GITHUB_USER_PROFILE_URL = os.environ['INFRABOX_GITHUB_API_URL'] + "/user"
GITHUB_CALLBACK_URL = get_root_url('global') + "/github/auth/callback"
# TODO(ib-steffen): move into DB
states = {}
def get_next_page(r):
link = r.headers.get('Link', None)
if not link:
return None
n1 = link.find('rel=\"next\"')
if n1 < 0:
return None
n2 = link.rfind('<', 0, n1)
if n2 < 0:
return None
n2 += 1
n3 = link.find('>;', n2)
return link[n2:n3]
def parse_link_header(link):
reg = r"<.+?page=(?P<page>\d+)>; rel=\"(?P<direction>prev|next|first|last)\""
res = {}
for match in re.finditer(reg, link):
res[match.group("direction")] = match.group("page")
return res
def get_github_api(url, token, raw_result=False):
headers = {
"Authorization": "token " + token,
"User-Agent": "InfraBox"
}
url = os.environ['INFRABOX_GITHUB_API_URL'] + url
# TODO(ib-steffen): allow custom ca bundles
r = requests.get(url, headers=headers, verify=False)
if raw_result:
return r
result = []
result.extend(r.json())
p = get_next_page(r)
while p:
r = requests.get(p, headers=headers, verify=False)
p = get_next_page(r)
result.extend(r.json())
return result
@api.route('/github/auth/connect', doc=False)
class Connect(Resource):
def get(self):
if os.environ['INFRABOX_GITHUB_LOGIN_ENABLED'] == 'true':
abort(404)
user_id = g.token['user']['id']
uid = str(uuid.uuid4())
g.db.execute('''
UPDATE "user" SET github_id = null, github_api_token = %s
WHERE id = %s
''', [uid, user_id])
g.db.commit()
state = str(uuid.uuid4())
url = GITHUB_AUTHORIZATION_URL
url += '?client_id=%s&scope=%s&state=%s&redirect_uri=%s' % (GITHUB_CLIENT_ID,
'%20'.join(['user:email', 'repo', 'read:org']),
state,
'%s%%3Ft=%s' % (GITHUB_CALLBACK_URL, uid))
states[str(state)] = True
return redirect(url)
@api.route('/api/v1/github/repos', doc=False)
class Repos(Resource):
def get(self):
user_id = g.token['user']['id']
user = g.db.execute_one_dict('''
SELECT github_api_token
FROM "user"
WHERE id = %s
''', [user_id])
if not user:
abort(404)
token = user['github_api_token']
github_repos = get_github_api('/user/repos?visibility=all', token)
repos = g.db.execute_many_dict('''
select github_id from collaborator co
INNER JOIN repository r
ON co.project_id = r.project_id
WHERE user_id = %s
AND github_id is not null
''', [user_id])
for gr in github_repos:
gr['connected'] = False
for r in repos:
if r['github_id'] == gr['id']:
gr['connected'] = True
break
return github_repos
@api.route('/api/v1/github/paginated_repos', doc=False)
class V2Repos(Resource):
def get(self):
user_id = g.token['user']['id']
user = g.db.execute_one_dict('''
SELECT github_api_token
FROM "user"
WHERE id = %s
''', [user_id])
if not user:
abort(404)
token = user['github_api_token']
page = request.args.get('page', 1)
per_page = request.args.get('per_page', 50)
github_repos_response = get_github_api('/user/repos?visibility=all&page={page}&per_page={per_page}'
.format(page=page, per_page=per_page),
token, raw_result=True)
github_repos = github_repos_response.json()
filtered_repos = []
for github_repo in github_repos:
filtered_repos.append({
"name": github_repo["name"],
"owner_login": github_repo["owner"]["login"],
"private": github_repo["private"],
"open_issues_count": github_repo["open_issues_count"],
"forks_count": github_repo["forks_count"],
})
nav = {}
for direction, page in parse_link_header(github_repos_response.headers.get('Link', "")).items():
nav[direction] = page
result = {
"nav": nav,
"items": filtered_repos
}
return result
@api.route('/github/auth', doc=False)
class Auth(Resource):
def get(self):
if os.environ['INFRABOX_GITHUB_LOGIN_ENABLED'] != 'true':
abort(404)
state = uuid.uuid4()
url = GITHUB_AUTHORIZATION_URL
url += '?client_id=%s&scope=%s&state=%s' % (GITHUB_CLIENT_ID,
'%20'.join(['user:email', 'repo', 'read:org']),
state)
states[str(state)] = True
return redirect(url)
def check_org(access_token):
allowed_orgs = os.environ.get('INFRABOX_GITHUB_LOGIN_ALLOWED_ORGANIZATIONS', None)
if not allowed_orgs:
return
allowed_orgs = allowed_orgs.split(',')
orgs = get_github_api('/user/orgs', access_token)
for o in orgs:
for ao in allowed_orgs:
if o['login'] == ao:
return
abort(401, "Not allowed to signup")
@api.route('/github/auth/callback', doc=False)
class Login(Resource):
def get(self):
state = request.args.get('state')
code = request.args.get('code')
t = request.args.get('t', None)
if not states.get(state, None):
abort(401)
del states[state]
# TODO(ib-steffen): allow custom ca bundles
r = requests.post(GITHUB_TOKEN_URL, data={
'client_id': GITHUB_CLIENT_ID,
'client_secret': GITHUB_CLIENT_SECRET,
'code': code,
'state': state
}, headers={'Accept': 'application/json'}, verify=False)
if r.status_code != 200:
logger.error(r.text)
abort(500)
result = r.json()
access_token = result['access_token']
check_org(access_token)
# TODO(ib-steffen): allow custom ca bundles
r = requests.get(GITHUB_USER_PROFILE_URL, headers={
'Accept': 'application/json',
'Authorization': 'token %s' % access_token
}, verify=False)
gu = r.json()
github_id = gu['id']
if os.environ['INFRABOX_GITHUB_LOGIN_ENABLED'] == 'true':
user = g.db.execute_one_dict('''
SELECT id FROM "user"
WHERE github_id = %s
''', [github_id])
if not user:
user = g.db.execute_one_dict('''
INSERT INTO "user" (github_id, username, avatar_url, name)
VALUES (%s, %s, %s, %s) RETURNING id
''', [github_id, gu['login'], gu['avatar_url'], gu['name']])
user_id = user['id']
else:
if not t:
abort(404)
user = g.db.execute_one_dict('''
SELECT id
FROM "user"
WHERE github_api_token = %s
''', [t])
if not user:
abort(404)
user_id = user['id']
g.db.execute('''
UPDATE "user" SET github_api_token = %s, github_id = %s
WHERE id = %s
''', [access_token, github_id, user_id])
g.db.commit()
token = encode_user_token(user_id)
url = get_root_url('global') + '/dashboard/'
logger.debug("Redirecting GitHub user to %s", url)
res = redirect(url)
res.set_cookie('token', token)
return res