Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a Bearer token in API authentication?
A Bearer token is a security token that a client sends to a server to prove its identity. It is called 'Bearer' because whoever holds the token can access the protected resource.
Click to reveal answer
beginner
How does FastAPI extract a Bearer token from an HTTP request?
FastAPI uses the OAuth2PasswordBearer class to declare a dependency that extracts the Bearer token from the Authorization header automatically.
Click to reveal answer
intermediate
Show a simple FastAPI dependency that reads a Bearer token from the Authorization header.
from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
async def get_token(token: str = Depends(oauth2_scheme)):
return token
Click to reveal answer
intermediate
Why should you validate the Bearer token after extracting it in FastAPI?
Extracting the token only gets the string. You must validate it to check if it is correct, not expired, and belongs to an authorized user before allowing access.
Click to reveal answer
beginner
What HTTP header carries the Bearer token in a request?
The Authorization header carries the Bearer token, formatted as: Authorization: Bearer <token>
Click to reveal answer
Which FastAPI class helps extract Bearer tokens from requests?
ADepends
BHTTPBearer
CAPIKeyHeader
DOAuth2PasswordBearer
✗ Incorrect
OAuth2PasswordBearer is designed to extract Bearer tokens from the Authorization header in FastAPI.
What prefix must the Authorization header contain for a Bearer token?
AToken
BBearer
CBasic
DAPIKey
✗ Incorrect
Bearer tokens are sent with the prefix 'Bearer' in the Authorization header.
After extracting a Bearer token, what is the next important step?
AValidate the token's authenticity and expiry
BSend it back to the client
CStore it in a database
DIgnore it
✗ Incorrect
Validation ensures the token is valid and the user is authorized.
In FastAPI, how do you declare a dependency to get the Bearer token?
AUse Depends with APIKeyQuery
BUse Depends with HTTPBasic
CUse Depends with OAuth2PasswordBearer instance
DUse Depends with Cookie
✗ Incorrect
OAuth2PasswordBearer is used with Depends to extract Bearer tokens.
What HTTP status code should you return if the Bearer token is missing or invalid?
A401 Unauthorized
B200 OK
C403 Forbidden
D404 Not Found
✗ Incorrect
401 Unauthorized indicates the client must authenticate with a valid token.
Explain how FastAPI handles Bearer token extraction and validation in a simple API endpoint.
Think about the Authorization header and FastAPI's dependency system.
You got /4 concepts.
Describe the role of the Authorization header and the Bearer token format in API security.
Focus on how the token is sent and why it matters.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Bearer token in FastAPI?
easy
A. To serve static files efficiently
B. To format JSON responses automatically
C. To speed up database queries
D. To securely identify and authorize API requests
Solution
Step 1: Understand Bearer token role
Bearer tokens are used to prove the client has permission to access protected routes.
Step 2: Identify purpose in FastAPI
FastAPI uses Bearer tokens to check authorization before allowing access to API endpoints.
Final Answer:
To securely identify and authorize API requests -> Option D
Quick Check:
Bearer token = Authorization [OK]
Hint: Bearer tokens are for security and access control [OK]
Common Mistakes:
Confusing token with response formatting
Thinking token speeds up database
Assuming token serves static files
2. Which FastAPI class is used to extract a Bearer token from the Authorization header?
easy
A. OAuth2PasswordBearer
B. HTTPBasicCredentials
C. OAuth2PasswordRequestForm
D. APIKeyHeader
Solution
Step 1: Recall FastAPI token extraction classes
OAuth2PasswordBearer is designed to read Bearer tokens from the Authorization header.
Step 2: Match class to Bearer token usage
OAuth2PasswordRequestForm is for form data, HTTPBasicCredentials is for basic auth, APIKeyHeader is for API keys, so only OAuth2PasswordBearer fits Bearer tokens.
5. You want to protect a FastAPI route so only requests with the exact Bearer token "secret123" are allowed. Which code snippet correctly implements this?
hard
A. async def protected_route(token: str = Depends(oauth2_scheme)):
if token == None:
return {"message": "Access granted"}
raise HTTPException(status_code=401, detail="Unauthorized")
B. async def protected_route(token: str = Depends(oauth2_scheme)):
if token != "secret123":
raise HTTPException(status_code=401, detail="Unauthorized")
return {"message": "Access granted"}
C. async def protected_route(token: str):
if token == "secret123":
return {"message": "Access granted"}
raise HTTPException(status_code=403, detail="Forbidden")
D. async def protected_route(token: str = Depends(oauth2_scheme)):
if token == "Bearer secret123":
return {"message": "Access granted"}
raise HTTPException(status_code=401, detail="Unauthorized")
Solution
Step 1: Use OAuth2PasswordBearer dependency
We must use Depends(oauth2_scheme) to extract the token from the Authorization header.
Step 2: Check token value correctly
The token string is just the token without 'Bearer ' prefix, so compare directly to "secret123" and raise 401 if not matching.
Final Answer:
async def protected_route(token: str = Depends(oauth2_scheme)):
if token != "secret123":
raise HTTPException(status_code=401, detail="Unauthorized")
return {"message": "Access granted"} -> Option B
Quick Check:
Compare token string directly to "secret123" [OK]
Hint: Compare token string directly, no 'Bearer ' prefix included [OK]
Common Mistakes:
Comparing token to 'Bearer secret123' including prefix