middleware and respnse models updated

This commit is contained in:
2025-01-27 17:25:49 +03:00
parent e403993d24
commit b88f910a43
54 changed files with 1125 additions and 808 deletions

View File

@@ -1,19 +1,16 @@
"""
Authentication related API endpoints.
"""
from typing import Any, Dict
from fastapi import Request
from typing import Union, Any, Dict
from ApiLayers.ApiValidations.Custom.wrapper_contexts import AuthContext, EventContext
from ApiLayers.Middleware import MiddlewareModule, TokenEventMiddleware
from ApiLayers.ApiValidations.Request import EmployeeSelection, OccupantSelection
from ApiLayers.Middleware import MiddlewareModule
from Events.Engine.abstract_class import MethodToEvent
from Events.base_request_model import EndpointBaseRequestModel
from Events.base_request_model import EndpointBaseRequestModel, ContextRetrievers
from .api_events import (
authentication_login_super_user_event,
authentication_select_company_or_occupant_type_super_user_event,
authentication_select_super_user_event,
authentication_check_token_event,
authentication_refresh_user_info_event,
authentication_change_password_event,
@@ -26,12 +23,6 @@ from .api_events import (
authentication_download_avatar_event,
)
from fastapi import Request
# Type aliases for common types
TokenDictType = Union["EmployeeTokenObject", "OccupantTokenObject"]
AuthenticationLoginEventMethods = MethodToEvent(
name="AuthenticationLoginEventMethods",
@@ -48,20 +39,24 @@ AuthenticationLoginEventMethods = MethodToEvent(
def authentication_login_with_domain_and_creds_endpoint(
request: Request,
data: EndpointBaseRequestModel,
request: Request, data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
event_2_catch = AuthenticationLoginEventMethods.retrieve_event(event_function_code=f"{authentication_login_super_user_event.key}")
event_2_catch = AuthenticationLoginEventMethods.retrieve_event(
event_function_code=f"{authentication_login_super_user_event.key}"
)
data = event_2_catch.REQUEST_VALIDATOR(**data.data)
return event_2_catch.endpoint_callable(request=request, data=data)
AuthenticationLoginEventMethods.endpoint_callable = authentication_login_with_domain_and_creds_endpoint
AuthenticationLoginEventMethods.endpoint_callable = (
authentication_login_with_domain_and_creds_endpoint
)
AuthenticationSelectEventMethods = MethodToEvent(
name="AuthenticationSelectEventMethods",
events={
authentication_select_company_or_occupant_type_super_user_event.key: authentication_select_company_or_occupant_type_super_user_event,
authentication_select_super_user_event.key: authentication_select_super_user_event,
},
decorators_list=[MiddlewareModule.auth_required],
headers=[],
@@ -72,28 +67,27 @@ AuthenticationSelectEventMethods = MethodToEvent(
description="Select company or occupant type",
)
def authentication_select_company_or_occupant_type(
request: Request,
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
def authentication_select_company_or_occupant_type(data: EndpointBaseRequestModel) -> Dict[str, Any]:
"""
Select company or occupant type.
"""
auth_context = authentication_select_company_or_occupant_type.auth_context
context_retriever = ContextRetrievers(func=authentication_select_company_or_occupant_type)
function = AuthenticationSelectEventMethods.retrieve_event(
event_function_code=f"{authentication_select_company_or_occupant_type_super_user_event.key}"
event_function_code=f"{authentication_select_super_user_event.key}"
)
function.endpoint_callable.auth_context = auth_context
return function.endpoint_callable(request=request, data=data)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationSelectEventMethods.endpoint_callable = authentication_select_company_or_occupant_type
AuthenticationSelectEventMethods.endpoint_callable = (
authentication_select_company_or_occupant_type
)
AuthenticationCheckTokenEventMethods = MethodToEvent(
name="AuthenticationCheckTokenEventMethods",
events={
authentication_check_token_event.key: authentication_check_token_event
},
events={authentication_check_token_event.key: authentication_check_token_event},
headers=[],
errors=[],
decorators_list=[MiddlewareModule.auth_required],
@@ -103,14 +97,20 @@ AuthenticationCheckTokenEventMethods = MethodToEvent(
description="Check if access token is valid for user",
)
def authentication_check_token_is_valid(request: Request):
def authentication_check_token_is_valid():
context_retriever = ContextRetrievers(func=authentication_check_token_is_valid)
function = AuthenticationCheckTokenEventMethods.retrieve_event(
event_function_code=f"{authentication_check_token_event.key}"
)
return function.endpoint_callable(request=request)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable()
AuthenticationCheckTokenEventMethods.endpoint_callable = authentication_check_token_is_valid
AuthenticationCheckTokenEventMethods.endpoint_callable = (
authentication_check_token_is_valid
)
AuthenticationRefreshEventMethods = MethodToEvent(
name="AuthenticationRefreshEventMethods",
@@ -127,16 +127,18 @@ AuthenticationRefreshEventMethods = MethodToEvent(
)
def authentication_refresh_user_info(request: Request):
token_dict = authentication_refresh_user_info.auth
def authentication_refresh_user_info():
context_retriever = ContextRetrievers(func=authentication_refresh_user_info)
function = AuthenticationRefreshEventMethods.retrieve_event(
event_function_code=f"{authentication_refresh_user_info_event.key}"
)
return function.endpoint_callable(request=request, token_dict=token_dict)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable()
AuthenticationRefreshEventMethods.endpoint_callable = authentication_refresh_user_info
AuthenticationChangePasswordEventMethods = MethodToEvent(
name="AuthenticationChangePasswordEventMethods",
events={
@@ -151,21 +153,23 @@ AuthenticationChangePasswordEventMethods = MethodToEvent(
description="Change password with access token",
)
def authentication_change_password_event_callable(request: Request, data: EndpointBaseRequestModel):
token_dict = authentication_change_password_event_callable.auth
def authentication_change_password_event_callable(data: EndpointBaseRequestModel):
context_retriever = ContextRetrievers(func=authentication_change_password_event_callable)
function = AuthenticationChangePasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_change_password_event.key}"
)
return function.endpoint_callable(data=data, token_dict=token_dict)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationChangePasswordEventMethods.endpoint_callable = authentication_change_password_event_callable
AuthenticationChangePasswordEventMethods.endpoint_callable = (
authentication_change_password_event_callable
)
AuthenticationCreatePasswordEventMethods = MethodToEvent(
name="AuthenticationCreatePasswordEventMethods",
events={
authentication_create_password_event: authentication_create_password_event
},
events={authentication_create_password_event: authentication_create_password_event},
headers=[],
errors=[],
url="/create-password",
@@ -174,14 +178,20 @@ AuthenticationCreatePasswordEventMethods = MethodToEvent(
description="Create password with password reset token requested via email",
)
def authentication_create_password(data: EndpointBaseRequestModel):
context_retriever = ContextRetrievers(func=authentication_create_password)
function = AuthenticationCreatePasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_create_password_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationCreatePasswordEventMethods.endpoint_callable = authentication_create_password
AuthenticationCreatePasswordEventMethods.endpoint_callable = (
authentication_create_password
)
AuthenticationDisconnectUserEventMethods = MethodToEvent(
name="AuthenticationDisconnectUserEventMethods",
@@ -197,22 +207,22 @@ AuthenticationDisconnectUserEventMethods = MethodToEvent(
description="Disconnect all sessions of user in access token",
)
def authentication_disconnect_user(request: Request, data: EndpointBaseRequestModel):
token_dict = authentication_disconnect_user.auth
def authentication_disconnect_user(data: EndpointBaseRequestModel):
context_retriever = ContextRetrievers(func=authentication_disconnect_user)
function = AuthenticationDisconnectUserEventMethods.retrieve_event(
event_function_code=f"{authentication_disconnect_user_event.key}"
)
return function.endpoint_callable(data=data, token_dict=token_dict)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationLogoutEventMethods = MethodToEvent(
name="AuthenticationLogoutEventMethods",
events={
authentication_logout_user_event.key: authentication_logout_user_event
},
events={authentication_logout_user_event.key: authentication_logout_user_event},
headers=[],
errors=[],
decorators_list=[TokenEventMiddleware.event_required],
decorators_list=[MiddlewareModule.auth_required],
url="/logout",
method="POST",
summary="Logout user",
@@ -220,12 +230,13 @@ AuthenticationLogoutEventMethods = MethodToEvent(
)
def authentication_logout_user(request: Request, data: EndpointBaseRequestModel):
event_context: EventContext = getattr(authentication_logout_user, "event_context", None)
print('event_context', event_context)
function = AuthenticationLogoutEventMethods.retrieve_event(event_function_code=f"{event_context.code}")
function.endpoint_callable.event_context = event_context
return function.endpoint_callable(request=request, data=data)
def authentication_logout_user(data: EndpointBaseRequestModel):
context_retriever = ContextRetrievers(func=authentication_logout_user)
function = AuthenticationLogoutEventMethods.retrieve_event(
event_function_code=f"{authentication_logout_user_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationLogoutEventMethods.endpoint_callable = authentication_logout_user
@@ -246,16 +257,18 @@ AuthenticationRefreshTokenEventMethods = MethodToEvent(
)
def authentication_refresher_token(request: Request, data: EndpointBaseRequestModel):
auth_context: AuthContext = getattr(authentication_refresher_token, "auth_context", None)
def authentication_refresher_token(data: EndpointBaseRequestModel):
context_retriever = ContextRetrievers(func=authentication_refresher_token)
function = AuthenticationRefreshTokenEventMethods.retrieve_event(
event_function_code=f"{authentication_refresher_token_event.key}"
)
function.endpoint_callable.auth_context = auth_context
return function.endpoint_callable(data=data, request=request)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationRefreshTokenEventMethods.endpoint_callable = authentication_refresher_token
AuthenticationRefreshTokenEventMethods.endpoint_callable = (
authentication_refresher_token
)
AuthenticationForgotPasswordEventMethods = MethodToEvent(
@@ -272,15 +285,18 @@ AuthenticationForgotPasswordEventMethods = MethodToEvent(
)
def authentication_forgot_password(request: Request, data: EndpointBaseRequestModel):
token_dict = authentication_forgot_password.auth
def authentication_forgot_password(data: EndpointBaseRequestModel):
context_retriever = ContextRetrievers(func=authentication_forgot_password)
function = AuthenticationForgotPasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_forgot_password_event.key}"
)
return function.endpoint_callable(data=data, token_dict=token_dict)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationForgotPasswordEventMethods.endpoint_callable = authentication_forgot_password
AuthenticationForgotPasswordEventMethods.endpoint_callable = (
authentication_forgot_password
)
AuthenticationResetPasswordEventMethods = MethodToEvent(
@@ -299,14 +315,18 @@ AuthenticationResetPasswordEventMethods = MethodToEvent(
def authentication_reset_password(data: EndpointBaseRequestModel):
# token_dict = authentication_reset_password.auth
context_retriever = ContextRetrievers(func=authentication_reset_password)
function = AuthenticationResetPasswordEventMethods.retrieve_event(
event_function_code=f"{authentication_reset_password_event.key}"
)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable(data=data)
AuthenticationResetPasswordEventMethods.endpoint_callable = authentication_reset_password
AuthenticationResetPasswordEventMethods.endpoint_callable = (
authentication_reset_password
)
AuthenticationDownloadAvatarEventMethods = MethodToEvent(
name="AuthenticationDownloadAvatarEventMethods",
@@ -315,7 +335,7 @@ AuthenticationDownloadAvatarEventMethods = MethodToEvent(
},
headers=[],
errors=[],
decorators_list=[],
decorators_list=[MiddlewareModule.auth_required],
url="/download-avatar",
method="POST",
summary="Download avatar",
@@ -323,13 +343,15 @@ AuthenticationDownloadAvatarEventMethods = MethodToEvent(
)
@MiddlewareModule.auth_required
def authentication_download_avatar(request: Request):
token_dict = authentication_download_avatar.auth
def authentication_download_avatar():
context_retriever = ContextRetrievers(func=authentication_download_avatar)
function = AuthenticationDownloadAvatarEventMethods.retrieve_event(
event_function_code=f"{authentication_download_avatar_event.key}"
)
return function.endpoint_callable(token_dict=token_dict)
setattr(function.endpoint_callable, context_retriever.key, context_retriever.context)
return function.endpoint_callable()
AuthenticationDownloadAvatarEventMethods.endpoint_callable = authentication_download_avatar
AuthenticationDownloadAvatarEventMethods.endpoint_callable = (
authentication_download_avatar
)