Smarter Token Authentication
- class smarter.lib.drf.token_authentication.SmarterTokenAuthentication(*args, **kwargs)[source]
Bases:
TokenAuthentication,SmarterHelperMixinDjango REST Framework (DRF) knox TokenAuthentication that also checks that the token is active.
This subclass adds:
a check of the token’s
is_activefield, so that tokens can be disabledDjango signals for token authentication events
logging
an update of the token’s
last_used_atwhen it is used
Authentication raises
AuthenticationFailedfor any failure to authenticate the request’s token.- __init__(*args, **kwargs)[source]
Initialize the authentication backend.
- Parameters:
args – Passed to knox’s
TokenAuthentication.kwargs – Passed to knox’s
TokenAuthentication.
- authenticate(request)[source]
Authenticate a request’s
Authorization: Token ...header, as knox does, once per request.A token that was already authenticated for this request (see
remembered()) is not authenticated again.- Parameters:
request (
Request) – The request.- Return type:
- Returns:
The user and api key, or None if the request has no token header.
- Raises:
AuthenticationFailed – If the header is malformed, or the token fails.
- authenticate_credentials(token)[source]
Authenticate a token as knox does, then check that the token is active.
Sends the token authentication request signal, and the success or failure signal. On success, it updates the token’s
last_used_atat most once perSmarterAuthToken.LAST_USED_RESOLUTION, with a queryset update.- Parameters:
token (
bytes) – The token from the request’sAuthorizationheader.- Return type:
- Returns:
A tuple of the authenticated user and their
SmarterAuthToken.- Raises:
AuthenticationFailed – If the token is not bytes, if knox can’t authenticate it, or if the token is not active.
- classmethod authenticate_for_request(request, token)[source]
Authenticate a token that a request carries, once per request.
- Parameters:
request (
HttpRequest|Request|None) – The request that carries the token, or None if there is none.token (
bytes) – The token from the request’sAuthorizationheader.
- Return type:
- Returns:
A tuple of the authenticated user and their
SmarterAuthToken.- Raises:
AuthenticationFailed – As
authenticate_credentials()does.
- classmethod get_user_from_request(request)[source]
Return the user of the active token in a request’s
Authorization: Token ...header.Unlike
authenticate_credentials(), this sends no signals, and doesn’t update the token’slast_used_at.- Parameters:
request (
Request) – The request.- Return type:
User|SmarterAnonymousUser- Returns:
The token’s user if the token is valid and active, otherwise a
SmarterAnonymousUser.
- model
alias of
SmarterAuthToken
- static remember(request, token, user, smarter_auth_token)[source]
Keep a successful authentication on the Django request, for
remembered().- Parameters:
request (
HttpRequest|Request|None) – The request, a Django request or DRF’s wrapper of one, or None.token (
bytes) – The token from the request’sAuthorizationheader.user (
User) – The token’s user.smarter_auth_token (
SmarterAuthToken) – The token’s api key.
- Return type:
- static remembered(request, token)[source]
Return the user and api key that
tokenwas already authenticated as for this request.Several layers authenticate the same request’s token: the token middleware, the CSRF middleware’s request classification,
AccountMixinand DRF’s own authentication classes. The first success is kept on the Django request, so that the others don’t repeat the database lookups and writes. It lives only as long as the request.- Parameters:
request (
HttpRequest|Request|None) – The request, a Django request or DRF’s wrapper of one, or None.token (
bytes) – The token from the request’sAuthorizationheader.
- Return type:
- Returns:
The user and api key, or None if this token wasn’t authenticated for this request.