summaryrefslogtreecommitdiff
path: root/passlib/utils
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2019-11-22 17:04:54 -0500
committerEli Collins <elic@assurancetechnologies.com>2019-11-22 17:04:54 -0500
commit482dba920f91c1e7f3906930a56126aec42caa7c (patch)
tree806c3d7b4dc65d1229ab376b6b0488438fd69d4b /passlib/utils
parentfc81bf7969129891c36496b5e2392c290b0f9b3c (diff)
parentc7c4ca4ece10ac0e1090bfa6a3bc41baabcf5ce7 (diff)
downloadpasslib-482dba920f91c1e7f3906930a56126aec42caa7c.tar.gz
Merge from stable
Diffstat (limited to 'passlib/utils')
-rw-r--r--passlib/utils/__init__.py4
-rw-r--r--passlib/utils/compat/__init__.py1
-rw-r--r--passlib/utils/handlers.py49
3 files changed, 38 insertions, 16 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index 2fc0e95..3f85389 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -538,6 +538,10 @@ def render_bytes(source, *args):
Calling ``render_bytes(source, *args)`` should function roughly the same as
``source % args`` under Python 2.
+
+ .. todo::
+ python >= 3.5 added back limited support for bytes %,
+ can revisit when 3.3/3.4 is dropped.
"""
if isinstance(source, bytes):
source = source.decode("latin-1")
diff --git a/passlib/utils/compat/__init__.py b/passlib/utils/compat/__init__.py
index 21415c4..2cc1e5b 100644
--- a/passlib/utils/compat/__init__.py
+++ b/passlib/utils/compat/__init__.py
@@ -27,6 +27,7 @@ if PYPY and sys.pypy_version_info < (2,0):
raise RuntimeError("passlib requires pypy >= 2.0 (as of passlib 1.7)")
# e.g. '2.7.7\n[Pyston 0.5.1]'
+# NOTE: deprecated support 2019-11
PYSTON = "Pyston" in sys.version
#=============================================================================
diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py
index 50440de..e53a99f 100644
--- a/passlib/utils/handlers.py
+++ b/passlib/utils/handlers.py
@@ -315,6 +315,38 @@ def render_mc3(ident, rounds, salt, checksum, sep=u"$", rounds_base=10):
parts = [ident, rounds, sep, salt]
return uascii_to_str(join_unicode(parts))
+
+def mask_value(value, show=4, pct=0.125, char=u"*"):
+ """
+ helper to mask contents of sensitive field.
+
+ :param value:
+ raw value (str, bytes, etc)
+
+ :param show:
+ max # of characters to remain visible
+
+ :param pct:
+ don't show more than this % of input.
+
+ :param char:
+ character to use for masking
+
+ :rtype: str | None
+ """
+ if value is None:
+ return None
+ if not isinstance(value, unicode):
+ if isinstance(value, bytes):
+ from passlib.utils.binary import ab64_encode
+ value = ab64_encode(value).decode("ascii")
+ else:
+ value = unicode(value)
+ size = len(value)
+ show = min(show, int(size * pct))
+ return value[:show] + char * (size - show)
+
+
#=============================================================================
# parameter helpers
#=============================================================================
@@ -831,21 +863,6 @@ class GenericHandler(MinimalHandler):
"""
return tuple(key for key in cls.setting_kwds if key not in cls._unparsed_settings)
- # XXX: make this a global function?
- @staticmethod
- def _sanitize(value, char=u"*"):
- """default method to obscure sensitive fields"""
- if value is None:
- return None
- if isinstance(value, bytes):
- from passlib.utils.binary import ab64_encode
- value = ab64_encode(value).decode("ascii")
- elif not isinstance(value, unicode):
- value = unicode(value)
- size = len(value)
- clip = min(4, size//8)
- return value[:clip] + char * (size-clip)
-
@classmethod
def parsehash(cls, hash, checksum=True, sanitize=False):
"""[experimental method] parse hash into dictionary of settings.
@@ -880,7 +897,7 @@ class GenericHandler(MinimalHandler):
kwds['checksum'] = self.checksum
if sanitize:
if sanitize is True:
- sanitize = cls._sanitize
+ sanitize = mask_value
for key in cls._unsafe_settings:
if key in kwds:
kwds[key] = sanitize(kwds[key])