Smarter Token Authentication

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

Bases: TokenAuthentication, SmarterHelperMixin

Django REST Framework (DRF) knox TokenAuthentication that also checks that the token is active.

This subclass adds:

  • a check of the token’s is_active field, so that tokens can be disabled

  • Django signals for token authentication events

  • logging

  • an update of the token’s last_used_at when it is used

Authentication raises AuthenticationFailed for 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:

tuple[User, SmarterAuthToken] | None

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_at at most once per SmarterAuthToken.LAST_USED_RESOLUTION, with a queryset update.

Parameters:

token (bytes) – The token from the request’s Authorization header.

Return type:

tuple[User, SmarterAuthToken]

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’s Authorization header.

Return type:

tuple[User, SmarterAuthToken]

Returns:

A tuple of the authenticated user and their SmarterAuthToken.

Raises:

AuthenticationFailed – As authenticate_credentials() does.

property formatted_class_name: str

Return the formatted class name for logging purposes.

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’s last_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’s Authorization header.

  • user (User) – The token’s user.

  • smarter_auth_token (SmarterAuthToken) – The token’s api key.

Return type:

None

static remembered(request, token)[source]

Return the user and api key that token was already authenticated as for this request.

Several layers authenticate the same request’s token: the token middleware, the CSRF middleware’s request classification, AccountMixin and 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’s Authorization header.

Return type:

tuple[User, SmarterAuthToken] | None

Returns:

The user and api key, or None if this token wasn’t authenticated for this request.