2021-05-26 18:17:47 +02:00
|
|
|
import bcrypt
|
2021-10-12 14:36:47 +02:00
|
|
|
import sqlalchemy as sa
|
|
|
|
import unicodedata
|
2021-05-26 18:17:47 +02:00
|
|
|
|
2021-05-26 21:42:19 +02:00
|
|
|
_NORMALIZATION_FORM = "NFKC"
|
|
|
|
|
|
|
|
|
2021-05-26 18:17:47 +02:00
|
|
|
class PasswordOracle:
|
2021-10-12 14:36:47 +02:00
|
|
|
password = sa.Column(sa.String(128), nullable=True)
|
2021-05-26 18:17:47 +02:00
|
|
|
|
|
|
|
def set_password(self, password):
|
2021-05-26 21:42:19 +02:00
|
|
|
password = unicodedata.normalize(_NORMALIZATION_FORM, password)
|
2021-05-26 18:17:47 +02:00
|
|
|
salt = bcrypt.gensalt()
|
2021-05-29 16:22:47 +02:00
|
|
|
self.password = bcrypt.hashpw(password.encode(), salt).decode()
|
2021-05-26 18:17:47 +02:00
|
|
|
|
|
|
|
def check_password(self, password) -> bool:
|
|
|
|
if not self.password:
|
|
|
|
return False
|
2021-05-26 21:42:19 +02:00
|
|
|
|
|
|
|
password = unicodedata.normalize(_NORMALIZATION_FORM, password)
|
2021-05-29 16:22:47 +02:00
|
|
|
return bcrypt.checkpw(password.encode(), self.password.encode())
|