44
55from . import oauth2
66
7+ def decode_part (raw , encoding = "utf-8" ):
8+ """Decode a part of the JWT.
79
8- def base64decode (raw ):
9- """A helper can handle a padding-less raw input"""
10+ JWT is encoded by padding-less base64url,
11+ based on `JWS specs <https://tools.ietf.org/html/rfc7515#appendix-C>`_.
12+
13+ :param encoding:
14+ If you are going to decode the first 2 parts of a JWT, i.e. the header
15+ or the payload, the default value "utf-8" would work fine.
16+ If you are going to decode the last part i.e. the signature part,
17+ it is a binary string so you should use `None` as encoding here.
18+ """
1019 raw += '=' * (- len (raw ) % 4 ) # https://stackoverflow.com/a/32517907/728675
11- return base64 .b64decode (raw ).decode ("utf-8" )
20+ raw = str (
21+ # On Python 2.7, argument of urlsafe_b64decode must be str, not unicode.
22+ # This is not required on Python 3.
23+ raw )
24+ output = base64 .urlsafe_b64decode (raw )
25+ if encoding :
26+ output = output .decode (encoding )
27+ return output
1228
29+ base64decode = decode_part # Obsolete. For backward compatibility only.
1330
1431def decode_id_token (id_token , client_id = None , issuer = None , nonce = None , now = None ):
1532 """Decodes and validates an id_token and returns its claims as a dictionary.
@@ -19,7 +36,7 @@ def decode_id_token(id_token, client_id=None, issuer=None, nonce=None, now=None)
1936 and it may contain other optional content such as "preferred_username",
2037 `maybe more <https://openid.net/specs/openid-connect-core-1_0.html#Claims>`_
2138 """
22- decoded = json .loads (base64decode (id_token .split ('.' )[1 ]))
39+ decoded = json .loads (decode_part (id_token .split ('.' )[1 ]))
2340 err = None # https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
2441 if issuer and issuer != decoded ["iss" ]:
2542 # https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse
0 commit comments