2021-05-26 21:42:19 +02:00
|
|
|
import unicodedata
|
|
|
|
|
2021-05-26 18:17:47 +02:00
|
|
|
import bcrypt
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
|
2021-05-26 21:42:19 +02:00
|
|
|
_NORMALIZATION_FORM = "NFKC"
|
|
|
|
|
|
|
|
|
2021-05-26 18:17:47 +02:00
|
|
|
class PasswordOracle:
|
|
|
|
password = db.Column(db.String(128), nullable=True)
|
|
|
|
|
|
|
|
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())
|