Complete the code to decode the JWT token header.
import jwt token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' header = jwt.get_unverified_header([1]) print(header)
The jwt.get_unverified_header() function takes the JWT token string to decode its header part without verifying the signature.
Complete the code to verify and decode the JWT token payload using the secret key.
import jwt secret_key = 'mysecret' token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' payload = jwt.decode(token, [1], algorithms=['HS256']) print(payload)
The jwt.decode() function requires the secret key to verify the token's signature and decode the payload.
Fix the error in the code to correctly create a JWT token with a payload and secret.
import jwt payload = {'user_id': 123} secret = 'mysecret' token = jwt.encode([1], secret, algorithm='HS256') print(token)
The jwt.encode() function expects the payload dictionary as the first argument to create the token.
Fill both blanks to create a JWT token and then decode it correctly.
import jwt payload = {'role': 'admin'} secret = 'topsecret' token = jwt.encode([1], [2], algorithm='HS256') decoded = jwt.decode(token, secret, algorithms=['HS256']) print(decoded)
First, the payload dictionary is encoded with the secret key to create the token. Then the token is decoded using the same secret key.
Fill all three blanks to create a JWT token with a payload, decode it, and extract the user ID.
import jwt payload = {'user_id': 42, 'exp': 1700000000} secret = 'secret123' token = jwt.encode([1], [2], algorithm='HS256') decoded = jwt.decode(token, secret, algorithms=['HS256']) user_id = decoded[[3]] print(user_id)
The payload is encoded with the secret key to create the token. Then the token is decoded with the same secret. Finally, the user ID is extracted from the decoded payload using the key 'user_id'.