|
@@ -23,6 +23,8 @@ class TokenError(BaseFlaskBBError):
|
|
Raised when there is an issue with deserializing
|
|
Raised when there is an issue with deserializing
|
|
a token. Has helper classmethods to ensure
|
|
a token. Has helper classmethods to ensure
|
|
consistent verbiage.
|
|
consistent verbiage.
|
|
|
|
+
|
|
|
|
+ :param str reason: An explanation of why the token is invalid
|
|
"""
|
|
"""
|
|
|
|
|
|
def __init__(self, reason):
|
|
def __init__(self, reason):
|
|
@@ -31,10 +33,19 @@ class TokenError(BaseFlaskBBError):
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
def invalid(cls):
|
|
def invalid(cls):
|
|
|
|
+ """
|
|
|
|
+ Used to raise an exception about a token that is invalid
|
|
|
|
+ due to being signed incorrectly, has been tampered with,
|
|
|
|
+ is unparsable or contains an inappropriate action.
|
|
|
|
+ """
|
|
return cls(_('Token is invalid'))
|
|
return cls(_('Token is invalid'))
|
|
|
|
|
|
@classmethod
|
|
@classmethod
|
|
def expired(cls):
|
|
def expired(cls):
|
|
|
|
+ """
|
|
|
|
+ Used to raise an exception about a token that has expired and is
|
|
|
|
+ no longer usable.
|
|
|
|
+ """
|
|
return cls(_('Token is expired'))
|
|
return cls(_('Token is expired'))
|
|
|
|
|
|
# in theory this would never be raised
|
|
# in theory this would never be raised
|
|
@@ -48,33 +59,60 @@ class TokenError(BaseFlaskBBError):
|
|
# holder for token actions
|
|
# holder for token actions
|
|
# not an enum so plugins can add to it
|
|
# not an enum so plugins can add to it
|
|
class TokenActions:
|
|
class TokenActions:
|
|
|
|
+ """
|
|
|
|
+ Collection of token actions.
|
|
|
|
+
|
|
|
|
+ .. note::
|
|
|
|
+ This is just a class rather than an enum because enums cannot be
|
|
|
|
+ extended at runtime which would limit the number of token actions
|
|
|
|
+ to the ones implemented by FlaskBB itself and block extension of
|
|
|
|
+ tokens by plugins.
|
|
|
|
+ """
|
|
RESET_PASSWORD = 'reset_password'
|
|
RESET_PASSWORD = 'reset_password'
|
|
ACTIVATE_ACCOUNT = 'activate_account'
|
|
ACTIVATE_ACCOUNT = 'activate_account'
|
|
|
|
|
|
|
|
|
|
@attr.s(frozen=True, cmp=True, hash=True)
|
|
@attr.s(frozen=True, cmp=True, hash=True)
|
|
class Token(object):
|
|
class Token(object):
|
|
|
|
+ """
|
|
|
|
+ :param int user_id:
|
|
|
|
+ :param str operation: An operation taken from
|
|
|
|
+ :class:`TokenActions<flaskbb.core.tokens.TokenActions>`
|
|
|
|
+ """
|
|
user_id = attr.ib()
|
|
user_id = attr.ib()
|
|
operation = attr.ib()
|
|
operation = attr.ib()
|
|
|
|
|
|
|
|
|
|
class TokenSerializer(ABC):
|
|
class TokenSerializer(ABC):
|
|
"""
|
|
"""
|
|
- Interface for token serializers.
|
|
|
|
|
|
|
|
- dumps must accept a Token instance and produce
|
|
|
|
- a JWT
|
|
|
|
-
|
|
|
|
- loads must accept a string representation of
|
|
|
|
- a JWT and produce a token instance
|
|
|
|
"""
|
|
"""
|
|
|
|
|
|
@abstractmethod
|
|
@abstractmethod
|
|
def dumps(self, token):
|
|
def dumps(self, token):
|
|
|
|
+ """
|
|
|
|
+ This method is abstract.
|
|
|
|
+
|
|
|
|
+ Used to transform a token into a string representation of it.
|
|
|
|
+
|
|
|
|
+ :param token:
|
|
|
|
+ :type token: :class:`Token<flaskbb.core.tokens.Token>`
|
|
|
|
+ :returns str:
|
|
|
|
+ """
|
|
pass
|
|
pass
|
|
|
|
|
|
@abstractmethod
|
|
@abstractmethod
|
|
def loads(self, raw_token):
|
|
def loads(self, raw_token):
|
|
|
|
+ """
|
|
|
|
+ This method is abstract
|
|
|
|
+
|
|
|
|
+ Used to transform a string representation of a token into an
|
|
|
|
+ actual :class:`Token<flaskbb.core.tokens.Token>` instance
|
|
|
|
+
|
|
|
|
+ :param str raw_token:
|
|
|
|
+ :returns token: The parsed token
|
|
|
|
+ :rtype: :class:`Token<flaskbb.core.tokens.Token`>
|
|
|
|
+ """
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
|
|
@@ -84,12 +122,20 @@ class TokenVerifier(ABC):
|
|
deserialization, such as an email matching the
|
|
deserialization, such as an email matching the
|
|
user id in the provided token.
|
|
user id in the provided token.
|
|
|
|
|
|
- Should raise a flaskbb.core.exceptions.ValidationError
|
|
|
|
|
|
+ Should raise a
|
|
|
|
+ :class:`ValidationError<flaskbb.core.exceptions.ValidationError>`
|
|
if verification fails.
|
|
if verification fails.
|
|
"""
|
|
"""
|
|
|
|
|
|
@abstractmethod
|
|
@abstractmethod
|
|
def verify_token(self, token, **kwargs):
|
|
def verify_token(self, token, **kwargs):
|
|
|
|
+ """
|
|
|
|
+ This method is abstract.
|
|
|
|
+
|
|
|
|
+ :param token: The parsed token to verify
|
|
|
|
+ :param kwargs: Arbitrary context for validation of the token
|
|
|
|
+ :type token: :class:`Token<flaskbb.core.tokens.Token>`
|
|
|
|
+ """
|
|
pass
|
|
pass
|
|
|
|
|
|
def __call__(self, token, **kwargs):
|
|
def __call__(self, token, **kwargs):
|