Under The Hood
backend fastapi FastAPI 0.100+ (PyJWT / python-jose)

OAuth2PasswordBearer, SecurityScopes, and JWT Token Verification

Last updated
Prerequisites:
JWT (JSON Web Tokens) structure
FastAPI Dependency Injection
  • fastapi
  • security
  • oauth2
  • jwt
  • security-scopes
  • rbac
  • authentication

Read at your depth

The practical view

FastAPI provides `OAuth2PasswordBearer(tokenUrl='token')` to extract Bearer tokens from the `Authorization: Bearer <token>` header. It automatically integrates with Swagger UI `/docs` (showing the Authorize lock button). You decode and verify the JWT using `pyjwt` or `python-jose`. `Security(get_current_user, scopes=['items:read'])` and `SecurityScopes` allow fine-grained permission and scope checks.

The same idea in other frameworks

fastapi equivalent
`oauth2_scheme = OAuth2PasswordBearer(tokenUrl='token')` and `Security(get_user, scopes=[...])`.

Legacy vs modern

Manual Header Parsing vs FastAPI OAuth2 + SecurityScopes

Manual authorization header parsing fails OpenAPI generation and has no scope enforcement; `SecurityScopes` provides declarative RBAC.

before → after
Manual Header Parsing
token = req.headers.get('Authorization'); jwt.decode(token) // ❌ No Swagger integration or scope validation
FastAPI Security with SecurityScopes
user = Security(get_current_user, scopes=['admin']) // ✅ NEW: Declarative RBAC + Swagger UI button

Interview gotchas

Context

Interviewers test your knowledge of security dependency wrappers.

The mechanical answer

`Depends()` executes the dependency function but does *not* pass security scopes. `Security()` behaves identically to `Depends()`, but **passes the declared `scopes` array into the `SecurityScopes` parameter** inside the dependency, and documents the required scopes for that specific endpoint in the generated OpenAPI `/docs` specification.

Trap

Using `Depends()` when your dependency expects `security_scopes: SecurityScopes` will pass an empty scope list.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.