annotate MoinMoin/support/passlib/utils/__init__.py @ 6133:a6283e189869 tip

fixup: remove nonexisting passlib.utils._blowfish this was removed by the passlib 1.7.1 upgrade.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Jun 2017 18:10:19 +0200
parents 7f0616feeae9
children
rev   line source
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 """passlib.utils -- helpers for writing password hashes"""
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 # imports
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 #=============================================================================
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
5 from passlib.utils.compat import JYTHON
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 # core
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
7 from binascii import b2a_base64, a2b_base64, Error as _BinAsciiError
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8 from base64 import b64encode, b64decode
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
9 import collections
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10 from codecs import lookup as _lookup_codec
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 from functools import update_wrapper
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
12 import itertools
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
13 import inspect
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 import logging; log = logging.getLogger(__name__)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 import math
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 import os
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 import sys
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 import random
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
19 import re
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 if JYTHON: # pragma: no cover -- runtime detection
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 # Jython 2.5.2 lacks stringprep module -
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 # see http://bugs.jython.org/issue1758320
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 try:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 import stringprep
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 except ImportError:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 stringprep = None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 _stringprep_missing_reason = "not present under Jython"
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 import stringprep
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 import time
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 if stringprep:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 import unicodedata
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
33 import types
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 from warnings import warn
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 # site
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 # pkg
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
37 from passlib.utils.binary import (
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
38 # [remove these aliases in 2.0]
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
39 BASE64_CHARS, AB64_CHARS, HASH64_CHARS, BCRYPT_CHARS,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
40 Base64Engine, LazyBase64Engine, h64, h64big, bcrypt64,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
41 ab64_encode, ab64_decode, b64s_encode, b64s_decode
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
42 )
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
43 from passlib.utils.decor import (
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
44 # [remove these aliases in 2.0]
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
45 deprecated_function,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
46 deprecated_method,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
47 memoized_property,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
48 classproperty,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
49 hybrid_method,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
50 )
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 from passlib.exc import ExpectedStringError
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
52 from passlib.utils.compat import (add_doc, join_bytes, join_byte_values,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
53 join_byte_elems, irange, imap, PY3, u,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
54 join_unicode, unicode, byte_elem_value, nextgetter,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
55 unicode_or_bytes_types,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
56 get_method_function, suppress_cause)
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 # local
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 __all__ = [
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 # constants
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 'JYTHON',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 'sys_bits',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 'unix_crypt_schemes',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 'rounds_cost_values',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 # unicode helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 'consteq',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 'saslprep',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 # bytes helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 "xor_bytes",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 "render_bytes",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 # encoding helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 'is_same_codec',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 'is_ascii_safe',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 'to_bytes',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 'to_unicode',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 'to_native_str',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 # host OS
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 'has_crypt',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 'test_crypt',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 'safe_crypt',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84 'tick',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 # randomness
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 'rng',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 'getrandbytes',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 'getrandstr',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 'generate_password',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92 # object type / interface tests
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 'is_crypt_handler',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94 'is_crypt_context',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 'has_rounds_info',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 'has_salt_info',
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 ]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 # constants
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 # bitsize of system architecture (32 or 64)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104 sys_bits = int(math.log(sys.maxsize if PY3 else sys.maxint, 2) + 1.5)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 # list of hashes algs supported by crypt() on at least one OS.
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
107 # XXX: move to .registry for passlib 2.0?
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108 unix_crypt_schemes = [
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 "sha512_crypt", "sha256_crypt",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 "sha1_crypt", "bcrypt",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111 "md5_crypt",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 # "bsd_nthash",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113 "bsdi_crypt", "des_crypt",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114 ]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116 # list of rounds_cost constants
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 rounds_cost_values = [ "linear", "log2" ]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 # legacy import, will be removed in 1.8
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 from passlib.exc import MissingBackendError
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 # internal helpers
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
123 _BEMPTY = b''
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124 _UEMPTY = u("")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 _USPACE = u(" ")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 # maximum password size which passlib will allow; see exc.PasswordSizeError
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128 MAX_PASSWORD_SIZE = int(os.environ.get("PASSLIB_MAX_PASSWORD_SIZE") or 4096)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 #=============================================================================
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
131 # type helpers
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
132 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
133
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
134 class SequenceMixin(object):
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 """
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
136 helper which lets result object act like a fixed-length sequence.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
137 subclass just needs to provide :meth:`_as_tuple()`.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
138 """
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
139 def _as_tuple(self):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
140 raise NotImplemented("implement in subclass")
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
142 def __repr__(self):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
143 return repr(self._as_tuple())
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
145 def __getitem__(self, idx):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
146 return self._as_tuple()[idx]
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
148 def __iter__(self):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
149 return iter(self._as_tuple())
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
150
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
151 def __len__(self):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
152 return len(self._as_tuple())
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
153
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
154 def __eq__(self, other):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
155 return self._as_tuple() == other
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
156
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
157 def __ne__(self, other):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
158 return not self.__eq__(other)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
159
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
160 if PY3:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
161 # getargspec() is deprecated, use this under py3.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
162 # even though it's a lot more awkward to get basic info :|
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
163
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
164 _VAR_KEYWORD = inspect.Parameter.VAR_KEYWORD
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
165 _VAR_ANY_SET = set([_VAR_KEYWORD, inspect.Parameter.VAR_POSITIONAL])
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
166
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
167 def accepts_keyword(func, key):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
168 """test if function accepts specified keyword"""
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
169 params = inspect.signature(get_method_function(func)).parameters
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
170 if not params:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
171 return False
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
172 arg = params.get(key)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
173 if arg and arg.kind not in _VAR_ANY_SET:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
174 return True
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
175 # XXX: annoying what we have to do to determine if VAR_KWDS in use.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
176 return params[list(params)[-1]].kind == _VAR_KEYWORD
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
177
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
178 else:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
179
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
180 def accepts_keyword(func, key):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
181 """test if function accepts specified keyword"""
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
182 spec = inspect.getargspec(get_method_function(func))
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
183 return key in spec.args or spec.keywords is not None
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
184
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
185 def update_mixin_classes(target, add=None, remove=None, append=False,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
186 before=None, after=None, dryrun=False):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
187 """
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
188 helper to update mixin classes installed in target class.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
189
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
190 :param target:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
191 target class whose bases will be modified.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
192
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
193 :param add:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
194 class / classes to install into target's base class list.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
195
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
196 :param remove:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
197 class / classes to remove from target's base class list.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
198
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
199 :param append:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
200 by default, prepends mixins to front of list.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
201 if True, appends to end of list instead.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
202
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
203 :param after:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
204 optionally make sure all mixins are inserted after
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
205 this class / classes.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
206
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
207 :param before:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
208 optionally make sure all mixins are inserted before
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
209 this class / classes.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
210
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
211 :param dryrun:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
212 optionally perform all calculations / raise errors,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
213 but don't actually modify the class.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
214 """
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
215 if isinstance(add, type):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
216 add = [add]
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
217
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
218 bases = list(target.__bases__)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
219
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
220 # strip out requested mixins
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
221 if remove:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
222 if isinstance(remove, type):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
223 remove = [remove]
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
224 for mixin in remove:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
225 if add and mixin in add:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
226 continue
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
227 if mixin in bases:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
228 bases.remove(mixin)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
229
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
230 # add requested mixins
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
231 if add:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
232 for mixin in add:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
233 # if mixin already present (explicitly or not), leave alone
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
234 if any(issubclass(base, mixin) for base in bases):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
235 continue
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
236
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
237 # determine insertion point
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
238 if append:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
239 for idx, base in enumerate(bases):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
240 if issubclass(mixin, base):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
241 # don't insert mixin after one of it's own bases
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
242 break
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
243 if before and issubclass(base, before):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
244 # don't insert mixin after any <before> classes.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
245 break
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
246 else:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
247 # append to end
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
248 idx = len(bases)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
249 elif after:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
250 for end_idx, base in enumerate(reversed(bases)):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
251 if issubclass(base, after):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
252 # don't insert mixin before any <after> classes.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
253 idx = len(bases) - end_idx
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
254 assert bases[idx-1] == base
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
255 break
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
256 else:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
257 idx = 0
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
258 else:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
259 # insert at start
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
260 idx = 0
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
261
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
262 # insert mixin
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
263 bases.insert(idx, mixin)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
264
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
265 # modify class
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
266 if not dryrun:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
267 target.__bases__ = tuple(bases)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
268
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
269 #=============================================================================
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
270 # collection helpers
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
271 #=============================================================================
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
272 def batch(source, size):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
273 """
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
274 split iterable into chunks of <size> elements.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
275 """
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
276 if size < 1:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
277 raise ValueError("size must be positive integer")
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
278 if isinstance(source, collections.Sequence):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
279 end = len(source)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
280 i = 0
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
281 while i < end:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
282 n = i + size
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
283 yield source[i:n]
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
284 i = n
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
285 elif isinstance(source, collections.Iterable):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
286 itr = iter(source)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
287 while True:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
288 chunk_itr = itertools.islice(itr, size)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
289 try:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
290 first = next(chunk_itr)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
291 except StopIteration:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
292 break
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
293 yield itertools.chain((first,), chunk_itr)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
294 else:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
295 raise TypeError("source must be iterable")
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
296
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
297 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
298 # unicode helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
299 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
300
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
301 # XXX: should this be moved to passlib.crypto, or compat backports?
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
302
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
303 def consteq(left, right):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
304 """Check two strings/bytes for equality.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
305
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
306 This function uses an approach designed to prevent
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
307 timing analysis, making it appropriate for cryptography.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
308 a and b must both be of the same type: either str (ASCII only),
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
309 or any type that supports the buffer protocol (e.g. bytes).
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
310
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
311 Note: If a and b are of different lengths, or if an error occurs,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
312 a timing attack could theoretically reveal information about the
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
313 types and lengths of a and b--but not their values.
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
314 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
315 # NOTE:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
316 # resources & discussions considered in the design of this function:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
317 # hmac timing attack --
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
318 # http://rdist.root.org/2009/05/28/timing-attack-in-google-keyczar-library/
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
319 # python developer discussion surrounding similar function --
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
320 # http://bugs.python.org/issue15061
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
321 # http://bugs.python.org/issue14955
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
322
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
323 # validate types
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
324 if isinstance(left, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
325 if not isinstance(right, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
326 raise TypeError("inputs must be both unicode or both bytes")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
327 is_py3_bytes = False
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
328 elif isinstance(left, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
329 if not isinstance(right, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
330 raise TypeError("inputs must be both unicode or both bytes")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
331 is_py3_bytes = PY3
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
332 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
333 raise TypeError("inputs must be both unicode or both bytes")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
334
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
335 # do size comparison.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
336 # NOTE: the double-if construction below is done deliberately, to ensure
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
337 # the same number of operations (including branches) is performed regardless
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
338 # of whether left & right are the same size.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
339 same_size = (len(left) == len(right))
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
340 if same_size:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
341 # if sizes are the same, setup loop to perform actual check of contents.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
342 tmp = left
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
343 result = 0
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
344 if not same_size:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
345 # if sizes aren't the same, set 'result' so equality will fail regardless
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
346 # of contents. then, to ensure we do exactly 'len(right)' iterations
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
347 # of the loop, just compare 'right' against itself.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
348 tmp = right
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
349 result = 1
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
350
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
351 # run constant-time string comparision
6008
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
352 # TODO: use izip instead (but first verify it's faster than zip for this case)
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
353 if is_py3_bytes:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
354 for l,r in zip(tmp, right):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
355 result |= l ^ r
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
356 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
357 for l,r in zip(tmp, right):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
358 result |= ord(l) ^ ord(r)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
359 return result == 0
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
360
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
361 # keep copy of this around since stdlib's version throws error on non-ascii chars in unicode strings.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
362 # our version does, but suffers from some underlying VM issues. but something is better than
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
363 # nothing for plaintext hashes, which need this. everything else should use consteq(),
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
364 # since the stdlib one is going to be as good / better in the general case.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
365 str_consteq = consteq
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
366
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
367 try:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
368 # for py3.3 and up, use the stdlib version
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
369 from hmac import compare_digest as consteq
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
370 except ImportError:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
371 pass
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
372
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
373 # TODO: could check for cryptography package's version,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
374 # but only operates on bytes, so would need a wrapper,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
375 # or separate consteq() into a unicode & a bytes variant.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
376 # from cryptography.hazmat.primitives.constant_time import bytes_eq as consteq
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
377
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
378 def splitcomma(source, sep=","):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
379 """split comma-separated string into list of elements,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
380 stripping whitespace.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
381 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
382 source = source.strip()
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
383 if source.endswith(sep):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
384 source = source[:-1]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
385 if not source:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
386 return []
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
387 return [ elem.strip() for elem in source.split(sep) ]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
388
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
389 def saslprep(source, param="value"):
6008
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
390 """Normalizes unicode strings using SASLPrep stringprep profile.
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
391
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
392 The SASLPrep profile is defined in :rfc:`4013`.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
393 It provides a uniform scheme for normalizing unicode usernames
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
394 and passwords before performing byte-value sensitive operations
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
395 such as hashing. Among other things, it normalizes diacritic
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
396 representations, removes non-printing characters, and forbids
6008
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
397 invalid characters such as ``\\n``. Properly internationalized
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
398 applications should run user passwords through this function
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
399 before hashing.
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
400
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
401 :arg source:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
402 unicode string to normalize & validate
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
403
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
404 :param param:
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
405 Optional noun identifying source parameter in error messages
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
406 (Defaults to the string ``"value"``). This is mainly useful to make the caller's error
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
407 messages make more sense contextually.
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
408
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
409 :raises ValueError:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
410 if any characters forbidden by the SASLPrep profile are encountered.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
411
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
412 :raises TypeError:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
413 if input is not :class:`!unicode`
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
414
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
415 :returns:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
416 normalized unicode string
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
417
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
418 .. note::
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
419
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
420 This function is not available under Jython,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
421 as the Jython stdlib is missing the :mod:`!stringprep` module
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
422 (`Jython issue 1758320 <http://bugs.jython.org/issue1758320>`_).
6008
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
423
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
424 .. versionadded:: 1.6
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
425 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
426 # saslprep - http://tools.ietf.org/html/rfc4013
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
427 # stringprep - http://tools.ietf.org/html/rfc3454
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
428 # http://docs.python.org/library/stringprep.html
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
429
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
430 # validate type
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
431 # XXX: support bytes (e.g. run through want_unicode)?
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
432 # might be easier to just integrate this into cryptcontext.
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
433 if not isinstance(source, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
434 raise TypeError("input must be unicode string, not %s" %
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
435 (type(source),))
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
436
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
437 # mapping stage
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
438 # - map non-ascii spaces to U+0020 (stringprep C.1.2)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
439 # - strip 'commonly mapped to nothing' chars (stringprep B.1)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
440 in_table_c12 = stringprep.in_table_c12
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
441 in_table_b1 = stringprep.in_table_b1
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
442 data = join_unicode(
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
443 _USPACE if in_table_c12(c) else c
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
444 for c in source
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
445 if not in_table_b1(c)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
446 )
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
447
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
448 # normalize to KC form
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
449 data = unicodedata.normalize('NFKC', data)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
450 if not data:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
451 return _UEMPTY
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
452
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
453 # check for invalid bi-directional strings.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
454 # stringprep requires the following:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
455 # - chars in C.8 must be prohibited.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
456 # - if any R/AL chars in string:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
457 # - no L chars allowed in string
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
458 # - first and last must be R/AL chars
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
459 # this checks if start/end are R/AL chars. if so, prohibited loop
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
460 # will forbid all L chars. if not, prohibited loop will forbid all
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
461 # R/AL chars instead. in both cases, prohibited loop takes care of C.8.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
462 is_ral_char = stringprep.in_table_d1
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
463 if is_ral_char(data[0]):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
464 if not is_ral_char(data[-1]):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
465 raise ValueError("malformed bidi sequence in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
466 # forbid L chars within R/AL sequence.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
467 is_forbidden_bidi_char = stringprep.in_table_d2
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
468 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
469 # forbid R/AL chars if start not setup correctly; L chars allowed.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
470 is_forbidden_bidi_char = is_ral_char
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
471
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
472 # check for prohibited output - stringprep tables A.1, B.1, C.1.2, C.2 - C.9
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
473 in_table_a1 = stringprep.in_table_a1
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
474 in_table_c21_c22 = stringprep.in_table_c21_c22
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
475 in_table_c3 = stringprep.in_table_c3
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
476 in_table_c4 = stringprep.in_table_c4
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
477 in_table_c5 = stringprep.in_table_c5
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
478 in_table_c6 = stringprep.in_table_c6
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
479 in_table_c7 = stringprep.in_table_c7
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
480 in_table_c8 = stringprep.in_table_c8
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
481 in_table_c9 = stringprep.in_table_c9
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
482 for c in data:
6008
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
483 # check for chars mapping stage should have removed
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
484 assert not in_table_b1(c), "failed to strip B.1 in mapping stage"
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
485 assert not in_table_c12(c), "failed to replace C.1.2 in mapping stage"
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
486
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
487 # check for forbidden chars
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
488 if in_table_a1(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
489 raise ValueError("unassigned code points forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
490 if in_table_c21_c22(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
491 raise ValueError("control characters forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
492 if in_table_c3(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
493 raise ValueError("private use characters forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
494 if in_table_c4(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
495 raise ValueError("non-char code points forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
496 if in_table_c5(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
497 raise ValueError("surrogate codes forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
498 if in_table_c6(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
499 raise ValueError("non-plaintext chars forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
500 if in_table_c7(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
501 # XXX: should these have been caught by normalize?
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
502 # if so, should change this to an assert
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
503 raise ValueError("non-canonical chars forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
504 if in_table_c8(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
505 raise ValueError("display-modifying / deprecated chars "
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
506 "forbidden in" + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
507 if in_table_c9(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
508 raise ValueError("tagged characters forbidden in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
509
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
510 # do bidi constraint check chosen by bidi init, above
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
511 if is_forbidden_bidi_char(c):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
512 raise ValueError("forbidden bidi character in " + param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
513
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
514 return data
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
515
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
516 # replace saslprep() with stub when stringprep is missing
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
517 if stringprep is None: # pragma: no cover -- runtime detection
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
518 def saslprep(source, param="value"):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
519 """stub for saslprep()"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
520 raise NotImplementedError("saslprep() support requires the 'stringprep' "
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
521 "module, which is " + _stringprep_missing_reason)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
522
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
523 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
524 # bytes helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
525 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
526 def render_bytes(source, *args):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
527 """Peform ``%`` formating using bytes in a uniform manner across Python 2/3.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
528
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
529 This function is motivated by the fact that
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
530 :class:`bytes` instances do not support ``%`` or ``{}`` formatting under Python 3.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
531 This function is an attempt to provide a replacement:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
532 it converts everything to unicode (decoding bytes instances as ``latin-1``),
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
533 performs the required formatting, then encodes the result to ``latin-1``.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
534
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
535 Calling ``render_bytes(source, *args)`` should function roughly the same as
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
536 ``source % args`` under Python 2.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
537 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
538 if isinstance(source, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
539 source = source.decode("latin-1")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
540 result = source % tuple(arg.decode("latin-1") if isinstance(arg, bytes)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
541 else arg for arg in args)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
542 return result.encode("latin-1")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
543
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
544 if PY3:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
545 # new in py32
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
546 def bytes_to_int(value):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
547 return int.from_bytes(value, 'big')
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
548 def int_to_bytes(value, count):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
549 return value.to_bytes(count, 'big')
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
550 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
551 # XXX: can any of these be sped up?
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
552 from binascii import hexlify, unhexlify
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
553 def bytes_to_int(value):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
554 return int(hexlify(value),16)
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
555 def int_to_bytes(value, count):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
556 return unhexlify(('%%0%dx' % (count<<1)) % value)
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
557
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
558 add_doc(bytes_to_int, "decode byte string as single big-endian integer")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
559 add_doc(int_to_bytes, "encode integer as single big-endian byte string")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
560
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
561 def xor_bytes(left, right):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
562 """Perform bitwise-xor of two byte strings (must be same size)"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
563 return int_to_bytes(bytes_to_int(left) ^ bytes_to_int(right), len(left))
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
564
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
565 def repeat_string(source, size):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
566 """repeat or truncate <source> string, so it has length <size>"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
567 cur = len(source)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
568 if size > cur:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
569 mult = (size+cur-1)//cur
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
570 return (source*mult)[:size]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
571 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
572 return source[:size]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
573
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
574 _BNULL = b"\x00"
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
575 _UNULL = u("\x00")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
576
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
577 def right_pad_string(source, size, pad=None):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
578 """right-pad or truncate <source> string, so it has length <size>"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
579 cur = len(source)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
580 if size > cur:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
581 if pad is None:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
582 pad = _UNULL if isinstance(source, unicode) else _BNULL
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
583 return source+pad*(size-cur)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
584 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
585 return source[:size]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
586
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
587 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
588 # encoding helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
589 #=============================================================================
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
590 _ASCII_TEST_BYTES = b"\x00\n aA:#!\x7f"
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
591 _ASCII_TEST_UNICODE = _ASCII_TEST_BYTES.decode("ascii")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
592
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
593 def is_ascii_codec(codec):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
594 """Test if codec is compatible with 7-bit ascii (e.g. latin-1, utf-8; but not utf-16)"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
595 return _ASCII_TEST_UNICODE.encode(codec) == _ASCII_TEST_BYTES
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
596
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
597 def is_same_codec(left, right):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
598 """Check if two codec names are aliases for same codec"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
599 if left == right:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
600 return True
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
601 if not (left and right):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
602 return False
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
603 return _lookup_codec(left).name == _lookup_codec(right).name
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
604
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
605 _B80 = b'\x80'[0]
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
606 _U80 = u('\x80')
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
607 def is_ascii_safe(source):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
608 """Check if string (bytes or unicode) contains only 7-bit ascii"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
609 r = _B80 if isinstance(source, bytes) else _U80
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
610 return all(c < r for c in source)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
611
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
612 def to_bytes(source, encoding="utf-8", param="value", source_encoding=None):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
613 """Helper to normalize input to bytes.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
614
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
615 :arg source:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
616 Source bytes/unicode to process.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
617
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
618 :arg encoding:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
619 Target encoding (defaults to ``"utf-8"``).
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
620
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
621 :param param:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
622 Optional name of variable/noun to reference when raising errors
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
623
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
624 :param source_encoding:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
625 If this is specified, and the source is bytes,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
626 the source will be transcoded from *source_encoding* to *encoding*
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
627 (via unicode).
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
628
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
629 :raises TypeError: if source is not unicode or bytes.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
630
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
631 :returns:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
632 * unicode strings will be encoded using *encoding*, and returned.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
633 * if *source_encoding* is not specified, byte strings will be
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
634 returned unchanged.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
635 * if *source_encoding* is specified, byte strings will be transcoded
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
636 to *encoding*.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
637 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
638 assert encoding
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
639 if isinstance(source, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
640 if source_encoding and not is_same_codec(source_encoding, encoding):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
641 return source.decode(source_encoding).encode(encoding)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
642 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
643 return source
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
644 elif isinstance(source, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
645 return source.encode(encoding)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
646 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
647 raise ExpectedStringError(source, param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
648
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
649 def to_unicode(source, encoding="utf-8", param="value"):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
650 """Helper to normalize input to unicode.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
651
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
652 :arg source:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
653 source bytes/unicode to process.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
654
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
655 :arg encoding:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
656 encoding to use when decoding bytes instances.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
657
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
658 :param param:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
659 optional name of variable/noun to reference when raising errors.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
660
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
661 :raises TypeError: if source is not unicode or bytes.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
662
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
663 :returns:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
664 * returns unicode strings unchanged.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
665 * returns bytes strings decoded using *encoding*
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
666 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
667 assert encoding
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
668 if isinstance(source, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
669 return source
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
670 elif isinstance(source, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
671 return source.decode(encoding)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
672 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
673 raise ExpectedStringError(source, param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
674
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
675 if PY3:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
676 def to_native_str(source, encoding="utf-8", param="value"):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
677 if isinstance(source, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
678 return source.decode(encoding)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
679 elif isinstance(source, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
680 return source
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
681 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
682 raise ExpectedStringError(source, param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
683 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
684 def to_native_str(source, encoding="utf-8", param="value"):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
685 if isinstance(source, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
686 return source
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
687 elif isinstance(source, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
688 return source.encode(encoding)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
689 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
690 raise ExpectedStringError(source, param)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
691
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
692 add_doc(to_native_str,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
693 """Take in unicode or bytes, return native string.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
694
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
695 Python 2: encodes unicode using specified encoding, leaves bytes alone.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
696 Python 3: leaves unicode alone, decodes bytes using specified encoding.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
697
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
698 :raises TypeError: if source is not unicode or bytes.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
699
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
700 :arg source:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
701 source unicode or bytes string.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
702
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
703 :arg encoding:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
704 encoding to use when encoding unicode or decoding bytes.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
705 this defaults to ``"utf-8"``.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
706
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
707 :param param:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
708 optional name of variable/noun to reference when raising errors.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
709
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
710 :returns: :class:`str` instance
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
711 """)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
712
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
713 @deprecated_function(deprecated="1.6", removed="1.7")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
714 def to_hash_str(source, encoding="ascii"): # pragma: no cover -- deprecated & unused
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
715 """deprecated, use to_native_str() instead"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
716 return to_native_str(source, encoding, param="hash")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
717
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
718 _true_set = set("true t yes y on 1 enable enabled".split())
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
719 _false_set = set("false f no n off 0 disable disabled".split())
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
720 _none_set = set(["", "none"])
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
721
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
722 def as_bool(value, none=None, param="boolean"):
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
723 """
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
724 helper to convert value to boolean.
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
725 recognizes strings such as "true", "false"
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
726 """
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
727 assert none in [True, False, None]
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
728 if isinstance(value, unicode_or_bytes_types):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
729 clean = value.lower().strip()
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
730 if clean in _true_set:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
731 return True
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
732 if clean in _false_set:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
733 return False
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
734 if clean in _none_set:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
735 return none
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
736 raise ValueError("unrecognized %s value: %r" % (param, value))
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
737 elif isinstance(value, bool):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
738 return value
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
739 elif value is None:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
740 return none
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
741 else:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
742 return bool(value)
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
743
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
744 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
745 # host OS helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
746 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
747
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
748 try:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
749 from crypt import crypt as _crypt
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
750 except ImportError: # pragma: no cover
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
751 _crypt = None
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
752 has_crypt = False
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
753 def safe_crypt(secret, hash):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
754 return None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
755 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
756 has_crypt = True
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
757 _NULL = '\x00'
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
758
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
759 # some crypt() variants will return various constant strings when
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
760 # an invalid/unrecognized config string is passed in; instead of
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
761 # returning NULL / None. examples include ":", ":0", "*0", etc.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
762 # safe_crypt() returns None for any string starting with one of the
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
763 # chars in this string...
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
764 _invalid_prefixes = u("*:!")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
765
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
766 if PY3:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
767 def safe_crypt(secret, hash):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
768 if isinstance(secret, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
769 # Python 3's crypt() only accepts unicode, which is then
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
770 # encoding using utf-8 before passing to the C-level crypt().
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
771 # so we have to decode the secret.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
772 orig = secret
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
773 try:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
774 secret = secret.decode("utf-8")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
775 except UnicodeDecodeError:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
776 return None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
777 assert secret.encode("utf-8") == orig, \
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
778 "utf-8 spec says this can't happen!"
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
779 if _NULL in secret:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
780 raise ValueError("null character in secret")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
781 if isinstance(hash, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
782 hash = hash.decode("ascii")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
783 result = _crypt(secret, hash)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
784 if not result or result[0] in _invalid_prefixes:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
785 return None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
786 return result
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
787 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
788 def safe_crypt(secret, hash):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
789 if isinstance(secret, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
790 secret = secret.encode("utf-8")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
791 if _NULL in secret:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
792 raise ValueError("null character in secret")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
793 if isinstance(hash, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
794 hash = hash.encode("ascii")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
795 result = _crypt(secret, hash)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
796 if not result:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
797 return None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
798 result = result.decode("ascii")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
799 if result[0] in _invalid_prefixes:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
800 return None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
801 return result
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
802
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
803 add_doc(safe_crypt, """Wrapper around stdlib's crypt.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
804
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
805 This is a wrapper around stdlib's :func:`!crypt.crypt`, which attempts
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
806 to provide uniform behavior across Python 2 and 3.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
807
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
808 :arg secret:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
809 password, as bytes or unicode (unicode will be encoded as ``utf-8``).
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
810
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
811 :arg hash:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
812 hash or config string, as ascii bytes or unicode.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
813
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
814 :returns:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
815 resulting hash as ascii unicode; or ``None`` if the password
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
816 couldn't be hashed due to one of the issues:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
817
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
818 * :func:`crypt()` not available on platform.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
819
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
820 * Under Python 3, if *secret* is specified as bytes,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
821 it must be use ``utf-8`` or it can't be passed
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
822 to :func:`crypt()`.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
823
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
824 * Some OSes will return ``None`` if they don't recognize
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
825 the algorithm being used (though most will simply fall
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
826 back to des-crypt).
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
827
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
828 * Some OSes will return an error string if the input config
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
829 is recognized but malformed; current code converts these to ``None``
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
830 as well.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
831 """)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
832
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
833 def test_crypt(secret, hash):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
834 """check if :func:`crypt.crypt` supports specific hash
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
835 :arg secret: password to test
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
836 :arg hash: known hash of password to use as reference
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
837 :returns: True or False
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
838 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
839 assert secret and hash
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
840 return safe_crypt(secret, hash) == hash
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
841
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
842 # pick best timer function to expose as "tick" - lifted from timeit module.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
843 if sys.platform == "win32":
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
844 # On Windows, the best timer is time.clock()
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
845 from time import clock as timer
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
846 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
847 # On most other platforms the best timer is time.time()
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
848 from time import time as timer
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
849
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
850 # legacy alias, will be removed in passlib 2.0
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
851 tick = timer
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
852
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
853 def parse_version(source):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
854 """helper to parse version string"""
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
855 m = re.search(r"(\d+(?:\.\d+)+)", source)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
856 if m:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
857 return tuple(int(elem) for elem in m.group(1).split("."))
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
858 return None
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
859
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
860 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
861 # randomness
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
862 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
863
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
864 #------------------------------------------------------------------------
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
865 # setup rng for generating salts
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
866 #------------------------------------------------------------------------
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
867
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
868 # NOTE:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
869 # generating salts (e.g. h64_gensalt, below) doesn't require cryptographically
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
870 # strong randomness. it just requires enough range of possible outputs
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
871 # that making a rainbow table is too costly. so it should be ok to
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
872 # fall back on python's builtin mersenne twister prng, as long as it's seeded each time
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
873 # this module is imported, using a couple of minor entropy sources.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
874
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
875 try:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
876 os.urandom(1)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
877 has_urandom = True
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
878 except NotImplementedError: # pragma: no cover
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
879 has_urandom = False
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
880
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
881 def genseed(value=None):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
882 """generate prng seed value from system resources"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
883 from hashlib import sha512
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
884 if hasattr(value, "getstate") and hasattr(value, "getrandbits"):
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
885 # caller passed in RNG as seed value
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
886 try:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
887 value = value.getstate()
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
888 except NotImplementedError:
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
889 # this method throws error for e.g. SystemRandom instances,
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
890 # so fall back to extracting 4k of state
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
891 value = value.getrandbits(1 << 15)
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
892 text = u("%s %s %s %.15f %.15f %s") % (
6008
d72a5e95c7c0 upgrade bundled passlib to 1.6.2
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5919
diff changeset
893 # if caller specified a seed value, mix it in
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
894 value,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
895
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
896 # add current process id
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
897 # NOTE: not available in some environments, e.g. GAE
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
898 os.getpid() if hasattr(os, "getpid") else None,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
899
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
900 # id of a freshly created object.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
901 # (at least 1 byte of which should be hard to predict)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
902 id(object()),
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
903
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
904 # the current time, to whatever precision os uses
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
905 time.time(),
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
906 time.clock(),
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
907
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
908 # if urandom available, might as well mix some bytes in.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
909 os.urandom(32).decode("latin-1") if has_urandom else 0,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
910 )
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
911 # hash it all up and return it as int/long
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
912 return int(sha512(text.encode("utf-8")).hexdigest(), 16)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
913
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
914 if has_urandom:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
915 rng = random.SystemRandom()
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
916 else: # pragma: no cover -- runtime detection
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
917 # NOTE: to reseed use ``rng.seed(genseed(rng))``
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
918 # XXX: could reseed on every call
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
919 rng = random.Random(genseed())
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
920
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
921 #------------------------------------------------------------------------
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
922 # some rng helpers
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
923 #------------------------------------------------------------------------
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
924 def getrandbytes(rng, count):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
925 """return byte-string containing *count* number of randomly generated bytes, using specified rng"""
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
926 # NOTE: would be nice if this was present in stdlib Random class
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
927
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
928 ###just in case rng provides this...
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
929 ##meth = getattr(rng, "getrandbytes", None)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
930 ##if meth:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
931 ## return meth(count)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
932
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
933 if not count:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
934 return _BEMPTY
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
935 def helper():
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
936 # XXX: break into chunks for large number of bits?
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
937 value = rng.getrandbits(count<<3)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
938 i = 0
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
939 while i < count:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
940 yield value & 0xff
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
941 value >>= 3
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
942 i += 1
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
943 return join_byte_values(helper())
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
944
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
945 def getrandstr(rng, charset, count):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
946 """return string containing *count* number of chars/bytes, whose elements are drawn from specified charset, using specified rng"""
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
947 # NOTE: tests determined this is 4x faster than rng.sample(),
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
948 # which is why that's not being used here.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
949
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
950 # check alphabet & count
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
951 if count < 0:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
952 raise ValueError("count must be >= 0")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
953 letters = len(charset)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
954 if letters == 0:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
955 raise ValueError("alphabet must not be empty")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
956 if letters == 1:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
957 return charset * count
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
958
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
959 # get random value, and write out to buffer
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
960 def helper():
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
961 # XXX: break into chunks for large number of letters?
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
962 value = rng.randrange(0, letters**count)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
963 i = 0
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
964 while i < count:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
965 yield charset[value % letters]
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
966 value //= letters
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
967 i += 1
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
968
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
969 if isinstance(charset, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
970 return join_unicode(helper())
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
971 else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
972 return join_byte_elems(helper())
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
973
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
974 _52charset = '2346789ABCDEFGHJKMNPQRTUVWXYZabcdefghjkmnpqrstuvwxyz'
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
975
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
976 @deprecated_function(deprecated="1.7", removed="2.0",
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
977 replacement="passlib.pwd.genword() / passlib.pwd.genphrase()")
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
978 def generate_password(size=10, charset=_52charset):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
979 """generate random password using given length & charset
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
980
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
981 :param size:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
982 size of password.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
983
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
984 :param charset:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
985 optional string specified set of characters to draw from.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
986
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
987 the default charset contains all normal alphanumeric characters,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
988 except for the characters ``1IiLl0OoS5``, which were omitted
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
989 due to their visual similarity.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
990
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
991 :returns: :class:`!str` containing randomly generated password.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
992
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
993 .. note::
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
994
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
995 Using the default character set, on a OS with :class:`!SystemRandom` support,
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
996 this function should generate passwords with 5.7 bits of entropy per character.
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
997 """
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
998 return getrandstr(rng, charset, size)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
999
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1000 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1001 # object type / interface tests
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1002 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1003 _handler_attrs = (
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1004 "name",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1005 "setting_kwds", "context_kwds",
6130
7f0616feeae9 upgrade passlib to 1.7.1, update CHANGES
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6096
diff changeset
1006 "verify", "hash", "identify",
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1007 )
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1008
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1009 def is_crypt_handler(obj):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
1010 """check if object follows the :ref:`password-hash-api`"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1011 # XXX: change to use isinstance(obj, PasswordHash) under py26+?
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1012 return all(hasattr(obj, name) for name in _handler_attrs)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1013
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1014 _context_attrs = (
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1015 "needs_update",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1016 "genconfig", "genhash",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1017 "verify", "encrypt", "identify",
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1018 )
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1019
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1020 def is_crypt_context(obj):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
1021 """check if object appears to be a :class:`~passlib.context.CryptContext` instance"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1022 # XXX: change to use isinstance(obj, CryptContext)?
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1023 return all(hasattr(obj, name) for name in _context_attrs)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1024
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1025 ##def has_many_backends(handler):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1026 ## "check if handler provides multiple baceknds"
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1027 ## # NOTE: should also provide get_backend(), .has_backend(), and .backends attr
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1028 ## return hasattr(handler, "set_backend")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1029
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1030 def has_rounds_info(handler):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
1031 """check if handler provides the optional :ref:`rounds information <rounds-attributes>` attributes"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1032 return ('rounds' in handler.setting_kwds and
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1033 getattr(handler, "min_rounds", None) is not None)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1034
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1035 def has_salt_info(handler):
6096
86a41c2bedec upgrade passlib from 1.6.2 to 1.6.5
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 6008
diff changeset
1036 """check if handler provides the optional :ref:`salt information <salt-attributes>` attributes"""
5919
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1037 return ('salt' in handler.setting_kwds and
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1038 getattr(handler, "min_salt_size", None) is not None)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1039
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1040 ##def has_raw_salt(handler):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1041 ## "check if handler takes in encoded salt as unicode (False), or decoded salt as bytes (True)"
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1042 ## sc = getattr(handler, "salt_chars", None)
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1043 ## if sc is None:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1044 ## return None
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1045 ## elif isinstance(sc, unicode):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1046 ## return False
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1047 ## elif isinstance(sc, bytes):
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1048 ## return True
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1049 ## else:
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1050 ## raise TypeError("handler.salt_chars must be None/unicode/bytes")
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1051
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1052 #=============================================================================
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1053 # eof
efd7c0be3339 added passlib 1.6.1 to MoinMoin/support/
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1054 #=============================================================================