Consider this FastAPI endpoint that requires a bearer token for access. What will be the response if the token is valid?
from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials app = FastAPI() security = HTTPBearer() @app.get("/secure-data") async def secure_data(credentials: HTTPAuthorizationCredentials = Depends(security)): if credentials.scheme != "Bearer" or credentials.credentials != "validtoken123": raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") return {"message": "Access granted"}
Check the condition that validates the token and what the function returns if the token matches.
If the token matches "validtoken123" and the scheme is "Bearer", the endpoint returns a JSON message confirming access. Otherwise, it raises a 401 error.
Given a FastAPI dependency to extract a bearer token, which code snippet correctly retrieves the token string?
from fastapi import Depends from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials security = HTTPBearer() def get_token(credentials: HTTPAuthorizationCredentials = Depends(security)): # Extract token here pass
Look at the attributes of HTTPAuthorizationCredentials to find the token string.
The token string is stored in the credentials attribute. Other attributes like scheme hold the type (e.g., 'Bearer').
Review the code below. The endpoint should accept a bearer token 'secrettoken' but always returns 401 Unauthorized. What is the cause?
from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials app = FastAPI() security = HTTPBearer() @app.get("/data") async def get_data(credentials: HTTPAuthorizationCredentials = Depends(security)): if credentials.scheme != "Bearer" or credentials.credentials != "secrettoken": raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized") return {"data": "secret info"}
Check the exact string used to compare the scheme in the Authorization header.
The scheme in the Authorization header is 'Bearer' with uppercase B. Comparing with lowercase 'bearer' causes the condition to fail always.
Given this FastAPI endpoint using HTTPBearer, what response does the client receive if the Authorization header is missing?
from fastapi import FastAPI, Depends from fastapi.security import HTTPBearer app = FastAPI() security = HTTPBearer() @app.get("/info") async def info(credentials = Depends(security)): return {"token": credentials.credentials}
Consider how HTTPBearer behaves when no Authorization header is provided.
HTTPBearer automatically returns a 401 Unauthorized error if the Authorization header is missing.
Choose the most accurate description of what HTTPBearer does in FastAPI applications.
Think about whether HTTPBearer checks token correctness or just extracts it.
HTTPBearer only extracts the token and checks the header format. It does not validate token content or authenticate the user. Validation must be done separately.
