Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't decode JWT token

Tags:

python

jwt

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?

like image 434
evgenyorlov1 Avatar asked Oct 17 '25 19:10

evgenyorlov1


2 Answers

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)
like image 136
evgenyorlov1 Avatar answered Oct 21 '25 01:10

evgenyorlov1


had the same issue, found this https://github.com/jpadilla/pyjwt/blob/master/jwt/algorithms.py#L49, needed to install cryptography (poetry add cryptography)

like image 24
ra_ Avatar answered Oct 21 '25 00:10

ra_