Browse Source

Add documentation for tokens

Alec Nikolas Reiter 7 years ago
parent
commit
8d7d4919ec
6 changed files with 103 additions and 13 deletions
  1. 2 0
      docs/api.rst
  2. 2 2
      docs/authentication.rst
  3. 13 0
      docs/coreexceptions.rst
  4. 20 0
      docs/tokens.rst
  5. 13 4
      flaskbb/core/exceptions.py
  6. 53 7
      flaskbb/core/tokens.py

+ 2 - 0
docs/api.rst

@@ -6,6 +6,8 @@ API
 .. toctree::
 .. toctree::
    :maxdepth: 2
    :maxdepth: 2
 
 
+   coreexceptions
    models
    models
    authentication
    authentication
    accountmanagement
    accountmanagement
+   tokens

+ 2 - 2
docs/authentication.rst

@@ -2,8 +2,8 @@
 
 
 .. module:: flaskbb.core.auth.authentication
 .. module:: flaskbb.core.auth.authentication
 
 
-Authentication Interfaces
-=========================
+Authentication
+==============
 
 
 
 
 FlaskBB exposes several interfaces and hooks to customize authentication and
 FlaskBB exposes several interfaces and hooks to customize authentication and

+ 13 - 0
docs/coreexceptions.rst

@@ -0,0 +1,13 @@
+.. _coreexceptions:
+
+.. module:: flaskbb.core.exceptions
+
+Core Exceptions
+===============
+
+These are exceptions that aren't specific to any one part of FlaskBB
+and are used ubiquitously.
+
+.. autoexception:: BaseFlaskBBError
+.. autoexception:: ValidationError
+.. autoexception:: StopValidation

+ 20 - 0
docs/tokens.rst

@@ -0,0 +1,20 @@
+.. _tokens:
+
+Tokens
+======
+
+.. module:: flaskbb.core.tokens
+
+.. autoclass:: TokenActions
+    :members:
+
+.. autoclass:: Token
+
+.. autoclass:: TokenSerializer
+    :members:
+
+.. autoclass:: TokenVerifier
+    :members:
+
+.. autoexception:: TokenError
+    :members:

+ 13 - 4
flaskbb/core/exceptions.py

@@ -11,14 +11,23 @@
     :license: BSD, see LICENSE for more details
     :license: BSD, see LICENSE for more details
 """
 """
 
 
+
 class BaseFlaskBBError(Exception):
 class BaseFlaskBBError(Exception):
-    "Root exception for FlaskBB"
+    """
+    Root exception for FlaskBB.
+    """
 
 
 
 
 class ValidationError(BaseFlaskBBError):
 class ValidationError(BaseFlaskBBError):
     """
     """
     Used to signal validation errors for things such as
     Used to signal validation errors for things such as
     token verification, user registration, etc.
     token verification, user registration, etc.
+
+    :param str attribute: The attribute the validation error applies to,
+        if the validation error applies to multiple attributes or to
+        the entire object, this should be set to None
+    :param str reason: Why the attribute, collection of attributes or object
+        is invalid.
     """
     """
 
 
     def __init__(self, attribute, reason):
     def __init__(self, attribute, reason):
@@ -33,11 +42,11 @@ class StopValidation(BaseFlaskBBError):
     validation should end immediately and no further
     validation should end immediately and no further
     processing should be done.
     processing should be done.
 
 
-    The reasons passed should be an iterable of
-    tuples consisting of `(attribute, reason)`
-
     Can also be used to communicate all errors
     Can also be used to communicate all errors
     raised during a validation run.
     raised during a validation run.
+
+    :param reasons: A sequence of `(attribute, reason)` pairs explaining
+        why the object is invalid.
     """
     """
 
 
     def __init__(self, reasons):
     def __init__(self, reasons):

+ 53 - 7
flaskbb/core/tokens.py

@@ -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):