2022-11-10 21:47:19 +00:00
|
|
|
from binascii import hexlify
|
|
|
|
from hashlib import sha256
|
|
|
|
|
2022-09-17 18:12:14 +00:00
|
|
|
from jwt import DecodeError, ExpiredSignatureError
|
2022-11-08 15:50:28 +00:00
|
|
|
from passlib.hash import bcrypt
|
2022-09-17 18:12:14 +00:00
|
|
|
from sqlalchemy import or_
|
|
|
|
|
|
|
|
from auth.jwtcodec import JWTCodec
|
|
|
|
from auth.tokenstorage import TokenStorage
|
2022-11-08 15:50:28 +00:00
|
|
|
from base.exceptions import InvalidPassword, InvalidToken
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import local_session
|
2022-09-17 18:12:14 +00:00
|
|
|
from orm import User
|
2022-11-08 15:50:28 +00:00
|
|
|
from validations.auth import AuthInput
|
2021-06-28 09:08:09 +00:00
|
|
|
|
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
|
|
|
|
\__/\/ \____________________/\_____________________________/
|
|
|
|
| | 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:
|
|
|
|
raise InvalidPassword("User password is empty")
|
2022-09-03 10:50:14 +00:00
|
|
|
if not Password.verify(password, user.password):
|
|
|
|
raise InvalidPassword("Wrong user password")
|
|
|
|
return user
|
2021-07-14 14:45:31 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
@staticmethod
|
2022-09-17 18:12:14 +00:00
|
|
|
def oauth(inp: AuthInput) -> User:
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
|
|
|
user = (
|
2022-09-17 18:12:14 +00:00
|
|
|
session.query(User)
|
|
|
|
.filter(or_(User.oauth == inp["oauth"], User.email == inp["email"]))
|
2022-09-03 10:50:14 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if not user:
|
2022-09-17 18:12:14 +00:00
|
|
|
user = User.create(**inp)
|
2022-09-03 10:50:14 +00:00
|
|
|
if not user.oauth:
|
2022-09-17 18:12:14 +00:00
|
|
|
user.oauth = inp["oauth"]
|
2022-09-03 10:50:14 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
user = User(**user.dict())
|
|
|
|
return user
|
2022-09-17 18:12:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def onetime(token: str) -> User:
|
|
|
|
try:
|
2022-11-23 09:57:58 +00:00
|
|
|
print('[auth.identity] using one time token')
|
2022-09-17 18:12:14 +00:00
|
|
|
payload = JWTCodec.decode(token)
|
|
|
|
if not await TokenStorage.exist(f"{payload.user_id}-{token}"):
|
|
|
|
raise InvalidToken("Login token has expired, please login again")
|
|
|
|
except ExpiredSignatureError:
|
|
|
|
raise InvalidToken("Login token has expired, please try again")
|
|
|
|
except DecodeError as e:
|
|
|
|
raise InvalidToken("token format error") from e
|
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).filter_by(id=payload.user_id).first()
|
|
|
|
if not user:
|
|
|
|
raise Exception("user not exist")
|
|
|
|
if not user.emailConfirmed:
|
|
|
|
user.emailConfirmed = True
|
|
|
|
session.commit()
|
|
|
|
return user
|