Skip to content

Commit 401dcf1

Browse files
committed
Get userinfo from /userinfo endpoint
1 parent 24d7fb0 commit 401dcf1

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

01-Login/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ flask
22
python-dotenv
33
requests
44
flask-oauthlib
5-
python-jose
5+
six

01-Login/server.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
from flask import session
1414
from flask import url_for
1515
from flask_oauthlib.client import OAuth
16-
from jose import jwt
1716
from six.moves.urllib.parse import urlencode
18-
from six.moves.urllib.request import urlopen
17+
import requests
1918

2019
import constants
2120

@@ -27,7 +26,9 @@
2726
AUTH0_CLIENT_ID = env.get(constants.AUTH0_CLIENT_ID)
2827
AUTH0_CLIENT_SECRET = env.get(constants.AUTH0_CLIENT_SECRET)
2928
AUTH0_DOMAIN = env.get(constants.AUTH0_DOMAIN)
30-
AUTH0_AUDIENCE = env.get(constants.API_ID)
29+
AUTH0_AUDIENCE = env.get(constants.AUTH0_AUDIENCE)
30+
if AUTH0_AUDIENCE is '':
31+
AUTH0_AUDIENCE = 'https://' + AUTH0_DOMAIN + '/userinfo'
3132

3233
APP = Flask(__name__, static_url_path='/public', static_folder='./public')
3334
APP.secret_key = constants.SECRET_KEY
@@ -62,7 +63,7 @@ def handle_auth_error(ex):
6263
consumer_secret=AUTH0_CLIENT_SECRET,
6364
request_token_params={
6465
'scope': 'openid profile',
65-
'audience': 'https://' + AUTH0_DOMAIN + '/userinfo'
66+
'audience': AUTH0_AUDIENCE
6667
},
6768
base_url='https://%s' % AUTH0_DOMAIN,
6869
access_token_method='POST',
@@ -93,26 +94,26 @@ def callback_handling():
9394
raise AuthError({'code': request.args['error'],
9495
'description': request.args['error_description']}, 401)
9596

96-
# Obtain JWT and the keys to validate the signature
97-
id_token = resp['id_token']
98-
jwks = urlopen("https://"+AUTH0_DOMAIN+"/.well-known/jwks.json")
97+
url = 'https://' + AUTH0_DOMAIN + '/userinfo'
98+
headers = {'authorization': 'Bearer ' + resp['access_token']}
99+
resp = requests.get(url, headers=headers)
100+
userinfo = resp.json()
99101

100-
payload = jwt.decode(id_token, jwks.read(), algorithms=['RS256'],
101-
audience=AUTH0_CLIENT_ID, issuer="https://"+AUTH0_DOMAIN+"/")
102-
103-
session[constants.JWT_PAYLOAD] = payload
102+
session[constants.JWT_PAYLOAD] = userinfo
104103

105104
session[constants.PROFILE_KEY] = {
106-
'user_id': payload['sub'],
107-
'name': payload['name'],
108-
'picture': payload['picture']
105+
'user_id': userinfo['sub'],
106+
'name': userinfo['name'],
107+
'picture': userinfo['picture']
109108
}
110109

111110
return redirect('/dashboard')
112111

112+
113113
@APP.route('/login')
114114
def login():
115-
return auth0.authorize(callback=AUTH0_CALLBACK_URL if AUTH0_CALLBACK_URL is not '' else "http://localhost:3000/callback")
115+
return auth0.authorize(callback=AUTH0_CALLBACK_URL)
116+
116117

117118
@APP.route('/logout')
118119
def logout():

0 commit comments

Comments
 (0)