import argparse
import jwt
import time
def validate_token(token, secret):
"""
Validates an authentication token.
Args:
token (str): The authentication token to validate.
secret (str): The secret key used to sign the token.
Returns:
dict: A dictionary containing the decoded token data if valid,
None otherwise.
"""
try:
# Decode the token
decoded_token = jwt.decode(token, secret, algorithms=["HS256"])
return decoded_token
except jwt.ExpiredSignatureError:
print("Token has expired.")
return None
except jwt.InvalidTokenError:
print("Invalid token.")
return None
except jwt.DecodeError:
print("Could not decode token.")
return None
except Exception as e:
print(f"An unexpected error occurred: {e}")
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Validate authentication tokens.")
parser.add_argument("token", help="The authentication token to validate.")
parser.add_argument("secret", help="The secret key used to sign the token.")
args = parser.parse_args()
# Validate the token
decoded_token = validate_token(args.token, args.secret)
if decoded_token:
print("Token is valid.")
print(f"Decoded token: {decoded_token}")
else:
print("Token is invalid.")
Add your comment