Security
Declare a FastAPI Security dependency.
The only difference with a regular dependency is that it can declare OAuth2
scopes that will be integrated with OpenAPI and the automatic UI docs (by default
at /docs).
It takes a single "dependable" callable (like a function).
Don't call it directly, FastAPI will call it for you.
Read more about it in the FastAPI docs for Security and in the FastAPI docs for OAuth2 scopes.
Example
from typing import Annotated
from fastapi import Security, FastAPI
from .db import User
from .security import get_current_active_user
app = FastAPI()
@app.get("/users/me/items/")
async def read_own_items(
current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])]
):
return [{"item_id": "Foo", "owner": current_user.username}]
def Security(
dependency: Callable[..., Any] | None = None,
scopes: Sequence[str] | None = None,
use_cache: bool = True
) - > Any
Declare a FastAPI Security dependency. The only difference with a regular dependency is that it can declare OAuth2 scopes that will be integrated with OpenAPI and the automatic UI docs (by default at /docs).
Parameters
| Name | Type | Description |
|---|---|---|
| dependency | `Callable[..., Any] | None` = None |
| scopes | `Sequence[str] | None` = None |
| use_cache | bool = True | Determines whether the dependency result should be cached and reused if requested multiple times within the same request cycle. |
Returns
| Type | Description |
|---|---|
Any | A dependency parameter descriptor that FastAPI uses to manage security requirements and OAuth2 scopes for a path operation. |