-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (42 loc) · 1.38 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
import os
from authlib.integrations.flask_client import OAuth
from flask import Flask, redirect, render_template, session, url_for
app = Flask(__name__)
app.secret_key = os.environ['SECRET_KEY']
app.config.update(
{
'ISIC_CLIENT_ID': os.environ['OAUTH_CLIENT_ID'],
'ISIC_CLIENT_SECRET': os.environ['OAUTH_CLIENT_SECRET'],
'ISIC_ACCESS_TOKEN_URL': f'{os.environ["OAUTH_BASE_URL"].rstrip("/")}/token/',
'ISIC_AUTHORIZE_URL': f'{os.environ["OAUTH_BASE_URL"].rstrip("/")}/authorize',
'ISIC_API_BASE_URL': os.environ['API_BASE_URL'].rstrip('/') + '/',
'ISIC_CLIENT_KWARGS': {
# Enable PKCE
'code_challenge_method': 'S256',
},
}
)
oauth = OAuth(app)
oauth.register('isic')
@app.route('/login')
def login():
redirect_uri = url_for('authorize', _external=True)
return oauth.isic.authorize_redirect(redirect_uri)
@app.route('/authorize')
def authorize():
oauth.isic.authorize_access_token()
resp = oauth.isic.get('users/me')
resp.raise_for_status()
session['user'] = resp.json()
return redirect('/')
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect('/')
@app.route('/')
def index():
ctx = {'logged_in': False}
if 'user' in session:
ctx['logged_in'] = True
ctx['me'] = session['user']
return render_template('index.html', **ctx)