As explained here, I am trying to verify a token that is passed, by an Android app, to a server running python3.
I want to verify the passed token. The trouble is that I am running python3 on the server which is not supported by the google-api-python-client library. I found the following workaround, using the pyjwt and requests libraries, from this site:
import json
import jwt
import requests
GOOGLE_CERTS_URI = 'https://www.googleapis.com/oauth2/v1/certs'
class GoogleIdToken(object):
def __init__(self):
self._certs = {}
self._token = {}
def getCerts(self):
cert = requests.get(GOOGLE_CERTS_URI)
if cert.status_code == 200:
return json.loads(cert.content)
def isValid(self, token, audience, clientId=None):
self._certs = self.getCerts()
for key in self._certs:
try:
token = jwt.decode(token, key=self._certs[key], verify=False)
if 'email' in token and 'aud' in token:
if token['aud'] == audience and (clientId == token['cid'] if clientId is not None else True):
self._token = token
return True
except Exception, e:
print("Error decoding: %s" % e.message)
return False
My two questions are:
- Does anyone know of a different and/or better existing solution that works in python3?
- Is the solution above complete?