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?
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)
had the same issue, found this https://github.com/jpadilla/pyjwt/blob/master/jwt/algorithms.py#L49, needed to install cryptography (poetry add cryptography)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With