AuthXDependency#
authx.dependencies.AuthXDependency #
Bases: Generic[T]
A dependency class for managing authentication-related operations within a request-response context.
Provides a convenient interface for creating tokens, setting and unsetting cookies, and retrieving the current authenticated subject.
ATTRIBUTE | DESCRIPTION |
---|---|
request | The HTTP request associated with the current authentication context. TYPE: |
response | The HTTP response associated with the current authentication context. TYPE: |
METHOD | DESCRIPTION |
---|---|
create_access_token | Generate an access token for a given user. |
create_refresh_token | Generate a refresh token for a given user. |
set_access_cookies | Set access token cookies in the response. |
set_refresh_cookies | Set refresh token cookies in the response. |
unset_access_cookies | Remove access token cookies from the response. |
unset_refresh_cookies | Remove refresh token cookies from the response. |
unset_cookies | Remove all authentication-related cookies from the response. |
get_current_subject | Asynchronously retrieve the currently authenticated subject. |
Initialize the authentication dependency with request, response, and security context.
Sets up the core components required for managing authentication operations within the current request-response cycle.
PARAMETER | DESCRIPTION |
---|---|
_from | TYPE: |
request | TYPE: |
response | TYPE: |
PARAMETER | DESCRIPTION |
---|---|
_from | The AuthX instance managing authentication mechanisms. TYPE: |
request | The incoming HTTP request object. TYPE: |
response | The HTTP response object to be potentially modified during authentication. TYPE: |
Source code in authx/dependencies.py
request property
#
Retrieve the HTTP request associated with the current authentication context.
Provides read-only access to the request object used during authentication processing.
RETURNS | DESCRIPTION |
---|---|
Request | The HTTP request object stored in the authentication dependency. |
response property
#
Retrieve the HTTP response associated with the current authentication context.
Provides read-only access to the response object used during authentication processing.
RETURNS | DESCRIPTION |
---|---|
Response | The HTTP response object stored in the authentication dependency. |
create_access_token #
create_access_token(uid, fresh=False, headers=None, expiry=None, data=None, audience=None, *args, **kwargs)
Generate an access token for a specific user with customizable parameters.
Delegates token creation to the underlying security mechanism with flexible configuration options.
Args: uid: Unique identifier of the user for whom the token is being created. fresh: Flag indicating whether the token should be marked as a fresh authentication. headers: Optional custom headers to include in the token. expiry: Optional expiration time for the token. data: Optional additional data to be encoded in the token. audience: Optional target audience for the token. args: Variable positional arguments for additional flexibility. *kwargs: Variable keyword arguments for additional configuration.
Returns: A string representing the generated access token.
PARAMETER | DESCRIPTION |
---|---|
uid | TYPE: |
fresh | TYPE: |
headers | TYPE: |
expiry | TYPE: |
data | TYPE: |
audience | TYPE: |
*args | TYPE: |
**kwargs | TYPE: |
Source code in authx/dependencies.py
create_refresh_token #
Generate a refresh token for a specific user with customizable parameters.
Delegates refresh token creation to the underlying security mechanism with flexible configuration options.
Args: uid: Unique identifier of the user for whom the refresh token is being created. headers: Optional custom headers to include in the token. expiry: Optional expiration time for the token. data: Optional additional data to be encoded in the token. audience: Optional target audience for the token. args: Variable positional arguments for additional flexibility. *kwargs: Variable keyword arguments for additional configuration.
Returns: A string representing the generated refresh token.
PARAMETER | DESCRIPTION |
---|---|
uid | TYPE: |
headers | TYPE: |
expiry | TYPE: |
data | TYPE: |
audience | TYPE: |
*args | TYPE: |
**kwargs | TYPE: |
Source code in authx/dependencies.py
set_access_cookies #
Set access token cookies in the HTTP response.
Configures the response with access token cookies, using the provided token and optional parameters.
Args: token: The access token to be set as a cookie. response: Optional HTTP response object to set cookies on. Defaults to the stored response if not provided. max_age: Optional maximum age for the cookie before expiration.
Returns: None
PARAMETER | DESCRIPTION |
---|---|
token | TYPE: |
response | TYPE: |
max_age | TYPE: |
Source code in authx/dependencies.py
set_refresh_cookies #
Set refresh token cookies in the HTTP response.
Configures the response with refresh token cookies, using the provided token and optional parameters.
Args: token: The refresh token to be set as a cookie. response: Optional HTTP response object to set cookies on. Defaults to the stored response if not provided. max_age: Optional maximum age for the cookie before expiration.
Returns: None
PARAMETER | DESCRIPTION |
---|---|
token | TYPE: |
response | TYPE: |
max_age | TYPE: |
Source code in authx/dependencies.py
unset_access_cookies #
Remove access token cookies from the HTTP response.
Clears the access token cookies from the specified or default response object.
Args: response: Optional HTTP response object to remove cookies from. Defaults to the stored response if not provided.
Returns: None
PARAMETER | DESCRIPTION |
---|---|
response | TYPE: |
Source code in authx/dependencies.py
unset_refresh_cookies #
Remove refresh token cookies from the HTTP response.
Clears the refresh token cookies from the specified or default response object.
Args: response: Optional HTTP response object to remove cookies from. Defaults to the stored response if not provided.
Returns: None
PARAMETER | DESCRIPTION |
---|---|
response | TYPE: |
Source code in authx/dependencies.py
unset_cookies #
Remove all authentication-related cookies from the HTTP response.
Clears both access and refresh token cookies from the specified or default response object.
Args: response: Optional HTTP response object to remove cookies from. Defaults to the stored response if not provided.
Returns: None
PARAMETER | DESCRIPTION |
---|---|
response | TYPE: |
Source code in authx/dependencies.py
get_current_subject async
#
Retrieve the currently authenticated subject from the request.
Asynchronously fetches the authenticated user or subject based on the current request context.
Returns: The authenticated subject if present, otherwise None.