0
0
Rest APIprogramming~20 mins

JWT structure and flow in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JWT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of decoding the JWT payload?

Given this JWT token payload encoded in Base64Url:

eyJ1c2VyX2lkIjogMTIzLCJyb2xlIjogImFkbWluIn0

What is the decoded JSON payload?

A{"user_id": "123", "role": "admin"}
B{"user_id": 123, "role": "user"}
C{"userid": 123, "role": "admin"}
D{"user_id": 123, "role": "admin"}
Attempts:
2 left
💡 Hint

Base64Url decoding converts the string back to JSON. Pay attention to key names and value types.

🧠 Conceptual
intermediate
1:00remaining
Which step in JWT flow verifies the token's integrity?

In the JWT authentication flow, which step ensures the token has not been tampered with?

AServer verifies the token signature using the secret key
BClient stores the token in local storage
CServer decodes the token payload to read user info
DClient sends the token in the Authorization header
Attempts:
2 left
💡 Hint

Think about how the server knows the token is authentic and unchanged.

🔧 Debug
advanced
2:00remaining
Why does this JWT verification code fail?

Consider this Python code snippet verifying a JWT token:

import jwt

token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.invalidsignature'
secret = 'mysecret'

try:
    payload = jwt.decode(token, secret, algorithms=['HS256'])
    print('Valid token')
except jwt.InvalidSignatureError:
    print('Invalid signature')
except Exception as e:
    print('Error:', e)

What is the output when running this code?

AError: Not enough segments
BValid token
CInvalid signature
DError: Decode error
Attempts:
2 left
💡 Hint

The token's signature part is clearly invalid. What exception does the library raise?

📝 Syntax
advanced
1:30remaining
Which code correctly creates a JWT token with payload and secret?

Choose the correct Python code snippet that creates a JWT token with payload {"user": "alice"} and secret "key123" using HS256 algorithm.

Ajwt.encode({"user": "alice"}, "key123", algorithm="HS256")
Bjwt.encode("user=alice", "key123", algorithm="HS256")
Cjwt.encode({user: "alice"}, "key123", algorithm="HS256")
Djwt.encode({"user": "alice"}, key="key123", algo="HS256")
Attempts:
2 left
💡 Hint

Check the function signature and argument names carefully.

🚀 Application
expert
1:30remaining
How many items are in the JWT payload after decoding this token?

Given this JWT token (header.payload.signature):

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYm9iIiwiaWQiOjQ1LCJleHAiOjE2ODk0MjAwMDB9.somesignature

After decoding the payload, how many key-value pairs does it contain?

A2
B3
C4
D1
Attempts:
2 left
💡 Hint

Decode the payload part and count the keys.