0
0
FastAPIframework~10 mins

JWT token creation in FastAPI - Interactive Code Practice

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

Complete the code to import the JWT encode function from the correct library.

FastAPI
from jose import [1]
Drag options to blanks, or click blank then click option'
Adecode
Bencode
Cjwt
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Importing encode directly instead of jwt module
Using decode instead of jwt
Importing from wrong library
2fill in blank
medium

Complete the code to define the secret key used for signing the JWT token.

FastAPI
SECRET_KEY = '[1]'
Drag options to blanks, or click blank then click option'
Ajwtsecret
Bmysecretkey123
C123456
Dtokenkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer instead of a string
Leaving the secret key empty
Using a too simple or insecure key
3fill in blank
hard

Fix the error in the code to correctly encode the JWT token with payload and secret key.

FastAPI
token = jwt.[1](payload, SECRET_KEY, algorithm='HS256')
Drag options to blanks, or click blank then click option'
Aencode
Bdecode
Csign
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using decode instead of encode
Using a non-existent function like sign or create
Misspelling the function name
4fill in blank
hard

Fill both blanks to create a payload dictionary with username and expiration time.

FastAPI
payload = {'sub': [1], 'exp': [2]
Drag options to blanks, or click blank then click option'
A'user123'
Bdatetime.utcnow() + timedelta(minutes=30)
C'admin'
Dtime.time() + 1800
Attempts:
3 left
💡 Hint
Common Mistakes
Using time.time() instead of datetime for expiration
Putting username without quotes
Using wrong keys in the payload
5fill in blank
hard

Fill all three blanks to import needed modules and create a JWT token with expiration.

FastAPI
from datetime import [1], [2]

payload = {'sub': 'admin', 'exp': datetime.utcnow() + [3](minutes=15)}
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
Drag options to blanks, or click blank then click option'
Adatetime
Btimedelta
Dtime
Attempts:
3 left
💡 Hint
Common Mistakes
Importing time instead of timedelta
Using timedelta without import
Confusing datetime and timedelta order