Decode a part of the JWT. JWT is encoded by padding-less base64url, based on `JWS specs `_. :param encoding: If you are going to decode the first 2 parts of a JWT, i.e. the header or the payload, the default value "utf-8"
(raw, encoding="utf-8")
| 12 | logger = logging.getLogger(__name__) |
| 13 | |
| 14 | def decode_part(raw, encoding="utf-8"): |
| 15 | """Decode a part of the JWT. |
| 16 | |
| 17 | JWT is encoded by padding-less base64url, |
| 18 | based on `JWS specs <https://tools.ietf.org/html/rfc7515#appendix-C>`_. |
| 19 | |
| 20 | :param encoding: |
| 21 | If you are going to decode the first 2 parts of a JWT, i.e. the header |
| 22 | or the payload, the default value "utf-8" would work fine. |
| 23 | If you are going to decode the last part i.e. the signature part, |
| 24 | it is a binary string so you should use `None` as encoding here. |
| 25 | """ |
| 26 | raw += '=' * (-len(raw) % 4) # https://stackoverflow.com/a/32517907/728675 |
| 27 | raw = str( |
| 28 | # On Python 2.7, argument of urlsafe_b64decode must be str, not unicode. |
| 29 | # This is not required on Python 3. |
| 30 | raw) |
| 31 | output = base64.urlsafe_b64decode(raw) |
| 32 | if encoding: |
| 33 | output = output.decode(encoding) |
| 34 | return output |
| 35 | |
| 36 | base64decode = decode_part # Obsolete. For backward compatibility only. |
| 37 |
no outgoing calls