Skip to content

Verify jwt locally #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions CFC_WebApp/api/cfc_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import time
# So that we can set the socket timeout
import socket
# For decoding tokens using the google decode URL
# We want to switch this to something offline later
# For decoding JWTs using the google decode URL
import urllib
import requests
# For decoding JWTs on the client side
import oauth2client.client
from oauth2client.crypt import AppIdentityError

config_file = open('config.json')
config_data = json.load(config_file)
Expand Down Expand Up @@ -351,23 +353,31 @@ def after_request():
# This should only be used by createUserProfile since we may not have a UUID
# yet. All others should use the UUID.
def verifyUserToken(token):
constructedURL = ("https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=%s"%token)
r = requests.get(constructedURL)
tokenFields = json.loads(r.content)
in_client_key = tokenFields['audience']
if (in_client_key != client_key):
if (in_client_key != hack_client_key):
abort(401, "Invalid client key %s" % in_client_key)
# attempt to validate token on the client-side
try:
tokenFields = verify_id_token(token, client_key)
except AppIdentityError:
try:
tokenFields = verify_id_token(token, hack_client_key)
except AppIdentityError:
# fall back to verifying using Google API
constructedURL = ("https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=%s" % token)
r = requests.get(constructedURL)
tokenFields = json.loads(r.content)
in_client_key = tokenFields['audience']
if (in_client_key != client_key):
if (in_client_key != hack_client_key):
abort(401, "Invalid client key %s" % in_client_key)

logging.debug("Found user email %s" % tokenFields['email'])
return tokenFields['email']

def getUUIDFromToken(token):
userEmail = verifyUserToken(token)
user=User.fromEmail(userEmail)
if user is None:
return None
user_uuid=user.uuid
return user_uuid
userEmail = verifyUserToken(token)
user = User.fromEmail(userEmail)
if user is None:
return None
return user.uuid

def getUUID(request):
retUUID = None
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ requests==2.5.1
six==1.9.0
utm==0.3.1
wsgiref==0.1.2
google-api-python-client==1.1