diff options
Diffstat (limited to 'passlib/apache.py')
-rw-r--r-- | passlib/apache.py | 153 |
1 files changed, 28 insertions, 125 deletions
diff --git a/passlib/apache.py b/passlib/apache.py index a75f2cf..80a8a91 100644 --- a/passlib/apache.py +++ b/passlib/apache.py @@ -3,8 +3,8 @@ #============================================================================= # imports #============================================================================= -from __future__ import with_statement # core +from io import BytesIO import logging; log = logging.getLogger(__name__) import os from warnings import warn @@ -15,8 +15,7 @@ from passlib.context import CryptContext from passlib.exc import ExpectedStringError from passlib.hash import htdigest from passlib.utils import render_bytes, to_bytes, is_ascii_codec -from passlib.utils.decor import deprecated_method -from passlib.utils.compat import join_bytes, unicode, BytesIO, PY3 +from passlib.utils.compat import join_bytes # local __all__ = [ 'HtpasswdFile', @@ -50,9 +49,9 @@ class _CommonFile(object): # charset encoding used by file (defaults to utf-8) encoding = None - # whether users() and other public methods should return unicode or bytes? - # (defaults to False under PY2, True under PY3) - return_unicode = None + # whether users() and other public methods should return str or bytes? + # (defaults to True) + return_unicode = True # if bound to local file, these will be set. _path = None # local file path @@ -76,7 +75,7 @@ class _CommonFile(object): def from_string(cls, data, **kwds): """create new object from raw string. - :type data: unicode or bytes + :type data: str or bytes :arg data: database to load, as single string. @@ -107,17 +106,14 @@ class _CommonFile(object): #=================================================================== # init #=================================================================== - def __init__(self, path=None, new=False, autoload=True, autosave=False, - encoding="utf-8", return_unicode=PY3, + # XXX: add a new() classmethod, ala TOTP.new()? + + def __init__(self, path=None, new=False, autosave=False, + encoding="utf-8", return_unicode=True, ): # set encoding if not encoding: - warn("``encoding=None`` is deprecated as of Passlib 1.6, " - "and will cause a ValueError in Passlib 1.8, " - "use ``return_unicode=False`` instead.", - DeprecationWarning, stacklevel=2) - encoding = "utf-8" - return_unicode = False + raise TypeError("'encoding' is required") elif not is_ascii_codec(encoding): # htpasswd/htdigest files assumes 1-byte chars, and use ":" separator, # so only ascii-compatible encodings are allowed. @@ -131,11 +127,6 @@ class _CommonFile(object): self._mtime = 0 # init db - if not autoload: - warn("``autoload=False`` is deprecated as of Passlib 1.6, " - "and will be removed in Passlib 1.8, use ``new=True`` instead", - DeprecationWarning, stacklevel=2) - new = True if path and not new: self.load() else: @@ -181,33 +172,17 @@ class _CommonFile(object): self.load() return True - def load(self, path=None, force=True): + def load(self, path=None): """Load state from local file. If no path is specified, attempts to load from ``self.path``. :type path: str :arg path: local file to load from - - :type force: bool - :param force: - if ``force=False``, only load from ``self.path`` if file - has changed since last load. - - .. deprecated:: 1.6 - This keyword will be removed in Passlib 1.8; - Applications should use :meth:`load_if_changed` instead. """ if path is not None: with open(path, "rb") as fh: self._mtime = 0 self._load_lines(fh) - elif not force: - warn("%(name)s.load(force=False) is deprecated as of Passlib 1.6," - "and will be removed in Passlib 1.8; " - "use %(name)s.load_if_changed() instead." % - dict(name=self.__class__.__name__), - DeprecationWarning, stacklevel=2) - return self.load_if_changed() elif self._path: with open(self._path, "rb") as fh: self._mtime = os.path.getmtime(self._path) @@ -376,7 +351,7 @@ class _CommonFile(object): :returns: encoded identifer as bytes """ - if isinstance(value, unicode): + if isinstance(value, str): value = value.encode(self.encoding) elif not isinstance(value, bytes): raise ExpectedStringError(value, param) @@ -397,7 +372,7 @@ class _CommonFile(object): (usually indicates wrong encoding set for file). :returns: - field as unicode or bytes, as appropriate. + field as str or bytes, as appropriate. """ assert isinstance(value, bytes), "expected value to be bytes" if self.return_unicode: @@ -563,7 +538,7 @@ class HtpasswdFile(_CommonFile): .. versionadded:: 1.6 This feature was previously enabled by setting ``autoload=False``. - That alias has been deprecated, and will be removed in Passlib 1.8 + That alias was removed in Passlib 1.8 :type autosave: bool :param autosave: @@ -615,7 +590,7 @@ class HtpasswdFile(_CommonFile): .. versionadded:: 1.6 This keyword was previously named ``default``. That alias - has been deprecated, and will be removed in Passlib 1.8. + was removed in Passlib 1.8. .. versionchanged:: 1.6.3 @@ -641,23 +616,6 @@ class HtpasswdFile(_CommonFile): will probably not be usable by another application, and particularly not by Apache. - :param autoload: - Set to ``False`` to prevent the constructor from automatically - loaded the file from disk. - - .. deprecated:: 1.6 - This has been replaced by the *new* keyword. - Instead of setting ``autoload=False``, you should use - ``new=True``. Support for this keyword will be removed - in Passlib 1.8. - - :param default: - Change the default algorithm used to hash new passwords. - - .. deprecated:: 1.6 - This has been renamed to *default_scheme* for clarity. - Support for this alias will be removed in Passlib 1.8. - Loading & Saving ================ .. automethod:: load @@ -713,12 +671,6 @@ class HtpasswdFile(_CommonFile): #=================================================================== def __init__(self, path=None, default_scheme=None, context=htpasswd_context, **kwds): - if 'default' in kwds: - warn("``default`` is deprecated as of Passlib 1.6, " - "and will be removed in Passlib 1.8, it has been renamed " - "to ``default_scheem``.", - DeprecationWarning, stacklevel=2) - default_scheme = kwds.pop("default") if default_scheme: if default_scheme in _warn_no_bcrypt: warn("HtpasswdFile: no bcrypt backends available, " @@ -727,7 +679,7 @@ class HtpasswdFile(_CommonFile): default_scheme = htpasswd_defaults.get(default_scheme, default_scheme) context = context.copy(default=default_scheme) self.context = context - super(HtpasswdFile, self).__init__(path, **kwds) + super().__init__(path, **kwds) def _parse_record(self, record, lineno): # NOTE: should return (user, hash) tuple @@ -772,24 +724,17 @@ class HtpasswdFile(_CommonFile): .. versionchanged:: 1.6 This method was previously called ``update``, it was renamed to prevent ambiguity with the dictionary method. - The old alias is deprecated, and will be removed in Passlib 1.8. + The old alias was removed in Passlib 1.8. """ hash = self.context.hash(password) return self.set_hash(user, hash) - @deprecated_method(deprecated="1.6", removed="1.8", - replacement="set_password") - def update(self, user, password): - """set password for user""" - return self.set_password(user, password) - def get_hash(self, user): """Return hash stored for user, or ``None`` if user not found. .. versionchanged:: 1.6 This method was previously named ``find``, it was renamed - for clarity. The old name is deprecated, and will be removed - in Passlib 1.8. + for clarity. The old name was removed in Passlib 1.8. """ try: return self._records[self._encode_user(user)] @@ -807,19 +752,13 @@ class HtpasswdFile(_CommonFile): .. versionadded:: 1.7 """ # assert self.context.identify(hash), "unrecognized hash format" - if PY3 and isinstance(hash, str): + if isinstance(hash, str): hash = hash.encode(self.encoding) user = self._encode_user(user) existing = self._set_record(user, hash) self._autosave() return existing - @deprecated_method(deprecated="1.6", removed="1.8", - replacement="get_hash") - def find(self, user): - """return hash for user""" - return self.get_hash(user) - # XXX: rename to something more explicit, like delete_user()? def delete(self, user): """Delete user's entry. @@ -848,13 +787,13 @@ class HtpasswdFile(_CommonFile): .. versionchanged:: 1.6 This method was previously called ``verify``, it was renamed to prevent ambiguity with the :class:`!CryptContext` method. - The old alias is deprecated, and will be removed in Passlib 1.8. + The old alias was removed in Passlib 1.8. """ user = self._encode_user(user) hash = self._records.get(user) if hash is None: return None - if isinstance(password, unicode): + if isinstance(password, str): # NOTE: encoding password to match file, making the assumption # that server will use same encoding to hash the password. password = password.encode(self.encoding) @@ -866,12 +805,6 @@ class HtpasswdFile(_CommonFile): self._autosave() return ok - @deprecated_method(deprecated="1.6", removed="1.8", - replacement="check_password") - def verify(self, user, password): - """verify password for user""" - return self.check_password(user, password) - #=================================================================== # eoc #=================================================================== @@ -928,7 +861,7 @@ class HtdigestFile(_CommonFile): .. versionadded:: 1.6 This feature was previously enabled by setting ``autoload=False``. - That alias has been deprecated, and will be removed in Passlib 1.8 + That alias was removed in Passlib 1.8 :type autosave: bool :param autosave: @@ -949,16 +882,6 @@ class HtdigestFile(_CommonFile): This is also exposed as a readonly instance attribute. - :param autoload: - Set to ``False`` to prevent the constructor from automatically - loaded the file from disk. - - .. deprecated:: 1.6 - This has been replaced by the *new* keyword. - Instead of setting ``autoload=False``, you should use - ``new=True``. Support for this keyword will be removed - in Passlib 1.8. - Loading & Saving ================ .. automethod:: load @@ -1030,7 +953,7 @@ class HtdigestFile(_CommonFile): #=================================================================== def __init__(self, path=None, default_realm=None, **kwds): self.default_realm = default_realm - super(HtdigestFile, self).__init__(path, **kwds) + super().__init__(path, **kwds) def _parse_record(self, record, lineno): result = record.rstrip().split(_BCOLON) @@ -1121,12 +1044,6 @@ class HtdigestFile(_CommonFile): hash = htdigest.hash(password, user, realm, encoding=self.encoding) return self.set_hash(user, realm, hash) - @deprecated_method(deprecated="1.6", removed="1.8", - replacement="set_password") - def update(self, user, realm, password): - """set password for user""" - return self.set_password(user, realm, password) - def get_hash(self, user, realm=None): """Return :class:`~passlib.hash.htdigest` hash stored for user. @@ -1135,15 +1052,13 @@ class HtdigestFile(_CommonFile): .. versionchanged:: 1.6 This method was previously named ``find``, it was renamed - for clarity. The old name is deprecated, and will be removed - in Passlib 1.8. + for clarity. The old name is was removed Passlib 1.8. """ key = self._encode_key(user, realm) hash = self._records.get(key) if hash is None: return None - if PY3: - hash = hash.decode(self.encoding) + hash = hash.decode(self.encoding) return hash def set_hash(self, user, realm=None, hash=_UNSET): @@ -1165,19 +1080,13 @@ class HtdigestFile(_CommonFile): # called w/ two args - (user, hash), use default realm realm, hash = None, realm # assert htdigest.identify(hash), "unrecognized hash format" - if PY3 and isinstance(hash, str): + if isinstance(hash, str): hash = hash.encode(self.encoding) key = self._encode_key(user, realm) existing = self._set_record(key, hash) self._autosave() return existing - @deprecated_method(deprecated="1.6", removed="1.8", - replacement="get_hash") - def find(self, user, realm): - """return hash for user""" - return self.get_hash(user, realm) - # XXX: rename to something more explicit, like delete_user()? def delete(self, user, realm=None): """Delete user's entry for specified realm. @@ -1227,7 +1136,7 @@ class HtdigestFile(_CommonFile): .. versionchanged:: 1.6 This method was previously called ``verify``, it was renamed to prevent ambiguity with the :class:`!CryptContext` method. - The old alias is deprecated, and will be removed in Passlib 1.8. + The old alias was removed in Passlib 1.8. """ if password is _UNSET: # called w/ two args - (user, password), use default realm @@ -1240,12 +1149,6 @@ class HtdigestFile(_CommonFile): return htdigest.verify(password, hash, user, realm, encoding=self.encoding) - @deprecated_method(deprecated="1.6", removed="1.8", - replacement="check_password") - def verify(self, user, realm, password): - """verify password for user""" - return self.check_password(user, realm, password) - #=================================================================== # eoc #=================================================================== |