Jump to >

djblets.secrets.token_generators.base

Base class for token generators.

New in version 3.0.

class BaseTokenGenerator[source]

Bases: object

A base class for token generators.

These are used to generate and validate access tokens.

At the very least, subclasses must set:

as well as implement the create_token() and validate_token() methods.

New in version 3.0.

token_generator_id: Optional[str] = None

The unique ID of the token generator, for registration purposes.

Type

str

create_token(token_info: Dict[str, Any] = {}, **kwargs) str[source]

Create a new token.

Subclasses must override this to actually create the token.

Parameters
  • token_info (dict, optional) – A dictionary that contains information about the token.

  • **kwargs (dict) – Additional keyword arguments.

Returns

The token that was created.

Return type

str

Raises

NotImplementedError – The subclass did not implement this method.

validate_token(token: str, token_info: Dict[str, Any] = {}, **kwargs) bool[source]

Validate the token.

This should return a bool indicating whether the given token is a valid token that could have been generated by this generator.

Subclasses must override this to actually validate the token.

Parameters
  • token (str) – The token to validate.

  • token_info (dict, optional) – A dictionary that contains information about the token.

  • **kwargs (dict) – Additional keyword arguments.

Returns

Whether the token is a valid token from this generator.

Return type

bool

Raises

NotImplementedError – The subclass did not implement this method.

__annotations__ = {'token_generator_id': typing.Optional[str]}