Reputation: 235
I receive JWT token from google oauth API. I am able to decode it via jwt.io website using RS256 algorithm. The question is how to decode it via python? I tried using pyJWT but with no luck:
import jwt
js = jwt.decode(
"JWT staff",
algorithms=["RS256"],
)
print(js)
I get following error:
jwt.exceptions.InvalidAlgorithmError: The specified alg value is not allowed
So, what is the issue? And how to decode received JWT?
Upvotes: 4
Views: 5943
Reputation: 895
This worked for me:
pip install pyjwt
pip install pyjwt[crypto]
Then run this code:
import jwt
token = 'your_token_here'
decoded_token = jwt.decode(token, options={'verify_signature': False})
print(decoded_token)
Upvotes: 1
Reputation: 21
had the same issue, found this https://github.com/jpadilla/pyjwt/blob/master/jwt/algorithms.py#L49, needed to install cryptography (poetry add cryptography)
Upvotes: 2
Reputation: 235
Well, I found an answer how to decode Google OAuth2.0 JWT id_token. Below is code I used, it only returns decoded payload.
import base64
import json
def parse_id_token(token: str) -> dict:
"""
Parse Google OAuth2.0 id_token payload
"""
parts = token.split(".")
if len(parts) != 3:
raise Exception("Incorrect id token format")
payload = parts[1]
padded = payload + "=" * (4 - len(payload) % 4)
decoded = base64.b64decode(padded)
return json.loads(decoded)
Upvotes: 2