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.
- smarter.lib.django.token_generators.DEFAULT_LINK_EXPIRATION = 86400
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:
PasswordResetTokenGeneratorAn 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.
- decode_link(uidb64, token)[source]
Extract the user from the uid and token and validate.
- Parameters:
- Return type:
- Returns:
The user.
- Raises:
SmarterTokenError – If the token is malformed, invalid or expired.
- encode_link(request, user, reverse_link)[source]
Create an encoded url link that expires after a certain amount of time.
The protocol comes from the
X-Forwarded-Protoheader, 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, withuidb64andtokenkwargs.
- Return type:
- 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:
- Returns:
The seconds since 2001-01-01.
- parse_link(url)[source]
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.
- validate(user, token)[source]
Check that a password reset token is correct for a given user.
- Parameters:
- Return type:
- Returns:
True if the token is valid.
- Raises:
SmarterTokenParseError – If the token is not two parts separated by a dash.
SmarterTokenIntegrityError – If the token fails Django’s check.
SmarterTokenConversionError – If the timestamp part is not base36.
SmarterTokenExpiredError – If the token is older than
expirationseconds.
- 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:
SmarterTokenErrorRaised when a token’s timestamp part is not a valid base36 number.
- exception smarter.lib.django.token_generators.SmarterTokenError(message='')[source]
Bases:
SmarterExceptionBase class for all token-related exceptions.
- exception smarter.lib.django.token_generators.SmarterTokenExpiredError(message='')[source]
Bases:
SmarterTokenErrorRaised when a token is older than its generator’s expiration.
- exception smarter.lib.django.token_generators.SmarterTokenIntegrityError(message='')[source]
Bases:
SmarterTokenErrorRaised when a token fails Django’s token check for the user.
- exception smarter.lib.django.token_generators.SmarterTokenParseError(message='')[source]
Bases:
SmarterTokenErrorRaised when a token is not two parts separated by one dash.