alchemy flush and save functions updated
This commit is contained in:
@@ -32,6 +32,11 @@ class PasswordModule:
|
||||
|
||||
@classmethod
|
||||
def check_hashed_password(cls, domain, id_, password, password_hashed):
|
||||
if not password_hashed:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Password is not changed yet user has no password.",
|
||||
)
|
||||
return cls.create_hashed_password(domain, id_, password) == password_hashed
|
||||
|
||||
|
||||
@@ -83,12 +88,19 @@ class AuthModule(PasswordModule):
|
||||
|
||||
def check_password(self, password):
|
||||
main_domain = self.get_main_domain_and_other_domains(get_main_domain=True)
|
||||
print('check_password', dict(
|
||||
domain=main_domain,
|
||||
id_=str(self.uu_id),
|
||||
password_hashed=self.hash_password,
|
||||
password=password,
|
||||
))
|
||||
if check_password := self.check_hashed_password(
|
||||
domain=main_domain,
|
||||
id_=self.uu_id,
|
||||
id_=str(self.uu_id),
|
||||
password_hashed=self.hash_password,
|
||||
password=password,
|
||||
):
|
||||
print('check_password', check_password)
|
||||
return check_password
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
@@ -105,59 +117,59 @@ class AuthModule(PasswordModule):
|
||||
detail="New password is same with old password.",
|
||||
)
|
||||
|
||||
def create_password(self, password, password_token=None):
|
||||
from databases import (
|
||||
MongoQueryIdentity,
|
||||
)
|
||||
if self.password_token:
|
||||
@staticmethod
|
||||
def create_password(found_user, password, password_token=None):
|
||||
from databases import MongoQueryIdentity
|
||||
if found_user.password_token:
|
||||
replace_day = 0
|
||||
try:
|
||||
replace_day = int(
|
||||
str(self.password_expires_day or 0)
|
||||
str(found_user.password_expires_day or 0)
|
||||
.split(",")[0]
|
||||
.replace(" days", "")
|
||||
)
|
||||
except Exception as e:
|
||||
err = e
|
||||
token_is_expired = system_arrow.now() >= system_arrow.get(
|
||||
self.password_expiry_begins
|
||||
found_user.password_expiry_begins
|
||||
).shift(days=replace_day)
|
||||
|
||||
if not password_token == self.password_token and token_is_expired:
|
||||
if not password_token == found_user.password_token and token_is_expired:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Password token is not valid. Please request a new password token.",
|
||||
)
|
||||
query_engine = MongoQueryIdentity(company_uuid=found_user.related_company)
|
||||
|
||||
query_engine = MongoQueryIdentity(company_uuid=self.related_company)
|
||||
domain_via_user = query_engine.get_domain_via_user(user_uu_id=str(self.uu_id))[
|
||||
domain_via_user = query_engine.get_domain_via_user(user_uu_id=str(found_user.uu_id))[
|
||||
"main_domain"
|
||||
]
|
||||
new_password_dict = {
|
||||
"password": self.create_hashed_password(
|
||||
domain=domain_via_user, id_=self.uu_id, password=password
|
||||
"password": found_user.create_hashed_password(
|
||||
domain=domain_via_user, id_=str(found_user.uu_id), password=password
|
||||
),
|
||||
"date": str(system_arrow.now()),
|
||||
"date": str(system_arrow.now().date()),
|
||||
}
|
||||
history_dict = PasswordHistoryViaUser(
|
||||
user_uu_id=str(self.uu_id),
|
||||
user_uu_id=str(found_user.uu_id),
|
||||
password_add=new_password_dict,
|
||||
access_history_detail={
|
||||
"request": "",
|
||||
"ip": "",
|
||||
},
|
||||
)
|
||||
found_user.password_expiry_begins = str(system_arrow.now())
|
||||
found_user.hash_password = new_password_dict.get("password")
|
||||
found_user.password_token = "" if found_user.password_token else ""
|
||||
found_user.save()
|
||||
query_engine.refresh_password_history_via_user(payload=history_dict)
|
||||
self.password_expiry_begins = str(system_arrow.now())
|
||||
self.hash_password = new_password_dict.get("password")
|
||||
if self.password_token:
|
||||
self.password_token = None
|
||||
self.save()
|
||||
return
|
||||
|
||||
def reset_password_token(self):
|
||||
self.password_expiry_begins = str(system_arrow.now())
|
||||
self.password_token = self.generate_token(127)
|
||||
self.save()
|
||||
@staticmethod
|
||||
def reset_password_token(found_user):
|
||||
found_user.password_expiry_begins = str(system_arrow.now())
|
||||
found_user.password_token = found_user.generate_token(127)
|
||||
found_user.save()
|
||||
|
||||
def generate_refresher_token(self, domain: str, remember_me=False):
|
||||
from databases import (
|
||||
@@ -212,8 +224,8 @@ class UserLoginModule(AuthModule):
|
||||
)
|
||||
access_token = found_user.generate_access_token()
|
||||
query_engine = MongoQueryIdentity(company_uuid=found_user.related_company)
|
||||
|
||||
if found_user.check_password(password=data.password):
|
||||
print('before access_object_to_redis')
|
||||
access_object_to_redis = save_access_token_to_redis(
|
||||
request=request,
|
||||
found_user=found_user,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import datetime
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
from .validations import PasswordHistoryViaUser, DomainViaUser, AccessHistoryViaUser
|
||||
from .mongo_database import MongoQuery
|
||||
|
||||
@@ -31,6 +33,7 @@ class MongoQueryIdentity:
|
||||
table_name=self.mongo_collection_name, database_name="mongo_database"
|
||||
)
|
||||
|
||||
|
||||
def create_domain_via_user(self, payload: DomainViaUser):
|
||||
self.use_collection("Domain")
|
||||
return self.mongo_engine.insert(
|
||||
@@ -69,15 +72,14 @@ class MongoQueryIdentity:
|
||||
"password_history": [],
|
||||
}
|
||||
)
|
||||
|
||||
password_history_item = self.mongo_engine.get_one(
|
||||
match=payload.user_uu_id, field="user_uu_id"
|
||||
)
|
||||
password_history_list = password_history_item.get("password_history", [])
|
||||
hashed_password = payload.password_add.get("password")
|
||||
|
||||
for password_in_history in password_history_list:
|
||||
if password_in_history.get("password") == str(hashed_password):
|
||||
if str(password_in_history.get("password")) == str(hashed_password):
|
||||
print('Password already used. Please enter a new password that you have not used last 3 times.')
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Password already used. Please enter a new password that you have not used last 3 times.",
|
||||
@@ -87,8 +89,7 @@ class MongoQueryIdentity:
|
||||
password_history_list.pop(0)
|
||||
|
||||
password_history_list.append(payload.password_add)
|
||||
|
||||
return self.mongo_engine.update(
|
||||
self.mongo_engine.update(
|
||||
match=payload.user_uu_id,
|
||||
payload={
|
||||
"password_history": password_history_list,
|
||||
@@ -97,6 +98,7 @@ class MongoQueryIdentity:
|
||||
},
|
||||
field="user_uu_id",
|
||||
)
|
||||
return True
|
||||
|
||||
def get_password_history_via_user(self, user_uu_id):
|
||||
self.use_collection("PasswordHistory")
|
||||
|
||||
@@ -61,6 +61,7 @@ class MongoQuery:
|
||||
return self.table.insert_many(documents=[payload])
|
||||
|
||||
def update(self, match, payload, field: str = "id"):
|
||||
print('update', match, payload, field)
|
||||
if field == "id":
|
||||
filter_ = {"_id": ObjectId(match)}
|
||||
self.table.update_one(filter=filter_, update={"$set": payload})
|
||||
|
||||
@@ -24,13 +24,14 @@ class FilterAttributes:
|
||||
FilterModel = ListOptions
|
||||
|
||||
def flush(self):
|
||||
from fastapi import status
|
||||
"""Flush the current session."""
|
||||
try:
|
||||
self.__session__.add(self)
|
||||
self.__session__.flush()
|
||||
except SQLAlchemyError as e:
|
||||
self.raise_http_exception(
|
||||
status_code="HTTP_304_NOT_MODIFIED",
|
||||
status_code="HTTP_400_BAD_REQUEST",
|
||||
error_case=e.__class__.__name__,
|
||||
data={},
|
||||
message=str(e.__context__).split("\n")[0],
|
||||
@@ -48,7 +49,7 @@ class FilterAttributes:
|
||||
cls.__session__.commit()
|
||||
except SQLAlchemyError as e:
|
||||
cls.raise_http_exception(
|
||||
status_code="HTTP_304_NOT_MODIFIED",
|
||||
status_code="HTTP_400_BAD_REQUEST",
|
||||
error_case=e.__class__.__name__,
|
||||
data={},
|
||||
message=str(e.__context__).split("\n")[0],
|
||||
@@ -92,10 +93,11 @@ class FilterAttributes:
|
||||
|
||||
@classmethod
|
||||
def add_new_arg_to_args(cls, args_list, argument, value):
|
||||
new_arg_list = list(
|
||||
new_arg_list = list(set(
|
||||
args_ for args_ in list(args_list) if isinstance(args_, SQLColumnExpression)
|
||||
)
|
||||
if not any(True for arg in new_arg_list if arg.left.key == argument):
|
||||
))
|
||||
arg_left = lambda arg_obj: getattr(getattr(arg_obj, "left", None), 'key', None)
|
||||
if not any(True for arg in new_arg_list if arg_left(arg_obj=arg) == argument):
|
||||
new_arg_list.append(value)
|
||||
return tuple(new_arg_list)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user