Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to decode a JWT token using a secret key.
IOT Protocols
decoded = jwt.decode(token, [1], algorithms=["HS256"])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a public key instead of the secret key.
Using the token itself as the key.
✗ Incorrect
The secret key is used to decode the JWT token to verify its authenticity.
2fill in blank
mediumComplete the code to create a JWT token with a payload and secret.
IOT Protocols
token = jwt.encode({"user_id": 123}, [1], algorithm="HS256") Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a public key or token variable instead of the secret key.
✗ Incorrect
The secret key is required to encode the payload into a JWT token securely.
3fill in blank
hardFix the error in the code to verify the JWT token expiration.
IOT Protocols
payload = jwt.decode(token, secret, algorithms=["HS256"], options=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect option keys or disabling expiration verification.
✗ Incorrect
Setting 'verify_exp' to True ensures the token expiration is checked during decoding.
4fill in blank
hardFill both blanks to create a JWT token with an expiration time of 1 hour.
IOT Protocols
payload = {"user": "iot_device", "exp": datetime.datetime.utcnow() [1] datetime.timedelta([2]=1)}
token = jwt.encode(payload, secret, algorithm="HS256") Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+', or using 'minutes' instead of 'hours'.
✗ Incorrect
The expiration time is set by adding 1 hour to the current UTC time.
5fill in blank
hardFill all three blanks to extract the user ID from a decoded JWT payload safely.
IOT Protocols
user_id = payload.get([1], [2]) if payload and payload.get([3]) else None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys or not providing a default value.
✗ Incorrect
This code safely gets the 'user_id' from the payload or returns None if not present.