Last active
December 26, 2023 10:33
-
-
Save andanhm/90d9a7ed9d8dd8f1be538d014f8e914f to your computer and use it in GitHub Desktop.
Decodes JWT base64 encoded token without the JWT singing key (Offline)
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
import base64 | |
import json | |
def decode(token): | |
# Decodes JWT base64 encoded token | |
try: | |
alg, payload, signature = token.split(".") | |
payload += '=' * (-len(payload) % 4) | |
decode_payload = json.loads(base64.b64decode(payload).decode("utf-8")) | |
if "client_id" in decode_payload.keys(): | |
return decode_payload | |
else: | |
return None | |
except (Exception, TypeError): | |
return None | |
if __name__ == "__main__": | |
print(decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBpbmZvIjp7ImlkIjoicGFydG5lci1hcHAiLCJuYW1lIjoiUGFydG5lciBBcHAifSwiYXVkIjoiaWFtIiwiY2xpZW50X2lkIjoicGFydG5lci1hcHAiLCJjbGllbnRfdHlwZSI6ImFwcCIsImVudiI6Im90ZSIsImV4cCI6NDY2ODc2MzM3NiwiZXhwaXJlcyI6MzE1NTY5MjYwMCwiaWF0IjoxNTEzMDcwNzc2LCJpc3MiOiJpbjppYW06Iiwic2NvcGUiOiJzZGsifQ.awtZSwRj9OJLxz5XSbC_2ALzpv2xViJ74AHbZBhN8Xg")) | |
print(decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBpbmZvIjp7ImlkIjoicGFydG5lci1hcHAiLCJuYW1lIjoiUGFydG5lciBBcHAifSwiYXVkIjoiaWFtIiwiY2xpZW50X2lkIjoicGFydG5lci1hcHAiLCJjbGllbnRfdHlwZSI6ImFwcCIsImVudiI6Im90ZSIsImV4cCI6NDY2OTM1OTM3NiwiZXhwaXJlcyI6MzE1NTY5MjYwMCwiaWF0IjoxNTEzNjY2Nzc2LCJpc3MiOiJpbjppYW06Iiwic2NvcGUiOiJtYW5pZmVzdCJ9.g25qSbGuMhVq5Q2Hm2eNzCMlbcM7hXzARqhyb776x_k")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment