updated handler exceptions
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
import hashlib
|
||||
import uuid
|
||||
import requests
|
||||
|
||||
import secrets
|
||||
from datetime import timedelta
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
from api_services.redis.functions import RedisActions
|
||||
from databases.no_sql_models.validations import (
|
||||
PasswordHistoryViaUser,
|
||||
AccessHistoryViaUser,
|
||||
)
|
||||
from api_library.date_time_actions.date_functions import system_arrow, client_arrow
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
from api_configs import ApiStatic, Auth
|
||||
|
||||
|
||||
class PasswordModule:
|
||||
@staticmethod
|
||||
def generate_token(length=32):
|
||||
return uuid.uuid4().__str__()[:length]
|
||||
return secrets.token_urlsafe(length)
|
||||
|
||||
@staticmethod
|
||||
def create_hashed_password(domain: str, id_: str, password: str):
|
||||
@@ -25,6 +26,10 @@ class PasswordModule:
|
||||
def check_password(cls, domain, id_, password, password_hashed):
|
||||
return cls.create_hashed_password(domain, id_, password) == password_hashed
|
||||
|
||||
@classmethod
|
||||
def generate_access_token(cls):
|
||||
return secrets.token_urlsafe(Auth.ACCESS_TOKEN_LENGTH)
|
||||
|
||||
|
||||
class AuthModule(PasswordModule):
|
||||
|
||||
@@ -33,6 +38,7 @@ class AuthModule(PasswordModule):
|
||||
from databases import Users
|
||||
from sqlalchemy import or_
|
||||
from fastapi import status
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
found_user: Users = Users.query.filter(
|
||||
or_(
|
||||
@@ -56,9 +62,6 @@ class AuthModule(PasswordModule):
|
||||
)
|
||||
return found_user
|
||||
|
||||
def generate_access_token(self):
|
||||
return self.generate_token(Auth.ACCESS_TOKEN_LENGTH)
|
||||
|
||||
def remove_refresher_token(self, domain, disconnect: bool = False):
|
||||
from databases import UsersTokens
|
||||
|
||||
@@ -76,6 +79,8 @@ class AuthModule(PasswordModule):
|
||||
UsersTokens.save()
|
||||
|
||||
def check_password_is_different(self, password):
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
main_domain = self.get_main_domain_and_other_domains(get_main_domain=True)
|
||||
if self.hash_password == self.create_hashed_password(
|
||||
domain=main_domain, id_=self.uu_id, password=password
|
||||
@@ -88,9 +93,9 @@ class AuthModule(PasswordModule):
|
||||
@staticmethod
|
||||
def create_password(found_user, password, password_token=None):
|
||||
from databases import MongoQueryIdentity, Users
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
found_user: Users = found_user
|
||||
|
||||
if found_user.password_token:
|
||||
replace_day = 0
|
||||
try:
|
||||
@@ -145,9 +150,7 @@ class AuthModule(PasswordModule):
|
||||
return found_user.password_token
|
||||
|
||||
def generate_refresher_token(self, domain: str, remember_me=False):
|
||||
from databases import (
|
||||
UsersTokens,
|
||||
)
|
||||
from databases import UsersTokens
|
||||
|
||||
if remember_me:
|
||||
refresh_token = self.generate_token(Auth.REFRESHER_TOKEN_LENGTH)
|
||||
@@ -183,6 +186,7 @@ class AuthModule(PasswordModule):
|
||||
|
||||
|
||||
class UserLoginModule(AuthModule):
|
||||
|
||||
@classmethod
|
||||
def set_login_details_to_mongo_database(
|
||||
cls, found_user, headers_request, access_token, record_id
|
||||
@@ -212,8 +216,8 @@ class UserLoginModule(AuthModule):
|
||||
"address": address_package,
|
||||
"user_id": found_user.id,
|
||||
}
|
||||
already_exits = mongo_db.mongo_engine.filter_by(filter_query) or None
|
||||
no_address_validates = mongo_db.mongo_engine.get_all()[0] == 0
|
||||
already_exits = mongo_db.mongo_engine.filter_by(filter_query).data
|
||||
no_address_validates = mongo_db.mongo_engine.get_all().data
|
||||
access_via_user = query_engine.update_access_history_via_user(
|
||||
AccessHistoryViaUser(
|
||||
**{
|
||||
@@ -241,7 +245,6 @@ class UserLoginModule(AuthModule):
|
||||
}
|
||||
},
|
||||
)
|
||||
print("update_mongo", update_mongo)
|
||||
else:
|
||||
insert_mongo = mongo_db.mongo_engine.insert(
|
||||
payload={
|
||||
@@ -257,14 +260,14 @@ class UserLoginModule(AuthModule):
|
||||
"is_first": True if no_address_validates else False,
|
||||
}
|
||||
)
|
||||
print("insert_mongo", insert_mongo)
|
||||
|
||||
@classmethod
|
||||
def login_user_with_credentials(cls, data, request):
|
||||
from databases.sql_models.identity.identity import Users
|
||||
from ApiServices.api_handlers.auth_actions.auth import AuthActions
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
found_user = Users.check_user_exits(
|
||||
found_user: Users = Users.check_user_exits(
|
||||
access_key=data.access_key, domain=data.domain
|
||||
)
|
||||
if not found_user:
|
||||
@@ -280,19 +283,20 @@ class UserLoginModule(AuthModule):
|
||||
)
|
||||
|
||||
if found_user.check_password(
|
||||
domain=data.domain,
|
||||
id_=found_user.uu_id,
|
||||
password_hashed=found_user.hash_password,
|
||||
password=data.password,
|
||||
):
|
||||
domain=data.domain,
|
||||
id_=found_user.uu_id,
|
||||
password_hashed=found_user.hash_password,
|
||||
password=data.password,
|
||||
):
|
||||
access_object_to_redis = AuthActions.save_access_token_to_redis(
|
||||
request=request,
|
||||
found_user=found_user,
|
||||
domain=data.domain,
|
||||
# remember_me=data.remember_me,
|
||||
)
|
||||
print("access_object_to_redis", access_object_to_redis)
|
||||
access_token = access_object_to_redis.get("access_token")
|
||||
|
||||
access_object_to_redis["user"] = found_user
|
||||
headers_request = dict(request.headers)
|
||||
headers_request["evyos-user-agent"] = headers_request.get("user-agent")
|
||||
headers_request["evyos-platform"] = headers_request.get("user-agent")
|
||||
|
||||
Reference in New Issue
Block a user