Consider a FastAPI app with a route protected by HTTP Basic authentication. What happens if a client tries to access the route without providing any credentials?
from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import HTTPBasic, HTTPBasicCredentials app = FastAPI() security = HTTPBasic() @app.get('/protected') def protected_route(credentials: HTTPBasicCredentials = Depends(security)): if credentials.username == 'user' and credentials.password == 'pass': return {'message': 'Access granted'} raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid credentials')
Think about what FastAPI's HTTPBasic security does when no credentials are sent.
When no credentials are provided, FastAPI's HTTPBasic dependency automatically returns a 401 Unauthorized response with a 'Not authenticated' detail. The route code is not executed.
Given the same FastAPI app as before, what is the response when the client sends username 'user' and password 'pass'?
from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import HTTPBasic, HTTPBasicCredentials app = FastAPI() security = HTTPBasic() @app.get('/protected') def protected_route(credentials: HTTPBasicCredentials = Depends(security)): if credentials.username == 'user' and credentials.password == 'pass': return {'message': 'Access granted'} raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid credentials')
Check the condition inside the route function for valid credentials.
If the username and password match, the route returns a JSON message 'Access granted' with status 200 OK.
Which code snippet correctly uses OAuth2PasswordBearer to protect a FastAPI route?
Remember how Depends is used to inject dependencies in FastAPI.
Option A correctly creates an OAuth2PasswordBearer instance and uses Depends to inject the token string into the route function parameter.
Examine the code below. The route always returns 401 Unauthorized even with correct credentials. What is the cause?
from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import HTTPBasic, HTTPBasicCredentials app = FastAPI() security = HTTPBasic() @app.get('/secure') def secure_route(credentials: HTTPBasicCredentials = Depends(security)): if credentials.username == 'user' and credentials.password == 'pass': return {'message': 'Welcome!'} raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid credentials') # Client sends username 'user' and password 'pass' but gets 401 Unauthorized
Check how HTTP Basic authentication expects credentials from the client.
The most common cause is the client not sending the Authorization header properly formatted as 'Basic base64encoded(username:password)'. The server then rejects the request with 401 Unauthorized.
Why does FastAPI use dependency injection (Depends) to handle authentication in protected routes?
Think about how dependencies help organize code in FastAPI.
Dependency injection lets you write authentication logic once and apply it to many routes easily, improving code reuse and maintainability.