Skip to content

Commit

Permalink
Merge pull request #246 from petrus-v/password-column-set-from-pwd-hash
Browse files Browse the repository at this point in the history
Password column, allow to set password usin hash password directly
  • Loading branch information
jssuzanne committed Feb 29, 2024
2 parents 319d9cf + a9ffa71 commit e50e2ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions anyblok/column.py
Expand Up @@ -846,7 +846,7 @@ class Password(Column):
@Declarations.register(Declarations.Model)
class Test:
x = Password(crypt_context={'schemes': ['md5_crypt']})
x = Password(crypt_context={'schemes': ['bcrypt']})
=========================================
Expand All @@ -856,6 +856,8 @@ class Test:
test.x
==> Password object with encrypt value, the value can not be read
test.x = "$2y$10$KlpJgedTiIH7SSBFOdooJuS4Gnoqv9qEUwDfqkxZ7iB2DRhg758li"
test.x == 'mypassword'
==> True
Expand Down Expand Up @@ -887,7 +889,9 @@ def setter_format_value(self, value):
:param value:
:return:
"""
value = self.sqlalchemy_type.context.hash(value).encode("utf8")
if not self.sqlalchemy_type.context.identify(value):
value = self.sqlalchemy_type.context.hash(value).encode("utf8")

value = SAU_PWD(value, context=self.sqlalchemy_type.context)
return value

Expand Down
13 changes: 13 additions & 0 deletions anyblok/tests/test_column.py
Expand Up @@ -532,6 +532,19 @@ def test_password(self):
!= "col"
)

@pytest.mark.skipif(not has_passlib, reason="passlib is not installed")
def test_password_set_with_hash(self):
registry = self.init_registry(
simple_column,
ColumnType=Password,
crypt_context={"schemes": ["bcrypt"]},
)
bcrypt_password_hash = (
"$2y$10$Crnf9zkl67BugBmA4ASoU.phSGda4ir4JzaU64jpg2h.s92dSsaUu"
)
test = registry.Test.insert(col=bcrypt_password_hash)
assert test.col == "password"

@pytest.mark.skipif(not has_passlib, reason="passlib is not installed")
def test_password_with_foreign_key(self):
with pytest.raises(FieldException):
Expand Down
5 changes: 5 additions & 0 deletions doc/CHANGES.rst
Expand Up @@ -18,6 +18,11 @@
CHANGELOG
=========
2.3.0 (UNRELEASED)
------------------

* Password column can be set using hash (only scheme using defined in field
definition).

2.2.0 (2024-02-18)
------------------
Expand Down

0 comments on commit e50e2ff

Please sign in to comment.