import argparse
import base64
import json
import sys
def decode_token(token):
"""Decodes an authentication token.
Args:
token: The authentication token to decode (base64 encoded).
Returns:
A dictionary containing the decoded token data, or None if decoding fails.
"""
try:
decoded_bytes = base64.b64decode(token)
decoded_str = decoded_bytes.decode('utf-8')
return json.loads(decoded_str)
except (base64.binascii.Error, json.JSONDecodeError):
return None
def main():
"""Main function to handle CLI arguments and decode tokens."""
parser = argparse.ArgumentParser(description="Decode authentication tokens for monitoring.")
parser.add_argument("token", help="The authentication token to decode.")
args = parser.parse_args()
decoded_token = decode_token(args.token)
if decoded_token:
print(json.dumps(decoded_token, indent=4)) # Pretty print the JSON
else:
print("Decoding failed. Invalid token format.")
sys.exit(1)
if __name__ == "__main__":
main()
Add your comment