Created
December 8, 2022 08:31
-
-
Save MrYakobo/9fa52f13fc10cc71f0f236305a857593 to your computer and use it in GitHub Desktop.
Decode a jwt from stdin, without using jwt.io or external libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import base64 | |
import json | |
import fileinput | |
for jwt in fileinput.input(): | |
parts = jwt.split(".") | |
# '==' is padding | |
parts = dict( | |
header = parts[0] + '==', | |
data = parts[1] + '==', | |
signature = parts[2] + '==' | |
) | |
decoded_parts = dict() | |
for name, part in parts.items(): | |
decoded = base64.urlsafe_b64decode(part.encode('utf8')) | |
try: | |
# parse the json string | |
decoded_parts[name] = json.loads(decoded.decode('utf8')) | |
except UnicodeDecodeError as e: | |
# signatures are binary, so don't try to unicode-decode them | |
decoded_parts[name] = part | |
print(json.dumps(decoded_parts, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment