mongo updated
This commit is contained in:
@@ -1,14 +1,39 @@
|
||||
import typing
|
||||
import json
|
||||
from typing import Union
|
||||
from fastapi import status
|
||||
from fastapi.requests import Request
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
from api_objects import OccupantTokenObject, EmployeeTokenObject
|
||||
from api_objects.auth.token_objects import CompanyToken, OccupantToken
|
||||
from api_services.templates.password_templates import (
|
||||
password_is_changed_template,
|
||||
change_your_password_template,
|
||||
)
|
||||
from api_services.token_service import TokenService
|
||||
from api_services.redis.functions import RedisActions
|
||||
from api_library.response_handlers import ResponseHandler
|
||||
from api_library.logger import user_logger
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
|
||||
# from api_library.user_logger import UserLogger
|
||||
|
||||
from api_validations.validations_request import (
|
||||
Login,
|
||||
Logout,
|
||||
ChangePassword,
|
||||
EmployeeSelection,
|
||||
OccupantSelection,
|
||||
CreatePassword,
|
||||
Forgot,
|
||||
# ResetPassword,
|
||||
# RefreshToken,
|
||||
)
|
||||
from api_validations.validations_response import (
|
||||
AuthenticationLoginResponse,
|
||||
AuthenticationRefreshResponse,
|
||||
AuthenticationUserInfoResponse,
|
||||
)
|
||||
from ApiServices.api_handlers.auth_actions.auth import AuthActions
|
||||
from api_configs import Auth, ApiStatic
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
|
||||
@@ -32,27 +57,6 @@ from databases import (
|
||||
|
||||
from api_services import (
|
||||
send_email,
|
||||
save_access_token_to_redis,
|
||||
update_selected_to_redis,
|
||||
password_is_changed_template,
|
||||
change_your_password_template,
|
||||
)
|
||||
|
||||
from api_validations.validations_request import (
|
||||
Login,
|
||||
Logout,
|
||||
ChangePassword,
|
||||
Remember,
|
||||
Forgot,
|
||||
CreatePassword,
|
||||
OccupantSelection,
|
||||
EmployeeSelection,
|
||||
)
|
||||
|
||||
from api_validations.validations_response import (
|
||||
AuthenticationLoginResponse,
|
||||
AuthenticationRefreshResponse,
|
||||
AuthenticationUserInfoResponse,
|
||||
)
|
||||
|
||||
|
||||
@@ -73,22 +77,20 @@ class AuthenticationLoginEventMethods(MethodToEvent):
|
||||
try:
|
||||
access_dict = Users.login_user_with_credentials(data=data, request=request)
|
||||
found_user = access_dict.get("user")
|
||||
|
||||
if not found_user:
|
||||
user_logger.log_login_attempt(
|
||||
request,
|
||||
None,
|
||||
data.domain,
|
||||
data.access_key,
|
||||
success=False,
|
||||
error="Invalid credentials",
|
||||
)
|
||||
# UserLogger.log_login_attempt(
|
||||
# request,
|
||||
# None,
|
||||
# data.domain,
|
||||
# data.access_key,
|
||||
# success=False,
|
||||
# error="Invalid credentials",
|
||||
# )
|
||||
return ResponseHandler.unauthorized("Invalid credentials")
|
||||
|
||||
user_logger.log_login_attempt(
|
||||
request, found_user.id, data.domain, data.access_key, success=True
|
||||
)
|
||||
|
||||
# UserLogger.log_login_attempt(
|
||||
# request, found_user.id, data.domain, data.access_key, success=True
|
||||
# )
|
||||
response_data = {
|
||||
"access_token": access_dict.get("access_token"),
|
||||
"refresh_token": access_dict.get("refresher_token"),
|
||||
@@ -98,12 +100,11 @@ class AuthenticationLoginEventMethods(MethodToEvent):
|
||||
return ResponseHandler.success(
|
||||
message="User logged in successfully",
|
||||
data=response_data,
|
||||
response_model=AuthenticationLoginResponse,
|
||||
)
|
||||
except Exception as e:
|
||||
user_logger.log_login_attempt(
|
||||
request, None, data.domain, data.access_key, success=False, error=str(e)
|
||||
)
|
||||
# UserLogger.log_login_attempt(
|
||||
# request, None, data.domain, data.access_key, success=False, error=str(e)
|
||||
# )
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e))
|
||||
|
||||
|
||||
@@ -198,7 +199,7 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
# Update Redis
|
||||
update_selected_to_redis(request=request, add_payload=company_token)
|
||||
AuthActions.update_selected_to_redis(request=request, add_payload=company_token)
|
||||
return ResponseHandler.success("Company selected successfully")
|
||||
|
||||
@classmethod
|
||||
@@ -265,7 +266,9 @@ class AuthenticationSelectEventMethods(MethodToEvent):
|
||||
)
|
||||
|
||||
# Update Redis
|
||||
update_selected_to_redis(request=request, add_payload=occupant_token)
|
||||
AuthActions.update_selected_to_redis(
|
||||
request=request, add_payload=occupant_token
|
||||
)
|
||||
return ResponseHandler.success("Occupant selected successfully")
|
||||
|
||||
@classmethod
|
||||
@@ -305,8 +308,8 @@ class AuthenticationCheckTokenEventMethods(MethodToEvent):
|
||||
@classmethod
|
||||
def authentication_check_token_is_valid(cls, request: Request):
|
||||
try:
|
||||
TokenService.validate_token(request)
|
||||
return ResponseHandler.success("Access Token is valid")
|
||||
if RedisActions.get_object_via_access_key(request=request):
|
||||
return ResponseHandler.success("Access Token is valid")
|
||||
except HTTPException:
|
||||
return ResponseHandler.unauthorized("Access Token is NOT valid")
|
||||
|
||||
@@ -338,16 +341,11 @@ class AuthenticationRefreshEventMethods(MethodToEvent):
|
||||
found_user = Users.filter_one(Users.uu_id == token_dict.user_uu_id).data
|
||||
if not found_user:
|
||||
return ResponseHandler.not_found("User not found")
|
||||
|
||||
user_token = UsersTokens.filter_one(
|
||||
UsersTokens.domain == found_user.domain_name,
|
||||
UsersTokens.user_id == found_user.id,
|
||||
UsersTokens.token_type == "RememberMe",
|
||||
).data
|
||||
|
||||
# Update user metadata
|
||||
TokenService.update_user_metadata(found_user, request)
|
||||
|
||||
response_data = {
|
||||
"access_token": access_token,
|
||||
"refresh_token": getattr(user_token, "token", None),
|
||||
@@ -356,7 +354,6 @@ class AuthenticationRefreshEventMethods(MethodToEvent):
|
||||
return ResponseHandler.success(
|
||||
"User info refreshed successfully",
|
||||
data=response_data,
|
||||
response_model=AuthenticationRefreshResponse,
|
||||
)
|
||||
except Exception as e:
|
||||
return ResponseHandler.error(str(e))
|
||||
@@ -392,29 +389,29 @@ class AuthenticationChangePasswordEventMethods(MethodToEvent):
|
||||
return ResponseHandler.not_found("User not found")
|
||||
|
||||
if not found_user.check_password(data.old_password):
|
||||
user_logger.log_password_change(
|
||||
request,
|
||||
found_user.id,
|
||||
"change",
|
||||
success=False,
|
||||
error="Invalid old password",
|
||||
)
|
||||
# UserLogger.log_password_change(
|
||||
# request,
|
||||
# found_user.id,
|
||||
# "change",
|
||||
# success=False,
|
||||
# error="Invalid old password",
|
||||
# )
|
||||
return ResponseHandler.unauthorized("Old password is incorrect")
|
||||
|
||||
found_user.set_password(data.new_password)
|
||||
user_logger.log_password_change(
|
||||
request, found_user.id, "change", success=True
|
||||
)
|
||||
# UserLogger.log_password_change(
|
||||
# request, found_user.id, "change", success=True
|
||||
# )
|
||||
|
||||
return ResponseHandler.success("Password changed successfully")
|
||||
except Exception as e:
|
||||
user_logger.log_password_change(
|
||||
request,
|
||||
found_user.id if found_user else None,
|
||||
"change",
|
||||
success=False,
|
||||
error=str(e),
|
||||
)
|
||||
# UserLogger.log_password_change(
|
||||
# request,
|
||||
# found_user.id if found_user else None,
|
||||
# "change",
|
||||
# success=False,
|
||||
# error=str(e),
|
||||
# )
|
||||
return ResponseHandler.error(str(e))
|
||||
|
||||
|
||||
@@ -484,7 +481,7 @@ class AuthenticationDisconnectUserEventMethods(MethodToEvent):
|
||||
user_id=str(found_user.uu_id)
|
||||
):
|
||||
for key, token_user in already_tokens.items():
|
||||
RedisActions.delete_key(key)
|
||||
RedisActions.delete(key)
|
||||
selected_user = Users.filter_one(
|
||||
Users.uu_id == token_user.get("uu_id"),
|
||||
).data
|
||||
@@ -518,9 +515,9 @@ class AuthenticationLogoutEventMethods(MethodToEvent):
|
||||
token_user = None
|
||||
if already_tokens := RedisActions.get_object_via_access_key(request=request):
|
||||
for key in already_tokens:
|
||||
token_user = json.loads(RedisActions.get_key(key) or {})
|
||||
token_user = RedisActions.get_json(key)
|
||||
if token_user.get("domain") == data.domain:
|
||||
RedisActions.delete_key(key)
|
||||
RedisActions.delete(key)
|
||||
selected_user = Users.filter_one(
|
||||
Users.uu_id == token_user.get("uu_id"),
|
||||
).data
|
||||
@@ -548,7 +545,11 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
|
||||
@classmethod
|
||||
def authentication_refresher_token(
|
||||
cls, request: Request, data: Remember, token_dict: dict = None
|
||||
# cls, request: Request, data: RefreshToken, token_dict: dict = None
|
||||
cls,
|
||||
request: Request,
|
||||
data,
|
||||
token_dict: dict = None,
|
||||
):
|
||||
token_refresher = UsersTokens.filter_by_one(
|
||||
token=data.refresh_token,
|
||||
@@ -561,7 +562,7 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
Users.id == token_refresher.user_id,
|
||||
).data:
|
||||
found_user: Users = found_user
|
||||
access_key = save_access_token_to_redis(
|
||||
access_key = AuthActions.save_access_token_to_redis(
|
||||
request=request, found_user=found_user, domain=data.domain
|
||||
)
|
||||
found_user.last_agent = request.headers.get("User-Agent", None)
|
||||
@@ -577,7 +578,6 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
|
||||
return ResponseHandler.success(
|
||||
"User is logged in successfully via refresher token",
|
||||
data=response_data,
|
||||
response_model=AuthenticationRefreshResponse,
|
||||
)
|
||||
return ResponseHandler.not_found("Invalid data")
|
||||
|
||||
@@ -604,7 +604,7 @@ class AuthenticationForgotPasswordEventMethods(MethodToEvent):
|
||||
found_user: Users = Users.check_user_exits(
|
||||
access_key=data.access_key, domain=data.domain
|
||||
)
|
||||
forgot_key = save_access_token_to_redis(
|
||||
forgot_key = AuthActions.save_access_token_to_redis(
|
||||
request=request, found_user=found_user, domain=data.domain
|
||||
)
|
||||
forgot_link = ApiStatic.forgot_link(forgot_key=forgot_key)
|
||||
@@ -708,7 +708,6 @@ class AuthenticationDownloadAvatarEventMethods(MethodToEvent):
|
||||
return ResponseHandler.success(
|
||||
"Avatar and profile is shared via user credentials",
|
||||
data=user_info,
|
||||
response_model=AuthenticationUserInfoResponse,
|
||||
)
|
||||
return ResponseHandler.not_found("Invalid data")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user