1. import argparse
  2. import base64
  3. import json
  4. import sys
  5. def decode_token(token):
  6. """Decodes an authentication token.
  7. Args:
  8. token: The authentication token to decode (base64 encoded).
  9. Returns:
  10. A dictionary containing the decoded token data, or None if decoding fails.
  11. """
  12. try:
  13. decoded_bytes = base64.b64decode(token)
  14. decoded_str = decoded_bytes.decode('utf-8')
  15. return json.loads(decoded_str)
  16. except (base64.binascii.Error, json.JSONDecodeError):
  17. return None
  18. def main():
  19. """Main function to handle CLI arguments and decode tokens."""
  20. parser = argparse.ArgumentParser(description="Decode authentication tokens for monitoring.")
  21. parser.add_argument("token", help="The authentication token to decode.")
  22. args = parser.parse_args()
  23. decoded_token = decode_token(args.token)
  24. if decoded_token:
  25. print(json.dumps(decoded_token, indent=4)) # Pretty print the JSON
  26. else:
  27. print("Decoding failed. Invalid token format.")
  28. sys.exit(1)
  29. if __name__ == "__main__":
  30. main()

Add your comment