Skip to content

Commit 646fd06

Browse files
committed
refactor: attempt at using types to achieve eq REFS #51
the commit remvoes the event that encrypted the password and uses a custom sqlalchemy type to achieve the same result, in theory we should be able to use on the TypeDecorator to compare values and thus achieve what we set out to the experiments performed so far don't seem to be able to use compare_values as intended
1 parent 7547ee3 commit 646fd06

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/labs/models/user.py

+5-19
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
from ..db import Base
1919
from .utils import DateTimeMixin, IdentifierMixin,\
20-
ModelCRUDMixin
20+
ModelCRUDMixin, PasswordType
2121

2222
from ..utils.auth import hash_password, verify_password
2323

24+
2425
class User(
2526
Base,
2627
IdentifierMixin,
@@ -49,7 +50,9 @@ class User(
4950
ensure that the password is never stored in plain text.
5051
5152
"""
52-
password: Mapped[str]
53+
password: Mapped[str] = mapped_column(
54+
PasswordType
55+
)
5356
otp_secret: Mapped[str]
5457

5558
first_name: Mapped[Optional[str]]
@@ -137,20 +140,3 @@ def receive_init(target, args, kwargs):
137140
"""
138141
target.otp_secret = random_base32()
139142

140-
def encrypt_password(target, value, oldvalue, initiator):
141-
""" Encrypt the password when it is set
142-
143-
This enables the application logic to simply set the plain
144-
text password and the model encrypts it on the way in.
145-
146-
The idea is to abstract this from the duties of the application.
147-
"""
148-
return hash_password(value)
149-
150-
# Support for the above method to run when the password is set
151-
event.listen(
152-
User.password,
153-
'set',
154-
encrypt_password,
155-
retval=True
156-
)

src/labs/models/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,21 @@ async def get_all(
255255
users = await async_db_session.execute(query)
256256
users = users.scalars().all()
257257
return users
258+
259+
260+
261+
from sqlalchemy import types
262+
from ..utils.auth import hash_password, verify_password
263+
264+
class PasswordType(types.TypeDecorator):
265+
266+
impl = types.String
267+
cache_ok = True
268+
269+
def process_bind_param(self, value, dialect):
270+
""" Called when the value is being written to the database
271+
272+
At that point we are literally converting the string to a hashed
273+
password and then we should be able to compare this
274+
"""
275+
return hash_password(value)

src/labs/routers/auth/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ async def login_for_auth_token(
4444
form_data.username
4545
)
4646

47+
# if user is None or not user.password == form_data.password:
4748
if user is None or not user.check_password(form_data.password):
4849
raise HTTPException(
4950
status_code=status.HTTP_401_UNAUTHORIZED,

0 commit comments

Comments
 (0)