Challenge - 5 Problems
Token Refresh Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this token refresh simulation?
Consider a simple token refresh function that returns a new access token if the refresh token is valid. What will be the output of the code below?
Rest API
def refresh_token(refresh_token): valid_refresh_tokens = {"abc123": "new_access_token_1", "def456": "new_access_token_2"} return valid_refresh_tokens.get(refresh_token, "Invalid refresh token") print(refresh_token("abc123"))
Attempts:
2 left
💡 Hint
Look at the dictionary keys and what the function returns when the key is found.
✗ Incorrect
The function checks if the refresh token exists in the dictionary. Since "abc123" is a key, it returns the corresponding value "new_access_token_1".
🧠 Conceptual
intermediate1:30remaining
Which statement best describes the purpose of a refresh token?
Choose the best description of what a refresh token does in an authentication system.
Attempts:
2 left
💡 Hint
Think about what happens when an access token expires.
✗ Incorrect
A refresh token allows the client to get a new access token without asking the user to log in again, improving security and user experience.
🔧 Debug
advanced2:30remaining
Why does this token refresh code raise an error?
The code below is intended to refresh an access token but raises an error. What is the cause?
Rest API
valid_tokens = {"r1": "token1", "r2": "token2"}
def refresh_token(refresh_token):
if refresh_token in valid_tokens:
return valid_tokens[refresh_token]
else:
return "Invalid token"
print(refresh_token("r1"))Attempts:
2 left
💡 Hint
Check the order of variable and function definitions.
✗ Incorrect
The function uses valid_tokens before it is defined, causing a NameError at runtime.
📝 Syntax
advanced2:30remaining
Which option correctly implements a token refresh function with error handling?
Select the code snippet that correctly returns a new access token or raises an exception if the refresh token is invalid.
Attempts:
2 left
💡 Hint
Look carefully at the syntax of if statements and raising exceptions.
✗ Incorrect
Option A uses correct syntax for if statement and raising an exception. Options B and C miss colons, and D returns an exception object instead of raising it.
🚀 Application
expert3:00remaining
How many tokens will be valid after this refresh sequence?
Given the code below, how many tokens remain valid after refreshing 'r1' and 'r3'?
Rest API
valid_tokens = {"r1": "token1", "r2": "token2", "r3": "token3"}
# Refresh function invalidates old refresh token and issues a new one
def refresh_token(token):
if token not in valid_tokens:
return "Invalid token"
new_token = f"token_new_{token}"
del valid_tokens[token]
valid_tokens[f"r_new_{token}"] = new_token
return new_token
refresh_token("r1")
refresh_token("r3")Attempts:
2 left
💡 Hint
Count tokens before and after refresh calls carefully.
✗ Incorrect
Initially 3 tokens exist. Each refresh deletes one old token and adds one new token. After two refreshes, total tokens = 3 - 2 + 2 = 3. But keys are different. The question asks how many tokens remain valid, which is 3. However, the new tokens added are different keys, so total keys after refresh are 3 (r2, r_new_r1, r_new_r3). So answer is 3.