In Python, you can sign and verify JSON Web Tokens (JWT) using the `PyJWT` library. Below is an example of how to create a JWT, sign it, and then verify it.
import jwt
import datetime
# Secret key for signing the JWT
secret_key = "your_secret_key"
# Create a payload with an expiration time
payload = {
'user_id': 123,
'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=30) # Token valid for 30 seconds
}
# Sign the JWT
token = jwt.encode(payload, secret_key, algorithm='HS256')
print("Generated JWT:", token)
# Verify the JWT
try:
decoded_payload = jwt.decode(token, secret_key, algorithms=['HS256'])
print("Decoded JWT Payload:", decoded_payload)
except jwt.ExpiredSignatureError:
print("Token has expired")
except jwt.InvalidTokenError:
print("Invalid Token")
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?