-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path__init__.py
More file actions
538 lines (450 loc) · 18.8 KB
/
__init__.py
File metadata and controls
538 lines (450 loc) · 18.8 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
""" A flask application to handle connecting external accounts into CSH LDAP """
import os
import subprocess
import random
import string
import time
import urllib.parse
import hmac
from hashlib import sha1
import base64
from typing import Any
import jwt
from requests.models import HTTPError
import flask
import werkzeug
from flask import Flask, request, redirect, session, render_template, send_from_directory, jsonify
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from flask_pyoidc.provider_configuration import ProviderConfiguration, ClientMetadata
import csh_ldap
import requests
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
APP = Flask(__name__)
if os.path.exists(os.path.join(os.getcwd(), 'config.py')):
APP.config.from_pyfile(os.path.join(os.getcwd(), 'config.py'))
else:
APP.config.from_pyfile(os.path.join(os.getcwd(), 'config.env.py'))
sentry_sdk.init(dsn=APP.config['SENTRY_DSN'],
integrations=[FlaskIntegration()])
APP.secret_key = APP.config['SECRET_KEY']
_CONFIG = ProviderConfiguration(
APP.config['OIDC_ISSUER'],
client_metadata=ClientMetadata(**APP.config['OIDC_CLIENT_CONFIG']))
_AUTH = OIDCAuthentication({'default': _CONFIG}, APP)
_LDAP = csh_ldap.CSHLDAP(APP.config['LDAP_DN'], APP.config['LDAP_SECRET'])
_SLACK_AUTH_URI = 'https://slack.com/oauth/authorize' \
+ '?scope=identity.basic' \
+ '&client_id=%s' \
+ '&state=%s' \
+ '&redirect_uri=https://eac.csh.rit.edu/slack/return'
_SLACK_ACCESS_URI = 'https://slack.com/api/oauth.access' \
+ '?client_id=%s' \
+ '&client_secret=%s' \
+ '&code=%s'
_GITHUB_AUTH_URI = 'https://github.com/login/oauth/authorize' \
+ '?client_id=%s' \
+ '&state=%s'
_GITHUB_TOKEN_URI = 'https://github.com/login/oauth/access_token' \
+ '?client_id=%s' \
+ '&client_secret=%s' \
+ '&code=%s'
_TWITCH_AUTH_URI = 'https://id.twitch.tv/oauth2/authorize' \
+ '?client_id=%s' \
+ '&redirect_uri=https://eac.csh.rit.edu/twitch/return' \
+ '&response_type=code' \
+ '&force_verify=true' \
+ '&state=%s'
_TWITCH_TOKEN_URI = 'https://id.twitch.tv/oauth2/token' \
+ '?client_id=%s' \
+ '&client_secret=%s' \
+ '&code=%s' \
+ '&grant_type=authorization_code' \
+ '&redirect_uri=https://eac.csh.rit.edu/twitch/return'
_TWITTER_REQUEST_TOKEN_URI = 'https://api.twitter.com/oauth/request_token'
_TWITTER_AUTHORIZATION_URI = 'https://api.twitter.com/oauth/authenticate'
_TWITTER_ACCESS_TOKEN_URI = 'https://api.twitter.com/oauth/access_token'
_TWITTER_ACCOUNT_INFO_URI = 'https://api.twitter.com/1.1/account/verify_credentials.json'
_TWITTER_AUTH_TOKEN_CACHE = {}
@APP.route('/static/<path:path>', methods=['GET'])
def _send_static(path: str) -> flask.wrappers.Response:
return send_from_directory('static', path)
@APP.route('/')
@_AUTH.oidc_auth('default')
def _index() -> str:
commit_hash = subprocess.check_output(
['git', 'rev-parse', '--short', 'HEAD']).strip().decode('utf-8')
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
services = {
'Slack': member.slackuid,
'GitHub': member.github,
'Twitch': member.twitchlogin,
'Twitter': member.twittername,
}
return render_template('home.html',
commit_hash=commit_hash,
uid=uid,
services=services)
@APP.route('/slack', methods=['GET'])
@_AUTH.oidc_auth('default')
def _auth_slack() -> werkzeug.Response:
return redirect(_SLACK_AUTH_URI %
(APP.config['SLACK_CLIENT_ID'], APP.config['STATE']))
@APP.route('/slack/return', methods=['GET'])
@_AUTH.oidc_auth('default')
def _link_slack() -> tuple[str, int]:
""" Links Slack into LDAP via slackUID """
# Determine if we have a valid reason to do things
state = request.args.get('state')
if state != APP.config['STATE']:
return 'Invalid state', 400
resp = requests.get(
_SLACK_ACCESS_URI %
(APP.config['SLACK_CLIENT_ID'], APP.config['SLACK_SECRET'],
request.args.get('code')),
timeout=APP.config['REQUEST_TIMEOUT'],
)
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
member.slackUID = resp.json()['user']['id']
return render_template('callback.html'), 200
@APP.route('/slack', methods=['DELETE'])
@_AUTH.oidc_auth('default')
def _revoke_slack() -> werkzeug.Response:
""" Revokes Slack by clearing slackUID """
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
member.slackUID = None
return jsonify(success=True)
@APP.route('/github', methods=['GET'])
@_AUTH.oidc_auth('default')
def _auth_github() -> werkzeug.Response:
# Redirect to github for authorisation
return redirect(
_GITHUB_AUTH_URI %
(APP.config['GITHUB_OAUTH_CLIENT_ID'], APP.config['STATE']))
@APP.route('/github/return', methods=['GET'])
@_AUTH.oidc_auth('default')
def _github_landing() -> tuple[str, int]:
# Determine if we have a valid reason to do things
state = request.args.get('state')
if state != APP.config['STATE']:
return 'Invalid state', 400
# Get token from github
resp = requests.post(
_GITHUB_TOKEN_URI %
(APP.config['GITHUB_OAUTH_CLIENT_ID'],
APP.config['GITHUB_OAUTH_CLIENT_SECRET'], request.args.get('code')),
headers={'Accept': 'application/json'},
timeout=APP.config['REQUEST_TIMEOUT'])
try:
resp.raise_for_status()
except HTTPError as e:
print('response:', resp.json())
raise e
resp_json = resp.json()
token = resp_json['access_token']
header = {
'Authorization': 'token ' + token,
'Accept': 'application/vnd.github.v3+json'
}
user_resp = requests.get('https://api.github.com/user',
headers=header,
timeout=APP.config['REQUEST_TIMEOUT'])
try:
user_resp.raise_for_status()
except HTTPError as e:
print('response:', user_resp.json())
raise e
user_resp_json = user_resp.json()
github_username = user_resp_json['login']
github_id = user_resp_json['id']
# Pull member from LDAP
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
_link_github(github_username, github_id, member)
return render_template('callback.html'), 200
def _get_github_jwt() -> str:
signing_key = APP.config["GITHUB_APP_PRIVATE_KEY"]
payload = {
'iat': int(time.time()),
'exp': int(time.time() + 600),
'iss': APP.config['GITHUB_APP_CLIENT_ID'],
}
encoded_jwt = jwt.encode(payload, signing_key, algorithm='RS256')
return encoded_jwt
def _auth_github_org() -> str:
jwt_auth = _get_github_jwt()
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'Bearer {jwt_auth}',
}
org_installation_resp = requests.get(
'https://api.github.com/orgs/ComputerScienceHouse/installation',
headers=headers,
timeout=APP.config['REQUEST_TIMEOUT'])
try:
org_installation_resp.raise_for_status()
except HTTPError as e:
print('response:', org_installation_resp.json())
raise e
org_installation_json = org_installation_resp.json()
org_installation_id = org_installation_json['id']
org_token_resp = requests.post(
f'https://api.github.com/app/installations/{org_installation_id}/access_tokens',
headers=headers,
timeout=APP.config['REQUEST_TIMEOUT'])
try:
org_token_resp.raise_for_status()
except HTTPError as e:
print('response:', org_token_resp.json())
raise e
org_token_json = org_token_resp.json()
org_token = org_token_json['token']
return org_token
def _link_github(github_username: str, github_id: str, member: Any) -> None:
"""
Puts a member's github into LDAP and adds them to the org.
:param github_username: the user's github username
:param github_id: the user's github id
:param member: the member's LDAP object
"""
org_token = _auth_github_org()
payload = {
'org': 'ComputerScienceHouse',
'invitee_id': github_id,
'role': 'direct_member'
}
github_org_headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'Token {org_token}',
}
resp = requests.post(
'https://api.github.com/orgs/ComputerScienceHouse/invitations',
headers=github_org_headers,
json=payload,
timeout=APP.config['REQUEST_TIMEOUT'])
try:
resp.raise_for_status()
except HTTPError as e:
print('response:', resp.json())
raise e
member.github = github_username
@APP.route('/github', methods=['DELETE'])
@_AUTH.oidc_auth('default')
def _revoke_github() -> werkzeug.Response:
""" Clear's a member's github in LDAP and removes them from the org. """
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
org_token = _auth_github_org()
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'Token {org_token}',
}
resp = requests.delete(
'https://api.github.com/orgs/ComputerScienceHouse/members/' +
member.github,
headers=headers,
timeout=APP.config['REQUEST_TIMEOUT'],
)
try:
resp.raise_for_status()
except HTTPError as e:
print('response:', resp.json())
raise e
member.github = None
return jsonify(success=True)
@APP.route('/twitch', methods=['GET'])
@_AUTH.oidc_auth('default')
def _auth_twitch() -> werkzeug.Response:
# Redirect to twitch for authorisation
return redirect(_TWITCH_AUTH_URI %
(APP.config['TWITCH_CLIENT_ID'], APP.config['STATE']))
@APP.route('/twitch/return', methods=['GET'])
@_AUTH.oidc_auth('default')
def _twitch_landing() -> tuple[str, int]:
# Determine if we have a valid reason to do things
state = request.args.get('state')
if state != APP.config['STATE']:
return 'Invalid state', 400
resp = requests.post(
_TWITCH_TOKEN_URI %
(APP.config['TWITCH_CLIENT_ID'], APP.config['TWITCH_CLIENT_SECRET'],
request.args.get('code')),
headers={'Accept': 'application/json'},
timeout=APP.config['REQUEST_TIMEOUT'],
)
header = {
'Authorization': 'OAuth ' + resp.json()['access_token'],
}
resp = requests.get(
'https://id.twitch.tv/oauth2/validate',
headers=header,
timeout=APP.config['REQUEST_TIMEOUT'],
)
# Pull member from LDAP
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
member.twitchlogin = resp.json()['login']
return render_template('callback.html'), 200
@APP.route('/twitch', methods=['DELETE'])
@_AUTH.oidc_auth('default')
def _revoke_twitch() -> werkzeug.Response:
""" Clear's a member's twitch login in LDAP."""
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
member.twitchlogin = None
return jsonify(success=True)
@APP.route('/twitter', methods=['GET'])
@_AUTH.oidc_auth('default')
def _auth_twitter() -> werkzeug.Response:
# Make a POST request to get the request token
oauth_nonce = ''.join([
random.choice(string.ascii_letters + string.digits) for n in range(32)
])
oauth_timestamp = int(time.time())
oauth_parameter_string = f'oauth_callback={urllib.parse.quote("https://eac.csh.rit.edu/twitter/return", safe="")}' \
f'&oauth_consumer_key={APP.config["TWITTER_CONSUMER_KEY"]}' \
f'&oauth_nonce={oauth_nonce}' \
f'&oauth_signature_method=HMAC-SHA1' \
f'&oauth_timestamp={oauth_timestamp}' \
f'&oauth_version=1.0'
oauth_signature_base_string = 'POST&' \
+ urllib.parse.quote(_TWITTER_REQUEST_TOKEN_URI, safe='') + '&' \
+ urllib.parse.quote(oauth_parameter_string, safe='')
oauth_signing_key = f'{APP.config["TWITTER_CONSUMER_SECRET_KEY"]}&'
oauth_signature = base64.b64encode(
hmac.new(oauth_signing_key.encode(),
oauth_signature_base_string.encode(),
sha1).digest()).decode('UTF-8')
oauth_header = f'OAuth oauth_callback="https://eac.csh.rit.edu/twitter/return"' \
f'oauth_consumer_key="{APP.config["TWITTER_CONSUMER_KEY"]}", ' \
f'oauth_nonce="{oauth_nonce}", ' \
f'oauth_signature="{urllib.parse.quote(oauth_signature, safe="")}", ' \
f'oauth_signature_method="HMAC-SHA1", ' \
f'oauth_timestamp="{oauth_timestamp}", ' \
f'oauth_version="1.0"'
resp = requests.post(
_TWITTER_REQUEST_TOKEN_URI,
headers={
'Accept': '*/*',
'Authorization': oauth_header
},
timeout=APP.config['REQUEST_TIMEOUT'],
)
if resp.status_code != 200:
print(f'Status: {resp.status_code}\nMessage: {resp.text}')
return flask.make_response(('Error fetching request_token', 500))
returned_params = dict(
(key.strip(), val.strip())
for key, val in (element.split('=')
for element in resp.text.split('&')))
_TWITTER_AUTH_TOKEN_CACHE[
returned_params['oauth_token']] = returned_params['oauth_token_secret']
# Redirect to twitter for authorisation
return redirect(
f'{_TWITTER_AUTHORIZATION_URI}?oauth_token={returned_params["oauth_token"]}'
)
@APP.route('/twitter/return', methods=['GET'])
@_AUTH.oidc_auth('default')
def _twitter_landing() -> tuple[str, int]:
oauth_token = request.args.get('oauth_token')
if oauth_token is None:
return "Failed to get outh token", 400
oauth_verifier = request.args.get('oauth_verifier')
if oauth_verifier is None:
return "Failed to get outh verifier", 400
oauth_nonce = ''.join([
random.choice(string.ascii_letters + string.digits) for n in range(32)
])
oauth_timestamp = int(time.time())
oauth_parameter_string = f'oauth_consumer_key={APP.config["TWITTER_CONSUMER_KEY"]}' \
f'&oauth_nonce={oauth_nonce}' \
f'&oauth_signature_method=HMAC-SHA1' \
f'&oauth_timestamp={oauth_timestamp}' \
f'&oauth_token={urllib.parse.quote(oauth_token, safe="")}' \
f'&oauth_verifier={urllib.parse.quote(oauth_verifier, safe="")}' \
f'&oauth_version=1.0'
oauth_signature_base_string = 'POST&' \
+ urllib.parse.quote(_TWITTER_ACCESS_TOKEN_URI, safe='') + '&' \
+ urllib.parse.quote(oauth_parameter_string, safe='')
oauth_signing_key = f'{APP.config["TWITTER_CONSUMER_SECRET_KEY"]}&{_TWITTER_AUTH_TOKEN_CACHE[oauth_token]}'
oauth_signature = base64.b64encode(
hmac.new(oauth_signing_key.encode(),
oauth_signature_base_string.encode(),
sha1).digest()).decode('UTF-8')
oauth_header = f'OAuth oauth_consumer_key="{APP.config["TWITTER_CONSUMER_KEY"]}", ' \
f'oauth_nonce="{oauth_nonce}", ' \
f'oauth_signature="{urllib.parse.quote(oauth_signature, safe="")}", ' \
f'oauth_signature_method="HMAC-SHA1", ' \
f'oauth_timestamp="{oauth_timestamp}", ' \
f'oauth_token="{oauth_token}"' \
f'oauth_version="1.0"'
resp = requests.post(
_TWITTER_REQUEST_TOKEN_URI,
data=f'oauth_verifier={oauth_verifier}',
headers={
'Accept': '*/*',
'Authorization': oauth_header,
'Content-Type': 'application/x-www-form-urlencoded'
},
timeout=APP.config['REQUEST_TIMEOUT'],
)
returned_params = dict(
(key.strip(), val.strip())
for key, val in (element.split('=')
for element in resp.text.split('&')))
oauth_token = returned_params['oauth_token']
oauth_token_secret = returned_params['oauth_token_secret']
# OK, now that we have the proper token and secret, we can get the user's information
oauth_nonce = ''.join([
random.choice(string.ascii_letters + string.digits) for n in range(32)
])
oauth_timestamp = int(time.time())
oauth_parameter_string = f'auth_consumer_key={APP.config["TWITTER_CONSUMER_KEY"]}' \
f'&oauth_nonce={oauth_nonce}' \
f'&oauth_signature_method=HMAC-SHA1' \
f'&oauth_timestamp={oauth_timestamp}' \
f'&oauth_token={urllib.parse.quote(oauth_token, safe="")}' \
f'&oauth_version=1.0'
oauth_signature_base_string = 'POST&' \
+ urllib.parse.quote(_TWITTER_ACCOUNT_INFO_URI, safe='') + '&' \
+ urllib.parse.quote(oauth_parameter_string, safe='')
oauth_signing_key = f"{APP.config['TWITTER_CONSUMER_SECRET_KEY']}&{oauth_token_secret}"
oauth_signature = base64.b64encode(
hmac.new(oauth_signing_key.encode(),
oauth_signature_base_string.encode(),
sha1).digest()).decode('UTF-8')
oauth_header = f'OAuth oauth_consumer_key="{APP.config["TWITTER_CONSUMER_KEY"]}", ' \
f'oauth_nonce="{oauth_nonce}", ' \
f'oauth_signature="{urllib.parse.quote(oauth_signature, safe="")}", ' \
f'oauth_signature_method="HMAC-SHA1", ' \
f'oauth_timestamp="{oauth_timestamp}", ' \
f'oauth_token="{oauth_token}"' \
f'oauth_version="1.0"'
resp = requests.get(
_TWITTER_ACCOUNT_INFO_URI,
headers={
'Accept': '*/*',
'Authorization': oauth_header
},
timeout=APP.config['REQUEST_TIMEOUT'],
)
# Pull member from LDAP
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
member.twittername = resp.json()[0]['screen_name']
return render_template('callback.html'), 200
@APP.route('/twitter', methods=['DELETE'])
@_AUTH.oidc_auth('default')
def _revoke_twitter() -> werkzeug.Response:
""" Clear's a member's twitter login in LDAP."""
uid = str(session['userinfo'].get('preferred_username', ''))
member = _LDAP.get_member(uid, uid=True)
member.twittername = None
return jsonify(success=True)
@APP.route('/logout')
@_AUTH.oidc_logout
def logout() -> werkzeug.Response:
return redirect('/', 302)