Challenge - 5 Problems
JWT Token Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI JWT token creation snippet?
Consider this FastAPI code that creates a JWT token. What does the
token variable contain after running this code?FastAPI
from fastapi import FastAPI from jose import jwt app = FastAPI() SECRET_KEY = "mysecret" ALGORITHM = "HS256" payload = {"sub": "user123"} token = jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM) print(token)
Attempts:
2 left
💡 Hint
Remember jwt.encode returns a string token, not a dictionary or bytes.
✗ Incorrect
The jwt.encode function from jose returns a string JWT token encoded with the given algorithm and secret key. The payload is embedded inside the token.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a JWT token with expiration in FastAPI?
You want to create a JWT token that expires in 30 minutes. Which code snippet correctly adds the expiration claim?
FastAPI
from datetime import datetime, timedelta from jose import jwt SECRET_KEY = "secret" ALGORITHM = "HS256" expire = datetime.utcnow() + timedelta(minutes=30) payload = {"sub": "user1", "exp": expire.timestamp()} token = jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
Attempts:
2 left
💡 Hint
JWT expects the 'exp' claim as a Unix timestamp (number), not a datetime object or string.
✗ Incorrect
The 'exp' claim must be a Unix timestamp (seconds since epoch). Using expire.timestamp() converts datetime to float seconds, which is valid.
🔧 Debug
advanced2:00remaining
Why does this FastAPI JWT token creation code raise a TypeError?
Examine this code snippet. Why does it raise a TypeError when encoding the token?
from jose import jwt
payload = {"sub": "user1", "exp": 1680000000}
secret = None
token = jwt.encode(payload, secret, algorithm="HS256")
FastAPI
from jose import jwt payload = {"sub": "user1", "exp": 1680000000} secret = None token = jwt.encode(payload, secret, algorithm="HS256")
Attempts:
2 left
💡 Hint
Check the type of the secret key passed to jwt.encode.
✗ Incorrect
jwt.encode requires the secret key to be a string or bytes. Passing None causes a TypeError.
❓ state_output
advanced2:00remaining
What is the value of 'decoded_payload' after decoding this JWT token?
Given this code, what does 'decoded_payload' contain?
from jose import jwt
SECRET_KEY = "key123"
ALGORITHM = "HS256"
payload = {"sub": "userX", "role": "admin"}
token = jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
decoded_payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
FastAPI
from jose import jwt SECRET_KEY = "key123" ALGORITHM = "HS256" payload = {"sub": "userX", "role": "admin"} token = jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM) decoded_payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
Attempts:
2 left
💡 Hint
jwt.decode returns the original payload if the token is valid and secret matches.
✗ Incorrect
The decoded payload matches the original payload dictionary used to encode the token.
🧠 Conceptual
expert2:00remaining
Which statement about JWT token creation in FastAPI is true?
Select the correct statement about creating JWT tokens in FastAPI using the jose library.
Attempts:
2 left
💡 Hint
Think about the standard JWT claims and their expected formats.
✗ Incorrect
JWT 'exp' claim must be a Unix timestamp number. Secret key must be a string or bytes. HS256 requires a secret key. Payload keys can be any strings.