Challenge - 5 Problems
Bearer Token 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 Bearer token check?
Given this Python Flask snippet checking a Bearer token, what will be the response if the token is 'abc123'?
Rest API
from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/data') def data(): auth = request.headers.get('Authorization') if auth == 'Bearer abc123': return jsonify({'message': 'Access granted'}) else: return jsonify({'message': 'Access denied'}), 401 # Simulate a request with header Authorization: Bearer abc123 response = None class DummyRequest: headers = {'Authorization': 'Bearer abc123'} request = DummyRequest() if request.headers.get('Authorization') == 'Bearer abc123': response = {'message': 'Access granted'} else: response = {'message': 'Access denied'} print(response)
Attempts:
2 left
💡 Hint
Check the exact match of the Authorization header value.
✗ Incorrect
The code checks if the Authorization header exactly matches 'Bearer abc123'. If yes, it returns access granted message.
🧠 Conceptual
intermediate1:30remaining
What does the 'Bearer' keyword signify in the Authorization header?
In the HTTP header 'Authorization: Bearer ', what is the role of the word 'Bearer'?
Attempts:
2 left
💡 Hint
Think about how HTTP headers specify token types.
✗ Incorrect
The word 'Bearer' tells the server that the following string is a bearer token used for authentication.
🔧 Debug
advanced2:30remaining
Why does this Bearer token check always fail?
Identify the error in this Python code snippet that checks a Bearer token from headers:
headers = {'Authorization': 'Bearer abc123'}
token = headers.get('Authorization')
if token == 'abc123':
print('Access granted')
else:
print('Access denied')
Attempts:
2 left
💡 Hint
Check what the token variable actually contains.
✗ Incorrect
The token variable contains 'Bearer abc123', so comparing it directly to 'abc123' fails.
📝 Syntax
advanced2:00remaining
Which option correctly extracts the Bearer token from the Authorization header?
Given a header string 'Authorization: Bearer abc123', which Python code correctly extracts 'abc123'?
Attempts:
2 left
💡 Hint
Split the string by space and take the second part.
✗ Incorrect
Splitting by space and taking index 1 extracts the token after 'Bearer'.
🚀 Application
expert3:00remaining
How many valid tokens are accepted by this Bearer token validation code?
Consider this Python code snippet:
valid_tokens = {'token1', 'token2', 'token3'}
header = 'Bearer token2'
if header.startswith('Bearer '):
token = header[7:]
if token in valid_tokens:
result = 'Access granted'
else:
result = 'Access denied'
else:
result = 'No token provided'
print(result)
How many tokens from the valid_tokens set will result in 'Access granted' if used in the header?
Attempts:
2 left
💡 Hint
All tokens in valid_tokens are accepted if header starts with 'Bearer '.
✗ Incorrect
Any token in valid_tokens after 'Bearer ' prefix grants access, so all 3 tokens are valid.