0
0
FastAPIframework~20 mins

JWT token creation in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JWT Token Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
AA string token encoded with HS256 containing the payload {"sub": "user123"}
BA dictionary with keys 'sub' and 'token'
CA bytes object representing the token
DAn error because jose.jwt.encode requires a list payload
Attempts:
2 left
💡 Hint
Remember jwt.encode returns a string token, not a dictionary or bytes.
📝 Syntax
intermediate
2: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)
AUse 'exp': expire.timestamp() in payload
BUse 'exp': expire in payload
CUse 'exp': str(expire) in payload
DUse 'exp': expire.isoformat() in payload
Attempts:
2 left
💡 Hint
JWT expects the 'exp' claim as a Unix timestamp (number), not a datetime object or string.
🔧 Debug
advanced
2: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")
ABecause the algorithm 'HS256' is invalid
BBecause the payload 'exp' value is an integer, it must be a string
CBecause the secret key is None, jwt.encode expects a string or bytes
DBecause the payload is missing the 'iat' claim
Attempts:
2 left
💡 Hint
Check the type of the secret key passed to jwt.encode.
state_output
advanced
2: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])
ARaises a JWTError because decode requires expiration claim
B{"sub": "userX", "role": "admin", "alg": "HS256"}
C{"sub": "userX"}
D{"sub": "userX", "role": "admin"}
Attempts:
2 left
💡 Hint
jwt.decode returns the original payload if the token is valid and secret matches.
🧠 Conceptual
expert
2: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.
AThe secret key can be any Python object; jose.jwt.encode will convert it automatically.
BThe 'exp' claim must be a Unix timestamp number; otherwise, token validation will fail.
CJWT tokens created with HS256 do not require a secret key for encoding.
DThe payload dictionary keys must be strings starting with 'jwt_' to be valid.
Attempts:
2 left
💡 Hint
Think about the standard JWT claims and their expected formats.