0
0
Rest APIprogramming~10 mins

JWT structure and flow in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to decode the JWT token header.

Rest API
import jwt

token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
header = jwt.get_unverified_header([1])
print(header)
Drag options to blanks, or click blank then click option'
Atoken
Bsignature
Csecret
Dpayload
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only the payload or signature instead of the full token.
Using the secret key instead of the token.
2fill in blank
medium

Complete the code to verify and decode the JWT token payload using the secret key.

Rest API
import jwt

secret_key = 'mysecret'
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
payload = jwt.decode(token, [1], algorithms=['HS256'])
print(payload)
Drag options to blanks, or click blank then click option'
Atoken
Bsecret_key
Cpayload
Dheader
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the token itself instead of the secret key.
Passing the payload or header instead of the secret key.
3fill in blank
hard

Fix the error in the code to correctly create a JWT token with a payload and secret.

Rest API
import jwt

payload = {'user_id': 123}
secret = 'mysecret'
token = jwt.encode([1], secret, algorithm='HS256')
print(token)
Drag options to blanks, or click blank then click option'
Asecret
Btoken
Cpayload
Dalgorithm
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the secret key as the first argument instead of the payload.
Passing the token variable which is not defined yet.
4fill in blank
hard

Fill both blanks to create a JWT token and then decode it correctly.

Rest API
import jwt

payload = {'role': 'admin'}
secret = 'topsecret'
token = jwt.encode([1], [2], algorithm='HS256')
decoded = jwt.decode(token, secret, algorithms=['HS256'])
print(decoded)
Drag options to blanks, or click blank then click option'
Apayload
Bsecret
Ctoken
Dalgorithm
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of payload and secret.
Using the token variable before it is created.
5fill in blank
hard

Fill all three blanks to create a JWT token with a payload, decode it, and extract the user ID.

Rest API
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)
Drag options to blanks, or click blank then click option'
Apayload
Bsecret
C'user_id'
D'exp'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exp' instead of 'user_id' to extract the user ID.
Swapping payload and secret in encode function.