Authentication & Security
Fenrir provides FastAPI-compatible security dependencies for API key, token, and OAuth2 authentication. All security classes inherit from SecurityBase and automatically register their OpenAPI models in the schema.
Class Hierarchy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
SecurityBase
All security classes inherit from SecurityBase, which provides common OpenAPI metadata parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name if not provided) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
API Key Authentication
API Key from Header
1 2 3 4 5 6 7 8 9 10 11 12 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
required | Header name to extract API key from |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
Return type: str | None
auto_error=False behavior: Returns None instead of raising HTTPException when the API key is missing.
API Key from Cookie
1 2 3 4 5 6 7 8 9 10 11 12 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
required | Cookie name to extract API key from |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
Return type: str | None
auto_error=False behavior: Returns None instead of raising HTTPException when the API key is missing.
API Key from Query Parameter
1 2 3 4 5 6 7 8 9 10 11 12 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
required | Query parameter name to extract API key from |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
Return type: str | None
auto_error=False behavior: Returns None instead of raising HTTPException when the API key is missing.
HTTPBase
HTTPBase is the base class for HTTP authentication schemes (HTTPBasic, HTTPBearer, HTTPDigest). It provides additional parameters beyond SecurityBase:
| Parameter | Type | Default | Description |
|---|---|---|---|
scheme |
str |
required | HTTP authentication scheme name (e.g., "basic", "bearer", "digest") |
realm |
str \| None |
None |
Realm for WWW-Authenticate header |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Bearer Token Authentication
1 2 3 4 5 6 7 8 9 10 11 12 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
bearerFormat |
str \| None |
None |
Format description shown in OpenAPI (e.g., "JWT") |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: str | None
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not contain a valid
Bearertoken
Basic Authentication
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
realm |
str \| None |
None |
Realm for WWW-Authenticate header |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: tuple[str, str] | None (username, password)
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not contain valid Basic credentials
- The credentials cannot be decoded or parsed
When auto_error=True and realm is provided, the WWW-Authenticate header is included in the 401 response.
Digest Authentication
1 2 3 4 5 6 7 8 9 10 11 12 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
realm |
str \| None |
None |
Realm for WWW-Authenticate header |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: dict | None (parsed digest fields as key-value pairs)
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not start with
Digest
When auto_error=True and realm is provided, the WWW-Authenticate header is included in the 401 response.
OAuth2
OAuth2 is the base class for OAuth2 authentication schemes. It provides additional parameters beyond SecurityBase:
| Parameter | Type | Default | Description |
|---|---|---|---|
flows |
dict[str, Any] |
required | OAuth2 flows configuration (e.g., {"password": {"tokenUrl": "token", "scopes": {}}}) |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not contain a valid
Bearertoken
OAuth2 Password Flow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
tokenUrl |
str |
required | URL to obtain the access token |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: str | None (the access token)
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not contain a valid
Bearertoken
OAuth2 Authorization Code Flow
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
authorizationUrl |
str |
required | URL to obtain the authorization code |
tokenUrl |
str |
required | URL to exchange code for access token |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: str | None (the access token)
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not contain a valid
Bearertoken
OpenID Connect
1 2 3 4 5 6 7 8 9 10 11 12 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
openIdConnectUrl |
str |
required | OpenID Connect discovery URL |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: str | None (the bearer token)
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The
Authorizationheader is missing - The header does not contain a valid
Bearertoken
WebSocket Token Authentication
Authenticate WebSocket connections using tokens from headers or query parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
header_name |
str |
"authorization" |
Header name to extract token from |
query_param |
str |
"token" |
Query parameter name to extract token from |
scheme_name |
str \| None |
None |
Name shown in OpenAPI schema (defaults to class name) |
description |
str \| None |
None |
Description shown in OpenAPI schema |
auto_error |
bool |
True |
Raise 401 if authentication fails; return None if False |
Return type: str | None (the token)
Token extraction logic:
- First checks the header for a
Bearer <token>format - If no Bearer token found, uses the raw header value
- Falls back to the query parameter if no header token found
auto_error=False behavior: Returns None instead of raising HTTPException when:
- The WebSocket connection has no scope (invalid WebSocket object)
- No token is found in either the header or query parameter
OpenAPI Integration
Security classes automatically register their OpenAPI security scheme models. Each class exposes a model attribute containing the OpenAPI security scheme definition:
| Class | OpenAPI Type | Scheme | Additional Fields |
|---|---|---|---|
APIKeyHeader |
apiKey |
in: header |
name (header name) |
APIKeyCookie |
apiKey |
in: cookie |
name (cookie name) |
APIKeyQuery |
apiKey |
in: query |
name (query param name) |
HTTPBasic |
http |
basic |
realm (if provided) |
HTTPBearer |
http |
bearer |
bearerFormat (if provided) |
HTTPDigest |
http |
digest |
realm (if provided) |
OAuth2PasswordBearer |
oauth2 |
password flow |
tokenUrl |
OAuth2AuthorizationCodeBearer |
oauth2 |
authorizationCode flow |
authorizationUrl, tokenUrl |
OpenIDConnect |
openIdConnect |
N/A | openIdConnectUrl |
WebSocketTokenAuth |
http |
bearer |
N/A |
All classes include description in the OpenAPI model if provided via the description parameter.
Return Types Summary
| Class | Return Type | Notes |
|---|---|---|
APIKeyHeader |
str \| None |
None when auto_error=False and key missing |
APIKeyCookie |
str \| None |
None when auto_error=False and key missing |
APIKeyQuery |
str \| None |
None when auto_error=False and key missing |
HTTPBearer |
str \| None |
None when auto_error=False and token invalid/missing |
HTTPBasic |
tuple[str, str] \| None |
None when auto_error=False and credentials invalid/missing |
HTTPDigest |
dict \| None |
None when auto_error=False and digest invalid/missing |
OAuth2PasswordBearer |
str \| None |
None when auto_error=False and token invalid/missing |
OAuth2AuthorizationCodeBearer |
str \| None |
None when auto_error=False and token invalid/missing |
OpenIDConnect |
str \| None |
None when auto_error=False and token invalid/missing |
WebSocketTokenAuth |
str \| None |
None when auto_error=False and token missing |
Error Behavior
When auto_error=True (default), all security classes raise an HTTPException with status code 401 and detail "Not authenticated" or "Invalid credentials" on failure.
When auto_error=False, all security classes return None on failure, allowing the endpoint to handle the missing authentication gracefully (e.g., returning a default value, redirecting, or providing anonymous access).