-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (55 loc) · 1.94 KB
/
app.py
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
import os
import json
import datetime
from urllib.parse import urlparse, parse_qsl
import jwt
import flask
import flask_cors
import requests
TOKEN_SECRET = os.environ.get('TOKEN_SECRET')
GITHUB_SECRET = os.environ.get('GITHUB_SECRET')
ORGANIZATION_NAME = 'workingmirror'
app = flask.Flask(__name__)
cors = flask_cors.CORS(app, resources={r'/auth/': {'origins': '*', 'supports_credentials': True}})
app.config['CORS_HEADERS'] = 'Content-Type'
def fetch_access_token(client_id, redirect_uri, code):
r = requests.get('https://github.com/login/oauth/access_token', params={
'client_id': flask.request.json['clientId'],
'redirect_uri': flask.request.json['redirectUri'],
'client_secret': GITHUB_SECRET,
'code': flask.request.json['code'],
})
return dict(parse_qsl(r.text))
def fetch_user(access_token):
r = requests.get(
'https://api.github.com/user',
params=access_token,
headers={'User-Agent': 'Satellizer'})
return json.loads(r.text)
def is_member(username):
r = requests.get('https://api.github.com/orgs/%s/members/%s' % (ORGANIZATION_NAME, username))
return r.status_code == 204
def create_jwt(user):
token = jwt.encode({
'sub': user['id'],
'iat': datetime.datetime.utcnow(),
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=14),
}, TOKEN_SECRET)
return token.decode('unicode_escape')
def parse_token(req):
token = req.headers.get('Authorization').split()[1]
return jwt.decode(token, TOKEN_SECRET)
@app.route('/auth/', methods=['POST', 'OPTIONS'])
@flask_cors.cross_origin(origin='*', headers=['Content-Type', 'Authorization'])
def github():
access_token = fetch_access_token(
flask.request.json['clientId'],
flask.request.json['redirectUri'],
flask.request.json['code'])
profile = fetch_user(access_token)
response = flask.jsonify(token=create_jwt(profile), profile=profile)
if not is_member(profile['login']):
response.status_code = 403
return response
if __name__ == '__main__':
app.run(port=3000, debug=True)