ExpiringTokenGenerator

ExpiringTokenGenerator implements single-use authentication tokens that expire after a configurable amount of time (24 hours by default). It is designed to be used in scenarios such as password reset links or email confirmation links, but could be used in other scenarios as well.

Django token generators for single-use authentications.

one day.

Type:

How many seconds a link stays valid, unless the generator is given another expiration

class smarter.lib.django.token_generators.ExpiringTokenGenerator(expiration=86400)[source]

Bases: PasswordResetTokenGenerator

An object of this class can generate a token that expires after a certain amount of time.

__init__(expiration=86400)[source]

Initialize the generator.

Parameters:

expiration (int) – How many seconds a token stays valid.

Extract the user from the uid and token and validate.

Parameters:
  • uidb64 (str) – The url-safe base64 encoded primary key of the user.

  • token (str) – The token.

Return type:

User

Returns:

The user.

Raises:

SmarterTokenError – If the token is malformed, invalid or expired.

Create an encoded url link that expires after a certain amount of time.

The protocol comes from the X-Forwarded-Proto header, else from the request, and defaults to https if neither can be read.

Parameters:
  • request (HttpRequest | Request) – The request, which gives the current site’s domain and the protocol.

  • user (User) – The user the link is for.

  • reverse_link (str) – The url name to reverse, with uidb64 and token kwargs.

Return type:

str

Returns:

The absolute url.

get_timestamp()[source]

Return the current time on the clock that Django’s tokens use.

Django stamps a token with the seconds since 2001-01-01, so a token’s age is this value minus the token’s own timestamp.

Return type:

int

Returns:

The seconds since 2001-01-01.

Return the user and token from a link made by encode_link(), without validating the token.

The last two path segments are the encoded user id and the token.

Parameters:

url (str) – The link.

Return type:

tuple[User, str]

Returns:

A tuple of the user and the token.

Raises:

User.DoesNotExist – If there is no such user.

token_age(timestamp)[source]

Return how many seconds ago a token was made.

Parameters:

timestamp (int) – The token’s timestamp, the base36-decoded first part of the token.

Return type:

int

Returns:

The token’s age, in seconds.

uidb64_to_user(uidb64)[source]

Fetch the user whose primary key is encoded in uidb64.

Parameters:

uidb64 (str) – The url-safe base64 encoded primary key.

Return type:

User

Returns:

The user.

Raises:

User.DoesNotExist – If there is no such user.

user_to_uidb64(user)[source]

Encode a user’s primary key as url-safe base64.

Parameters:

user (User) – The user.

Return type:

str

Returns:

The encoded primary key.

validate(user, token)[source]

Check that a password reset token is correct for a given user.

Parameters:
  • user (User) – The user.

  • token (str) – The token.

Return type:

bool

Returns:

True if the token is valid.

Raises:
smarter.lib.django.token_generators.SmarterRequest = django.http.request.HttpRequest | rest_framework.request.Request

A Django or a DRF request, either of which ExpiringTokenGenerator.encode_link() accepts.

exception smarter.lib.django.token_generators.SmarterTokenConversionError(message='')[source]

Bases: SmarterTokenError

Raised when a token’s timestamp part is not a valid base36 number.

exception smarter.lib.django.token_generators.SmarterTokenError(message='')[source]

Bases: SmarterException

Base class for all token-related exceptions.

exception smarter.lib.django.token_generators.SmarterTokenExpiredError(message='')[source]

Bases: SmarterTokenError

Raised when a token is older than its generator’s expiration.

exception smarter.lib.django.token_generators.SmarterTokenIntegrityError(message='')[source]

Bases: SmarterTokenError

Raised when a token fails Django’s token check for the user.

exception smarter.lib.django.token_generators.SmarterTokenParseError(message='')[source]

Bases: SmarterTokenError

Raised when a token is not two parts separated by one dash.