Refactored password type
This commit is contained in:
@@ -14,7 +14,6 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
class Password(object):
|
class Password(object):
|
||||||
|
|
||||||
def __init__(self, value, context=None):
|
def __init__(self, value, context=None):
|
||||||
# Store the hash.
|
# Store the hash.
|
||||||
self.hash = value
|
self.hash = value
|
||||||
@@ -87,6 +86,12 @@ class PasswordType(types.TypeDecorator, ScalarCoercible):
|
|||||||
self.context = CryptContext(**kwargs)
|
self.context = CryptContext(**kwargs)
|
||||||
|
|
||||||
if max_length is None:
|
if max_length is None:
|
||||||
|
max_length = self.calculate_max_length()
|
||||||
|
|
||||||
|
# Set the length to the now-calculated max length.
|
||||||
|
self.length = max_length
|
||||||
|
|
||||||
|
def calculate_max_length(self):
|
||||||
# Calculate the largest possible encoded password.
|
# Calculate the largest possible encoded password.
|
||||||
# name + rounds + salt + hash + ($ * 4) of largest hash
|
# name + rounds + salt + hash + ($ * 4) of largest hash
|
||||||
max_lengths = [1024]
|
max_lengths = [1024]
|
||||||
@@ -102,11 +107,8 @@ class PasswordType(types.TypeDecorator, ScalarCoercible):
|
|||||||
)
|
)
|
||||||
max_lengths.append(length)
|
max_lengths.append(length)
|
||||||
|
|
||||||
# Set the max_length to the maximum calculated max length.
|
# Return the maximum calculated max length.
|
||||||
max_length = max(max_lengths)
|
return max(max_lengths)
|
||||||
|
|
||||||
# Set the length to the now-calculated max length.
|
|
||||||
self.length = max_length
|
|
||||||
|
|
||||||
def load_dialect_impl(self, dialect):
|
def load_dialect_impl(self, dialect):
|
||||||
if dialect.name == 'postgresql':
|
if dialect.name == 'postgresql':
|
||||||
|
Reference in New Issue
Block a user