Skip to content

Commit

Permalink
Merge pull request #173 from grillazz/172-passlib-deprecationwarning-…
Browse files Browse the repository at this point in the history
…crypt-is-deprecated-and-slated-for-removal-in-python-313

replace passlib with bcrypt
  • Loading branch information
grillazz authored Oct 7, 2024
2 parents 1b0776b + 65cd767 commit db3f728
Show file tree
Hide file tree
Showing 3 changed files with 1,105 additions and 1,060 deletions.
13 changes: 5 additions & 8 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
from typing import Any

import bcrypt
from passlib.context import CryptContext
from pydantic import SecretStr
from sqlalchemy import String, LargeBinary, select
from sqlalchemy import String, LargeBinary, select, Column
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import mapped_column, Mapped

from app.models.base import Base

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


class User(Base):
id: Mapped[uuid.UUID] = mapped_column(
Expand All @@ -21,21 +18,21 @@ class User(Base):
email: Mapped[str] = mapped_column(String, nullable=False, unique=True)
first_name: Mapped[str] = mapped_column(String, nullable=False)
last_name: Mapped[str] = mapped_column(String, nullable=False)
_password: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
_password: bytes = Column(LargeBinary, nullable=False)

@property
def password(self):
return self._password.decode("utf-8")

@password.setter
def password(self, password: SecretStr):
_password_string = password.get_secret_value()
_password_string = password.get_secret_value().encode("utf-8")
self._password = bcrypt.hashpw(
_password_string.encode("utf-8"), bcrypt.gensalt()
_password_string, bcrypt.gensalt()
)

def check_password(self, password: SecretStr):
return pwd_context.verify(password.get_secret_value(), self.password)
return bcrypt.checkpw(password.get_secret_value().encode("utf-8"), self._password)

@classmethod
async def find(cls, database_session: AsyncSession, where_conditions: list[Any]):
Expand Down
Loading

0 comments on commit db3f728

Please sign in to comment.