2023-10-26 20:38:31 +00:00
|
|
|
from binascii import hexlify
|
|
|
|
from hashlib import sha256
|
2023-10-26 21:07:35 +00:00
|
|
|
|
2023-10-26 20:38:31 +00:00
|
|
|
from passlib.hash import bcrypt
|
2023-10-26 21:07:35 +00:00
|
|
|
|
2025-02-11 09:00:35 +00:00
|
|
|
from auth.exceptions import ExpiredToken, InvalidToken
|
2023-10-26 21:07:35 +00:00
|
|
|
from auth.jwtcodec import JWTCodec
|
|
|
|
from auth.tokenstorage import TokenStorage
|
2025-02-09 19:26:50 +00:00
|
|
|
from orm.user import User
|
2021-06-28 09:08:09 +00:00
|
|
|
|
2025-02-11 09:00:35 +00:00
|
|
|
# from base.exceptions import InvalidPassword, InvalidToken
|
|
|
|
from services.db import local_session
|
|
|
|
|
2022-09-17 18:12:14 +00:00
|
|
|
|
|
|
|
class Password:
|
|
|
|
@staticmethod
|
2022-11-10 21:47:19 +00:00
|
|
|
def _to_bytes(data: str) -> bytes:
|
|
|
|
return bytes(data.encode())
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_sha256(cls, password: str) -> bytes:
|
|
|
|
bytes_password = cls._to_bytes(password)
|
|
|
|
return hexlify(sha256(bytes_password).digest())
|
2022-11-08 15:50:28 +00:00
|
|
|
|
2022-11-10 21:47:19 +00:00
|
|
|
@staticmethod
|
|
|
|
def encode(password: str) -> str:
|
|
|
|
password_sha256 = Password._get_sha256(password)
|
|
|
|
return bcrypt.using(rounds=10).hash(password_sha256)
|
2022-09-17 18:12:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-11-08 15:50:28 +00:00
|
|
|
def verify(password: str, hashed: str) -> bool:
|
2022-11-10 21:47:19 +00:00
|
|
|
"""
|
|
|
|
Verify that password hash is equal to specified hash. Hash format:
|
|
|
|
|
|
|
|
$2a$10$Ro0CUfOqk6cXEKf3dyaM7OhSCvnwM9s4wIX9JeLapehKK5YdLxKcm
|
2023-10-30 21:00:55 +00:00
|
|
|
\__/\/ \____________________/\_____________________________/ # noqa: W605
|
2022-11-10 21:47:19 +00:00
|
|
|
| | Salt Hash
|
|
|
|
| Cost
|
|
|
|
Version
|
|
|
|
|
|
|
|
More info: https://passlib.readthedocs.io/en/stable/lib/passlib.hash.bcrypt.html
|
|
|
|
|
|
|
|
:param password: clear text password
|
|
|
|
:param hashed: hash of the password
|
|
|
|
:return: True if clear text password matches specified hash
|
|
|
|
"""
|
|
|
|
hashed_bytes = Password._to_bytes(hashed)
|
|
|
|
password_sha256 = Password._get_sha256(password)
|
|
|
|
|
|
|
|
return bcrypt.verify(password_sha256, hashed_bytes)
|
2021-07-14 14:45:31 +00:00
|
|
|
|
2021-06-28 09:08:09 +00:00
|
|
|
|
|
|
|
class Identity:
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
2022-09-17 18:12:14 +00:00
|
|
|
def password(orm_user: User, password: str) -> User:
|
2022-09-17 19:48:21 +00:00
|
|
|
user = User(**orm_user.dict())
|
2022-09-05 16:12:49 +00:00
|
|
|
if not user.password:
|
2023-01-10 08:15:28 +00:00
|
|
|
# raise InvalidPassword("User password is empty")
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "User password is empty"}
|
2022-09-03 10:50:14 +00:00
|
|
|
if not Password.verify(password, user.password):
|
2023-01-10 08:15:28 +00:00
|
|
|
# raise InvalidPassword("Wrong user password")
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "Wrong user password"}
|
2022-09-03 10:50:14 +00:00
|
|
|
return user
|
2021-07-14 14:45:31 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
2023-11-08 18:12:55 +00:00
|
|
|
def oauth(inp) -> User:
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
2023-11-08 18:12:55 +00:00
|
|
|
user = session.query(User).filter(User.email == inp["email"]).first()
|
2022-09-03 10:50:14 +00:00
|
|
|
if not user:
|
2023-11-08 18:12:55 +00:00
|
|
|
user = User.create(**inp, emailConfirmed=True)
|
2022-09-03 10:50:14 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
return user
|
2022-09-17 18:12:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def onetime(token: str) -> User:
|
|
|
|
try:
|
2023-10-30 21:00:55 +00:00
|
|
|
print("[auth.identity] using one time token")
|
2022-09-17 18:12:14 +00:00
|
|
|
payload = JWTCodec.decode(token)
|
2023-01-31 06:57:35 +00:00
|
|
|
if not await TokenStorage.exist(f"{payload.user_id}-{payload.username}-{token}"):
|
2023-01-10 08:15:28 +00:00
|
|
|
# raise InvalidToken("Login token has expired, please login again")
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "Token has expired"}
|
2025-02-09 19:26:50 +00:00
|
|
|
except ExpiredToken:
|
2023-01-10 08:15:28 +00:00
|
|
|
# raise InvalidToken("Login token has expired, please try again")
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "Token has expired"}
|
2025-02-09 19:26:50 +00:00
|
|
|
except InvalidToken:
|
2023-01-10 08:15:28 +00:00
|
|
|
# raise InvalidToken("token format error") from e
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "Token format error"}
|
2022-09-17 18:12:14 +00:00
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).filter_by(id=payload.user_id).first()
|
|
|
|
if not user:
|
2023-01-10 08:15:28 +00:00
|
|
|
# raise Exception("user not exist")
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "User does not exist"}
|
2022-09-17 18:12:14 +00:00
|
|
|
if not user.emailConfirmed:
|
|
|
|
user.emailConfirmed = True
|
|
|
|
session.commit()
|
|
|
|
return user
|