-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·54 lines (45 loc) · 1.59 KB
/
main.py
File metadata and controls
executable file
·54 lines (45 loc) · 1.59 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
from flask import Flask, session, redirect, url_for, request, render_template
import urlparse
import requests
from snippets import ReverseProxied
app = Flask(__name__)
app.config.from_object('default_settings')
app.wsgi_app = ReverseProxied(app.wsgi_app)
@app.route("/")
def index():
access_token = session.get('access_token', None)
if not access_token:
return redirect(url_for('login'))
return render_template('index.html', access_token=access_token)
@app.route("/login")
def login():
if 'access_token' in session:
return redirect(url_for('index'))
elif 'code' in request.args:
# Exchange auth code for access token.
params = {
'client_id': app.config['CLIENT_ID'],
'client_secret': app.config['CLIENT_SECRET'],
'code': request.args.get('code')
}
url = 'https://github.com/login/oauth/access_token'
res = requests.post(url, data=params)
qs = urlparse.parse_qs(res.text)
session['access_token'] = qs['access_token'][0]
return redirect(url_for('index'))
else:
# Request auth code.
params = {
'client_id': app.config['CLIENT_ID'],
'redirect_uri': app.config['SITE_URL'] + '/login',
'scope': 'repo'
}
qs = '&'.join(a + '=' + b for a, b in params.iteritems())
url = 'https://github.com/login/oauth/authorize?' + qs
return redirect(url)
@app.route('/logout')
def logout():
session.pop('access_token', None)
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(host='0.0.0.0')