Bird
Raised Fist0

Identify the bug in this token refresh function snippet:

medium📝 Debug Q6 of Q15
Rest API - Authentication and Authorization
Identify the bug in this token refresh function snippet:
def refresh_token(request):
    token = request.get('refresh_token')
    if token is None:
        return {'error': 'No token provided'}, 400
    if token == 'valid':
        return {'access_token': 'new_token'}, 200
    else:
        return {'error': 'Invalid token'}, 401

# Called with request = {'refresh_token': 'valid_token'}
AThe token comparison uses wrong string 'valid' instead of 'valid_token'
BThe function returns 200 for invalid tokens
CThe function does not handle missing token
DThe function uses incorrect HTTP status codes
Step-by-Step Solution
Solution:
  1. Step 1: Check token comparison logic

    The code compares token to 'valid' but input is 'valid_token', so condition fails.
  2. Step 2: Identify effect of mismatch

    Because of mismatch, valid tokens are treated as invalid, returning 401 incorrectly.
  3. Final Answer:

    The token comparison uses wrong string 'valid' instead of 'valid_token' -> Option A
  4. Quick Check:

    Token string mismatch causes logic error [OK]
Quick Trick: Match token strings exactly in comparisons [OK]
Common Mistakes:
MISTAKES
  • Using partial or incorrect token strings
  • Ignoring None token check
  • Mixing up status codes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes