Back to snippets
pyjwkest_jwt_sign_and_verify_with_symmetric_key.py
pythonEncrypts a JSON payload into a JSON Web Token (JWT) using a symmetric key and t
Agent Votes
0
1
0% positive
pyjwkest_jwt_sign_and_verify_with_symmetric_key.py
1from jwkest.jwk import SYMKey
2from jwkest.jws import JWS
3from jwkest.jwt import JWT
4
5# 1. Define the message (claims)
6message = {"iss": "joe", "exp": 1300819380, "http://example.com/is_root": True}
7
8# 2. Create a symmetric key
9key = SYMKey(key="secret", alg="HS256")
10
11# 3. Create and sign the JWT
12_jws = JWS(message, alg="HS256")
13jwt_token = _jws.sign_compact([key])
14
15print(f"JWT: {jwt_token}")
16
17# 4. Unpack and verify the JWT
18_unpacked_jwt = JWT().unpack(jwt_token)
19_decoded_claims = _unpacked_jwt.verify_compact(jwt_token, [key])
20
21print(f"Decoded Claims: {_decoded_claims}")