Bird
0
0

Consider the following pseudocode for API key validation:

medium📝 Analysis Q4 of 15
Microservices - Authentication and Authorization
Consider the following pseudocode for API key validation:
function checkApiKey(request) {
  key = request.headers['X-API-Key'];
  if (key === '12345') {
    return 'Authorized';
  } else {
    return 'Unauthorized';
  }
}
print(checkApiKey({headers: {'X-API-Key': 'abcde'}}));

What will be the output of this code?
AAuthorized
BError: key not found
CUnauthorized
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Identify the key in the request

    The request header contains 'X-API-Key' with value 'abcde'.
  2. Step 2: Compare with expected key

    The code checks if key equals '12345'. Since 'abcde' != '12345', condition fails.
  3. Step 3: Determine return value

    Because the key is invalid, the function returns 'Unauthorized'.
  4. Final Answer:

    Unauthorized -> Option C
  5. Quick Check:

    Key mismatch leads to 'Unauthorized' response [OK]
Quick Trick: Mismatch key returns 'Unauthorized' [OK]
Common Mistakes:
MISTAKES
  • Assuming any key returns 'Authorized'
  • Confusing header names
  • Ignoring strict equality check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes