Smarter Token Authentication

class smarter.lib.drf.token_authentication.SmarterTokenAuthentication(*args, **kwargs)[source]

Bases: TokenAuthentication, SmarterHelperMixin

Enhanced Django Rest Framework (DRF) knox TokenAuthentication

This subclass adds:

  • adds an activation field to enable/disable tokens

  • adds Django signals for token authentication events

  • adds app logging

  • verifies token activity.

  • adds timestamp update on token use

Raises:

AuthenticationFailed – for any failure to authenticate the token the request.

__init__(*args, **kwargs)[source]
authenticate_credentials(token)[source]

Override parent authenticate_credentials() to add token activity check and logging.

Return type:

tuple[User, SmarterAuthToken]

Parameters:

token (bytes) – The authentication token provided in the request.

Raises:
  • AuthenticationFailed – for any failure to authenticate the token the request.

  • AuthenticationFailed – _if the token is not active.

  • AuthenticationFailed – if the token is not a bytes instance.

Returns:

A tuple containing the authenticated User and SmarterAuthToken.

Return type:

tuple[User, SmarterAuthToken]

property formatted_class_name: str

Return the formatted class name for logging purposes.

classmethod get_user_from_request(request)[source]

Override get_user_from_request() to add logging and to use SmarterAuthToken.

Return type:

User | SmarterAnonymousUser

Parameters:

request (HttpRequest) – a Django request object.

Returns:

The authenticated user if the token is valid, otherwise SmarterAnonymousUser.

Return type:

User or SmarterAnonymousUser

model

alias of SmarterAuthToken