summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--passlib/crypto/scrypt/__init__.py23
-rw-r--r--passlib/handlers/bcrypt.py25
-rw-r--r--passlib/handlers/scrypt.py18
-rw-r--r--passlib/tests/test_handlers_bcrypt.py2
-rw-r--r--passlib/tests/test_utils_handlers.py17
-rw-r--r--passlib/tests/utils.py10
-rw-r--r--passlib/utils/__init__.py6
-rw-r--r--passlib/utils/handlers.py460
8 files changed, 340 insertions, 221 deletions
diff --git a/passlib/crypto/scrypt/__init__.py b/passlib/crypto/scrypt/__init__.py
index 8fa3e78..2761287 100644
--- a/passlib/crypto/scrypt/__init__.py
+++ b/passlib/crypto/scrypt/__init__.py
@@ -162,7 +162,7 @@ def _load_backend(name):
return _builtin_first_run
raise ValueError("unknown scrypt backend %r" % name)
-def _set_backend(name):
+def _set_backend(name, dryrun=False):
"""
set backend for scrypt(). if name not specified, loads first available.
@@ -170,20 +170,23 @@ def _set_backend(name):
.. note:: mainly intended to be called by unittests, and scrypt hash handler
"""
- if name == "default":
+ if name == "any":
+ return True
+ elif name == "default":
for name in backend_values:
- hash = _load_backend(name)
- if hash:
- break
- else:
- raise exc.MissingBackendError("no scrypt backends available")
+ try:
+ _set_backend(name, dryrun=dryrun)
+ return
+ except exc.MissingBackendError:
+ continue
+ raise exc.MissingBackendError("no scrypt backends available")
else:
hash = _load_backend(name)
if not hash:
raise exc.MissingBackendError("scrypt backend %r not available" % name)
- global _scrypt, backend
- backend = name
- _scrypt = hash
+ global _scrypt, backend
+ backend = name
+ _scrypt = hash
# initialize backend
_set_backend("default")
diff --git a/passlib/handlers/bcrypt.py b/passlib/handlers/bcrypt.py
index a7f11bf..b1bade5 100644
--- a/passlib/handlers/bcrypt.py
+++ b/passlib/handlers/bcrypt.py
@@ -279,16 +279,21 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
# backend capability/bug detection
#---------------------------------------------------------------
@classmethod
- def set_backend(cls, *a, **k):
- backend = super(bcrypt, cls).set_backend(*a, **k)
- cls._scan_backend(backend)
- return backend
+ def set_backend(cls, name="any", dryrun=False):
+ """
+ subclass hook to handle workaround detection
+ """
+ super(bcrypt, cls).set_backend(name, dryrun=dryrun)
+ if not dryrun:
+ cls._configure_workarounds()
@classmethod
- def _scan_backend(cls, backend):
+ def _configure_workarounds(cls, backend):
"""
- check for known bugs & feature support once backend is loaded
+ detect & configure workarounds for specific backend
"""
+ backend = cls.get_backend()
+
# check for cryptblowfish 8bit bug (fixed in 2y/2b);
# even though it's not known to be present in any of passlib's backends.
# this is treated as FATAL, because it can easily result in seriously malformed hashes,
@@ -438,7 +443,7 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
except:
log.warning("(trapped) error reading bcrypt version", exc_info=True)
version = '<unknown>'
- log.debug("loaded 'bcrypt' backend, version %r", version)
+ log.debug("detected 'bcrypt' backend, version %r", version)
return cls._calc_checksum_bcrypt
def _calc_checksum_bcrypt(self, secret, config):
@@ -478,7 +483,7 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
except:
log.warning("(trapped) error reading pybcrypt version", exc_info=True)
version = "<unknown>"
- log.debug("loaded 'pybcrypt' backend, version %r", version)
+ log.debug("detected 'pybcrypt' backend, version %r", version)
# return calc function based on version
vinfo = parse_version(version) or (0, 0)
@@ -570,10 +575,10 @@ class bcrypt(uh.HasManyIdents, uh.HasRounds, uh.HasSalt, uh.HasManyBackends, uh.
@classmethod
def _load_backend_builtin(cls):
if os.environ.get("PASSLIB_BUILTIN_BCRYPT") not in ["enable","enabled"]:
+ log.debug("bcrypt 'builtin' backend not enabled via $PASSLIB_BUILTIN_BCRYPT")
return None
global _builtin_bcrypt
- if _builtin_bcrypt is None:
- from passlib.crypto._blowfish import raw_bcrypt as _builtin_bcrypt
+ from passlib.crypto._blowfish import raw_bcrypt as _builtin_bcrypt
return cls._calc_checksum_builtin
def _calc_checksum_builtin(self, secret, config):
diff --git a/passlib/handlers/scrypt.py b/passlib/handlers/scrypt.py
index c5ea074..faac09c 100644
--- a/passlib/handlers/scrypt.py
+++ b/passlib/handlers/scrypt.py
@@ -230,25 +230,21 @@ class scrypt(uh.HasRounds, uh.HasRawChecksum, uh.HasRawSalt, uh.GenericHandler):
def backends(cls):
return _scrypt.backend_values
- @classproperty
- def backend(cls):
- return _scrypt.backend
-
@classmethod
def get_backend(cls):
- return cls.backend
+ return _scrypt.backend
@classmethod
def has_backend(cls, name="any"):
- if name == "any" or name == "default":
+ try:
+ cls.set_backend(name, dryrun=True)
return True
- else:
- return _scrypt._load_backend(name) is not None
+ except uh.exc.MissingBackendError:
+ return False
@classmethod
- def set_backend(cls, name="any"):
- if name != "any":
- _scrypt._set_backend(name)
+ def set_backend(cls, name="any", dryrun=False):
+ _scrypt._set_backend(name, dryrun=dryrun)
#===================================================================
# calc checksum
diff --git a/passlib/tests/test_handlers_bcrypt.py b/passlib/tests/test_handlers_bcrypt.py
index 8c2acf0..d02467c 100644
--- a/passlib/tests/test_handlers_bcrypt.py
+++ b/passlib/tests/test_handlers_bcrypt.py
@@ -233,7 +233,7 @@ class _bcrypt_test(HandlerCase):
return
if not _detect_pybcrypt():
return
- hash.bcrypt._load_backend_pybcrypt()
+ hash.bcrypt._load_backend_pybcrypt() # called for side-effect of init'ing lock
lock = hash.bcrypt._calc_lock # reuse threadlock workaround for pybcrypt 0.2
def check_pybcrypt(secret, hash):
"""pybcrypt"""
diff --git a/passlib/tests/test_utils_handlers.py b/passlib/tests/test_utils_handlers.py
index c1e4e37..b751bd8 100644
--- a/passlib/tests/test_utils_handlers.py
+++ b/passlib/tests/test_utils_handlers.py
@@ -356,13 +356,22 @@ class SkeletonTest(TestCase):
backends = ("a", "b")
+ _enable_a = False
+ _enable_b = False
+
@classmethod
def _load_backend_a(cls):
- return None
+ if cls._enable_a:
+ return cls._calc_checksum_a
+ else:
+ return None
@classmethod
def _load_backend_b(cls):
- return None
+ if cls._enable_b:
+ return cls._calc_checksum_b
+ else:
+ return None
def _calc_checksum_a(self, secret):
return 'a'
@@ -378,7 +387,7 @@ class SkeletonTest(TestCase):
self.assertFalse(d1.has_backend())
# enable 'b' backend
- d1._load_backend_b = classmethod(lambda cls: cls._calc_checksum_b)
+ d1._enable_b = True
# test lazy load
obj = d1()
@@ -395,7 +404,7 @@ class SkeletonTest(TestCase):
self.assertFalse(d1.has_backend('a'))
# enable 'a' backend also
- d1._load_backend_a = classmethod(lambda cls: cls._calc_checksum_a)
+ d1._enable_a = True
# test explicit
self.assertTrue(d1.has_backend())
diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py
index f3f9579..fe0670d 100644
--- a/passlib/tests/utils.py
+++ b/passlib/tests/utils.py
@@ -2445,13 +2445,15 @@ class HandlerCase(TestCase):
verifiers.append(func)
# create verifiers for any other available backends
- # NOTE: using subclass so we can load alt backend in threadsafe manner
if hasattr(handler, "backends") and TEST_MODE("full"):
def maker(backend):
- sub_handler = handler.using()
- sub_handler.set_backend(backend)
def func(secret, hash):
- return sub_handler.verify(secret, hash)
+ orig_backend = handler.get_backend()
+ try:
+ handler.set_backend(backend)
+ return handler.verify(secret, hash)
+ finally:
+ handler.set_backend(orig_backend)
func.__name__ = "check_" + backend + "_backend"
func.__doc__ = backend + "-backend"
return func
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index 4176abe..8092f3b 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -7,6 +7,7 @@ from passlib.utils.compat import JYTHON
from base64 import b64encode, b64decode
from codecs import lookup as _lookup_codec
from functools import update_wrapper
+import inspect
import logging; log = logging.getLogger(__name__)
import math
import os
@@ -275,6 +276,11 @@ class SequenceMixin(object):
def __ne__(self, other):
return not self.__eq__(other)
+def accepts_keyword(func, key):
+ """test if function accepts specified keyword"""
+ spec = inspect.getargspec(get_method_function(func))
+ return key in spec.args or spec.keywords is not None
+
#=============================================================================
# unicode helpers
#=============================================================================
diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py
index 2dd0002..ce1dabe 100644
--- a/passlib/utils/handlers.py
+++ b/passlib/utils/handlers.py
@@ -4,6 +4,7 @@
#=============================================================================
from __future__ import with_statement
# core
+from functools import partial
import inspect
import logging; log = logging.getLogger(__name__)
import math
@@ -18,7 +19,7 @@ from passlib.registry import get_crypt_handler
from passlib.utils import classproperty, consteq, getrandstr, getrandbytes,\
BASE64_CHARS, HASH64_CHARS, rng, to_native_str, \
is_crypt_handler, to_unicode, deprecated_method, \
- MAX_PASSWORD_SIZE
+ MAX_PASSWORD_SIZE, accepts_keyword
from passlib.utils.compat import join_byte_values, irange, u, native_string_types, \
uascii_to_str, join_unicode, unicode, str_to_uascii, \
join_unicode, unicode_or_bytes_types, PY2, int_types
@@ -1815,24 +1816,10 @@ class HasRounds(GenericHandler):
#------------------------------------------------------------------------
# backend mixin & helpers
#------------------------------------------------------------------------
-##def _clear_backend(cls):
-## "restore HasManyBackend subclass to unloaded state - used by unittests"
-## assert issubclass(cls, HasManyBackends) and cls is not HasManyBackends
-## if cls._backend:
-## del cls._backend
-## del cls._calc_checksum
-
-class HasManyBackends(GenericHandler):
- """GenericHandler mixin which provides selecting from multiple backends.
-
- .. todo::
-
- finish documenting this class's usage
-
- For hashes which need to select from multiple backends,
- depending on the host environment, this class
- offers a way to specify alternate :meth:`_calc_checksum` methods,
- and will dynamically chose the best one at runtime.
+class BackendMixin(PasswordHash):
+ """
+ PasswordHash mixin which provides generic framework for supporting multiple backends
+ within the class.
Public API
----------
@@ -1850,253 +1837,364 @@ class HasManyBackends(GenericHandler):
.. warning::
- :meth:`set_backend` and :meth:`has_backend` are intended to be called
- during application startup -- they affect global state, are not threadsafe.
+ :meth:`set_backend` is intended to be called during application startup --
+ it affects global state, and switching backends is not guaranteed threadsafe.
Private API (Subclass Hooks)
----------------------------
- The following attributes and methods should be filled in by the subclass
- which is using :class:`HasManyBackends` as a mixin:
+ Subclasses should set the :attr:`!backends` attribute to a tuple of the backends
+ they wish to support. They should also define one method:
- .. attribute:: _load_backend_{name}
+ .. classmethod:: _load_backend_{name}(dryrun=False)
- private class method that should try to load the specified backend,
- one of which should be provided for each backend listed in :attr:`backends`.
+ One copy of this method should be defined for each :samp:`name` within :attr:`!backends`.
- * if backend isn't available, it should return ``None``.
- * if backend is available, it should return a callable
- which implements :meth:`_calc_checksum`.
- * it may also do things like import modules, run tests, issue warnings,
- etc; though it should avoid doing things which would change the operation
- of other backends (e.g. modify ``cls.default_rounds``).
+ It will be called in order to load the backend, and should take care of whatever
+ is needed to enable the backend. This may include importing modules, running tests,
+ issuing warnings, etc.
- .. versionadded:: 1.7
+ :param dryrun:
+ if True, this should be treated as a dry run -- the method should perform
+ all setup actions *except* switching the class over to the new backend.
+ it should default to False.
+
+ this keyword is optional, methods can omit it if they are stateless.
+
+ :raises passlib.exc.PasslibSecurityError:
+ if the backend is available, but cannot be loaded due to a security issue.
+
+ :returns:
+ None / False if backend not available.
+ Otherwise it can return any boolean-true value,
+ which will be passed on to :meth:`!_finalize_backend`,
+ (to be ignored or used however is appropriate for the class).
.. warning::
Due to the way passlib's internals are arranged,
- backends should always store stateful data at the class level
+ backends should generally store stateful data at the class level
(not the module level), and be prepared to be called on subclasses
which may be set to a different backend from their parent.
- Idempotent module-level data such as lazy imports are fine.
+ (Idempotent module-level data such as lazy imports are fine).
- .. attribute:: _has_backend_{name}
-
- private class attribute checked by :meth:`has_backend` to see if a
- specific backend is available, it should be either ``True``
- or ``False``. One of these should be provided by
- the subclass for each backend listed in :attr:`backends`.
+ .. automethod:: _finalize_backend
- .. deprecated:: 1.7
-
- use :attr:`_load_backend_{name}` instead.
- support for this attribute will be removed in Passlib 2.0.
-
- .. classmethod:: _calc_checksum_{name}
-
- private class method that should implement :meth:`_calc_checksum`
- for a given backend. it will only be called if the backend has
- been selected by :meth:`set_backend`. One of these should be provided
- by the subclass for each backend listed in :attr:`backends`.
-
- .. deprecated:: 1.7
-
- use :attr:`_load_backend_{name}` instead.
- this attribute will be ignored in Passlib 2.0.
+ .. versionadded:: 1.7
"""
- backends = None # list of backend names, provided by subclass.
+ #===================================================================
+ # class attrs
+ #===================================================================
+
+ #: list of backend names, provided by subclass.
+ backends = None
- _backend = None # holds currently loaded backend (if any) or None
+ #: private attr mixin uses to hold currently loaded backend (or ``None``)
+ __backend = None
#: optional class-specific text containing suggestion about what to do
#: when no backends are available.
_no_backend_suggestion = None
+ #: optionally non-inherited flag used to detect which class 'owns' the backend
+ _backend_owner = False
+
+ #===================================================================
+ # public api
+ #===================================================================
+
@classmethod
def get_backend(cls):
- """return name of currently active backend.
-
- if no backend has been loaded,
- loads and returns name of default backend.
+ """
+ Return name of currently active backend.
+ if no backend has been loaded, loads and returns name of default backend.
- :raises passlib.exc.MissingBackendError: if no backends are available.
+ :raises passlib.exc.MissingBackendError:
+ if no backends are available.
- :returns: name of active backend
+ :returns:
+ name of active backend
"""
- if not cls._backend:
+ if not cls.__backend:
cls.set_backend()
- assert cls._backend, "set_backend() failed to load a default backend"
- return cls._backend
+ assert cls.__backend, "set_backend() failed to load a default backend"
+ return cls.__backend
@classmethod
def has_backend(cls, name="any"):
- """check if support is currently available for specified backend.
+ """
+ Check if support is currently available for specified backend.
:arg name:
name of backend to check for.
- defaults to ``"any"``,
- but can be any string accepted by :meth:`set_backend`.
+ can be any string accepted by :meth:`set_backend`.
- :raises ValueError: if backend name is unknown
+ :raises ValueError:
+ if backend name is unknown
:returns:
- ``True`` if backend is currently supported,
- ``False`` if it's not,
- and ``None`` if it's present, but won't load due to a security issue.
+ * ``True`` if backend is available.
+ * ``False`` if it's not.
+ * ``None`` if it's present, but won't load due to a security issue.
"""
- if name == "any" or name == "default":
- if cls._backend:
- return True
- try:
- cls.set_backend()
- return True
- except exc.PasslibSecurityError:
- return None
- except exc.MissingBackendError:
- return False
- else:
- try:
- return cls._load_backend(name) is not None
- except exc.PasslibSecurityError:
- return None
+ try:
+ cls.set_backend(name, dryrun=True)
+ return True
+ except exc.MissingBackendError:
+ return False
+ except exc.PasslibSecurityError:
+ return None
@classmethod
- def set_backend(cls, name="any"):
- """load specified backend to be used for future _calc_checksum() calls
-
- this method replaces :meth:`_calc_checksum` with a method
- which uses the specified backend.
+ def set_backend(cls, name="any", dryrun=False):
+ """
+ Load specified backend.
:arg name:
- name of backend to load, defaults to ``"any"``.
- this can be any of the following values:
+ name of backend to load, can be any of the following:
- * any string in :attr:`backends`,
- indicating the specific backend to use.
+ * ``"any"`` -- use current backend if one is loaded,
+ otherwise load the first available backend.
- * the special string ``"default"``, which means to use
- the preferred backend on the given host
- (this is generally the first backend in :attr:`backends`
- which can be loaded).
+ * ``"default"`` -- use the first available backend.
- * the special string ``"any"``, which means to use
- the current backend if one has been loaded,
- else acts like ``"default"``.
+ * any string in :attr:`backends`, loads specified backend.
- :raises passlib.exc.MissingBackendError:
- * ... if a specific backend was requested,
- but is not currently available.
+ :param dryrun:
+ If True, this perform all setup actions *except* switching over to the new backend.
+ (this flag is used to implement :meth:`has_backend`).
- * ... if ``"any"`` or ``"default"`` was specified,
- and *no* backends are currently available.
+ .. versionadded:: 1.7
+
+ :raises ValueError:
+ If backend name is unknown.
+
+ :raises passlib.exc.MissingBackendError:
+ If specific backend is missing;
+ or in the case of ``"any"`` / ``"default"``, if *no* backends are available.
:raises passlib.exc.PasslibSecurityError:
- if ``"any"`` or ``"default"`` was specified,
+ If ``"any"`` or ``"default"`` was specified,
but the only backend available has a PasslibSecurityError.
- may be raised by the loading code, or by a subclassed set_backend() function.
-
- :returns:
- The return value of this function should be ignored.
"""
- if name == "any" and cls._backend:
- # keep active backend
- return cls._backend
- elif name == "any" or name == "default":
- # select default backend
- failed_name = None
+ # check if active backend is acceptable
+ if (name == "any" and cls.__backend) or (name and name == cls.__backend):
+ return cls.__backend
+
+ # check if parent class set marker indicating it should always be used to set backend
+ # (and not a subclass)
+ if cls._backend_owner and not cls.__dict__.get("_backend_owner"):
+ for base in cls.__mro__:
+ if base.__dict__.get("_backend_owner"):
+ return base.set_backend(name, dryrun=dryrun)
+ raise AssertionError("expected to find class w/ '_backend_owner' set")
+
+ # pick first available backend
+ if name == "any" or name == "default":
+ default_error = None
for name in cls.backends:
try:
- calc = cls._load_backend(name)
- except exc.PasslibSecurityError:
+ return cls.set_backend(name, dryrun=dryrun)
+ except exc.MissingBackendError:
+ continue
+ except exc.PasslibSecurityError as err:
# backend is available, but refuses to load due to security issue.
- if failed_name is None:
- failed_name = name
+ if default_error is None:
+ default_error = err
continue
- if calc:
- break
- assert calc is None
- else:
- if failed_name:
- # if there was at least one backend, but it had a PasslibSecurityError,
- # report that to the user rather than MissingBackendError
- cls._load_backend(failed_name)
+ if default_error is None:
msg = "%s: no backends available" % cls.name
if cls._no_backend_suggestion:
msg += cls._no_backend_suggestion
- raise exc.MissingBackendError(msg)
+ default_error = exc.MissingBackendError(msg)
+ raise default_error
+
+ # validate name
+ if name not in cls.backends:
+ raise ValueError("%s: unknown backend: %r" % (cls.name, name))
+
+ # hand off to loader
+ loader = cls._get_backend_loader(name)
+ if accepts_keyword(loader, "dryrun"):
+ result = loader(dryrun=dryrun)
else:
- # select specific backend
- calc = cls._load_backend(name)
- if not calc:
- assert calc is None
- raise exc.MissingBackendError("%s: backend not available: %s" %
- (cls.name, name))
- # load backend into class
- # NOTE: not overwriting _calc_checksum() directly, so that classes can provide
- # common behavior in that method,
- # and then invoke _calc_checksum_backend() to do the work.
- assert callable(calc)
- cls._calc_checksum_backend = calc
- cls._backend = name
- return name
+ result = loader()
+ if not result:
+ raise exc.MissingBackendError("%s: backend not available: %s" % (cls.name, name))
+ cls._finalize_backend(name, result, dryrun=dryrun)
+ if not dryrun:
+ # XXX: would it be better to do this in _finalize_backend()? might simplify bcrypt handler
+ cls.__backend = name
+ # return name
+
+ #===================================================================
+ # subclass hooks
+ #===================================================================
+
+ @classmethod
+ def _get_backend_loader(cls, name):
+ """
+ helper to support legacy HasManyBackends;
+ this hook may be removed in passlib 2.0.
+ """
+ return getattr(cls, "_load_backend_" + name)
@classmethod
- def _load_backend(cls, name):
- """helper used by has_backend() & set_backend(), loads specified backend.
+ def _finalize_backend(cls, name, result, dryrun=False):
+ """
+ hook called by :meth:`set_backend` after loader returns.
+ it should perform common tests & finalization.
+ this may be subclassed in order to perform class-specific actions
+ with the result of a backend.
- :raises ValueError: if invalid backend name is provided
+ :param name:
+ name of backend
- :raises passlib.exc.SecurityError:
- backend code itself is allowed to raise this if backend is available,
- but a fatal security issue was found.
+ :param result:
+ value returned by backend
- :returns:
- * ``None`` if backend can't be loaded.
- * backend-specific ``_calc_checksum()`` callable on success.
+ :param dryrun:
+ dryrun flag passed to set_backend()
"""
- # validate name
- if name not in cls.backends:
- raise ValueError("%s: unknown backend: %r" % (cls.name, name))
+ pass
+
+ @classmethod
+ def _stub_requires_backend(cls):
+ """
+ helper for subclasses to create stub methods which auto-load backend.
+ """
+ if cls.__backend:
+ raise AssertionError("%s: _finalize_backend(%r) failed to replace lazy loader" %
+ (cls.name, cls.__backend))
+ cls.set_backend()
+ if not cls.__backend:
+ raise AssertionError("%s: set_backend() failed to load a default backend" %
+ (cls.name))
+
+ #===================================================================
+ # eoc
+ #===================================================================
+
+class HasManyBackends(BackendMixin, GenericHandler):
+ """
+ GenericHandler mixin which provides selecting from multiple backends.
+
+ .. todo::
+
+ finish documenting this class's usage
+
+ For hashes which need to select from multiple backends,
+ depending on the host environment, this class
+ offers a way to specify alternate :meth:`_calc_checksum` methods,
+ and will dynamically chose the best one at runtime.
+
+ .. versionchanged:: 1.7
+
+ This class now derives from :class:`BackendMixin`, which abstracts
+ out a more generic framework for supporting multiple backends.
+ The public api (:meth:`!get_backend`, :meth:`!has_backend`, :meth:`!set_backend`)
+ is roughly the same.
+
+ Private API (Subclass Hooks)
+ ----------------------------
+ As of version 1.7, classes should implement :meth:`!_load_backend_{name}`, per
+ :class:`BackendMixin`. The following api is deprecated:
+
+ .. attribute:: _has_backend_{name}
+
+ private class attribute checked by :meth:`has_backend` to see if a
+ specific backend is available, it should be either ``True``
+ or ``False``. One of these should be provided by
+ the subclass for each backend listed in :attr:`backends`.
+
+ .. deprecated:: 1.7
+
+ use :attr:`_load_backend_{name}` instead.
+ support for this attribute will be removed in Passlib 2.0.
+
+ .. classmethod:: _calc_checksum_{name}
- # new in v1.7: check for _load_backend_xxx() function
- load = getattr(cls, "_load_backend_" + name, None)
- if load is not None:
+ private class method that should implement :meth:`_calc_checksum`
+ for a given backend. it will only be called if the backend has
+ been selected by :meth:`set_backend`. One of these should be provided
+ by the subclass for each backend listed in :attr:`backends`.
+
+ .. deprecated:: 1.7
+
+ use :attr:`_load_backend_{name}` instead.
+ this attribute will not have special meaning in Passlib 2.0.
+ """
+ #===================================================================
+ # digest calculation
+ #===================================================================
+
+ def _calc_checksum(self, secret):
+ "wrapper for backend, for common code"""
+ # NOTE: not overwriting _calc_checksum() directly, so that classes can provide
+ # common behavior in that method,
+ # and then invoke _calc_checksum_backend() to do the work.
+ return self._calc_checksum_backend(secret)
+
+ def _calc_checksum_backend(self, secret):
+ """
+ stub for _calc_checksum_backend() --
+ should load backend if one hasn't been loaded;
+ if one has been loaded, this method should have been monkeypatched by _finalize_backend().
+ """
+ self._stub_requires_backend()
+ return self._calc_checksum_backend(secret)
+
+ #===================================================================
+ # BackendMixin hooks
+ #===================================================================
+ @classmethod
+ def _get_backend_loader(cls, name):
+ """
+ subclassed to support legacy 1.6 HasManyBackends api.
+ (will be removed in passlib 2.0)
+ """
+ # check for 1.7 loader
+ loader = getattr(cls, "_load_backend_" + name, None)
+ if loader is None:
+ # fallback to pre-1.7 _has_backend_xxx + _calc_checksum_xxx() api
+ loader = partial(cls._load_legacy_backend, name)
+ else:
+ # make sure 1.6 api isn't defined at same time
assert not hasattr(cls, "_has_backend_" + name), (
"%s: can't specify both ._load_backend_%s() "
"and ._has_backend_%s" % (cls.name, name, name)
)
- return load()
+ return loader
- # fallback to _has_backend_xxx + _calc_checksum_xxx() style
+ @classmethod
+ def _load_legacy_backend(cls, name):
value = getattr(cls, "_has_backend_" + name)
warn("%s: support for ._has_backend_%s is deprecated as of Passlib 1.7, "
- "and will be removed in Passlib 2.0, please implement "
+ "and will be removed in Passlib 1.9/2.0, please implement "
"._load_backend_%s() instead" % (cls.name, name, name),
DeprecationWarning,
)
if value:
return getattr(cls, "_calc_checksum_" + name)
else:
- return None
+ return False
- def _calc_checksum_backend(self, secret):
+ @classmethod
+ def _finalize_backend(cls, name, result, dryrun=False):
"""
- stub for _calc_checksum_backend(),
- the default backend will be selected the first time stub is called.
+ subclass to implement the common behavior used by the HasManyBackend subclasses --
+ backends should return a callable for use as _calc_checksum_backend()
"""
- # if we got here, no backend has been loaded; so load default backend
- assert not self._backend, "set_backend() failed to replace lazy loader"
- self.set_backend()
- assert self._backend, "set_backend() failed to load a default backend"
+ if not callable(result):
+ raise RuntimeError("%s: backend %r returned invalid callable: %r" %
+ (cls.name, name, result))
+ cls._calc_checksum_backend = result
+ return super(HasManyBackends, cls)._finalize_backend(name, result, dryrun=dryrun)
- # this should now invoke the backend-specific version, so call it again.
- return self._calc_checksum_backend(secret)
-
- def _calc_checksum(self, secret):
- "wrapper for backend, for common code"""
- return self._calc_checksum_backend(secret)
+ #===================================================================
+ # eoc
+ #===================================================================
#=============================================================================
# wrappers