wag-managment-api-service-l.../trash/endpoints.py

385 lines
12 KiB
Python

"""
Authentication endpoint configurations.
"""
from typing import TYPE_CHECKING, Dict, Any, Union, Annotated
from ApiServices.Token.token_handler import TokenService
from ApiValidations.Request import (
Logout,
Login,
Remember,
Forgot,
CreatePassword,
ChangePassword,
OccupantSelection,
EmployeeSelection,
)
from .auth import (
AuthenticationChangePasswordEventMethods,
AuthenticationCheckTokenEventMethods,
AuthenticationCreatePasswordEventMethods,
AuthenticationDisconnectUserEventMethods,
AuthenticationDownloadAvatarEventMethods,
AuthenticationForgotPasswordEventMethods,
AuthenticationLoginEventMethods,
AuthenticationLogoutEventMethods,
AuthenticationRefreshEventMethods,
AuthenticationRefreshTokenEventMethods,
AuthenticationResetPasswordEventMethods,
AuthenticationSelectEventMethods,
)
from .models import (
ChangePasswordRequestModel,
CreatePasswordRequestModel,
ForgotRequestModel,
LoginData,
LoginRequestModel,
LogoutRequestModel,
SelectionDataEmployee,
SelectionDataOccupant,
RememberRequestModel,
)
from ApiEvents.base_request_model import DictRequestModel, EndpointBaseRequestModel
from ApiEvents.abstract_class import (
RouteFactoryConfig,
EndpointFactoryConfig,
endpoint_wrapper,
)
if TYPE_CHECKING:
from fastapi import Request, HTTPException, status, Body
from ApiValidations.Custom.token_objects import EmployeeTokenObject, OccupantTokenObject
# Type aliases for common types
@endpoint_wrapper("/authentication/select")
async def authentication_select_company_or_occupant_type(
request: "Request",
data: Union[EmployeeSelection, OccupantSelection],
) -> Dict[str, Any]:
"""
Select company or occupant type.
"""
auth_dict = authentication_select_company_or_occupant_type.auth
if await AuthenticationSelectEventMethods.authentication_select_company_or_occupant_type(
request=request, data=data, token_dict=auth_dict
):
if data.is_employee:
return {"selected_company": data.company_uu_id, "completed": True}
elif data.is_occupant:
return {
"selected_occupant": data.build_living_space_uu_id,
"completed": True,
}
return {"completed": False, "selected_company": None, "selected_occupant": None}
@endpoint_wrapper("/authentication/login")
async def authentication_login_with_domain_and_creds(
request: "Request",
data: Login,
) -> Dict[str, Any]:
"""
Authenticate user with domain and credentials.
"""
return await AuthenticationLoginEventMethods.authentication_login_with_domain_and_creds(
request=request, data=data
)
@endpoint_wrapper("/authentication/valid")
async def authentication_check_token_is_valid(
request: "Request",
) -> Dict[str, Any]:
"""
Check if a token is valid.
"""
try:
access_token = TokenService.get_access_token_from_request(request=request)
if TokenService.get_object_via_access_key(access_token=access_token):
return {
"message": "Access Token is valid",
}
except HTTPException:
return {
"message": "Access Token is NOT valid",
}
@endpoint_wrapper("/authentication/refresh")
async def authentication_refresh_user_info(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Refresh user information.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/change-password")
async def authentication_change_password(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Change user password.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/create-password")
async def authentication_create_password(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Create new password.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/forgot-password")
async def authentication_forgot_password(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Handle forgot password request.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/reset-password")
async def authentication_reset_password(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Reset password.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/disconnect")
async def authentication_disconnect_user(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Disconnect user.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/logout")
async def authentication_logout_user(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Logout user.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/remember")
async def authentication_refresher_token(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Refresh remember token.
"""
return {
"status": "OK",
}
@endpoint_wrapper("/authentication/avatar")
async def authentication_download_avatar(
request: "Request",
data: EndpointBaseRequestModel,
) -> Dict[str, Any]:
"""
Download user avatar.
"""
return {
"status": "OK",
}
prefix = "/authentication"
AUTH_CONFIG = RouteFactoryConfig(
name="authentication",
prefix=prefix,
tags=["Authentication"],
include_in_schema=True,
endpoints=[
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/select",
url_of_endpoint="/authentication/select",
endpoint="/select",
method="POST",
summary="Select company or occupant type",
description="Select company or occupant type",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_select_company_or_occupant_type,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/login",
url_of_endpoint="/authentication/login",
endpoint="/login",
method="POST",
summary="Login user with domain and password",
description="Login user with domain and password",
is_auth_required=False, # Public endpoint
is_event_required=False,
endpoint_function=authentication_login_with_domain_and_creds,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/valid",
url_of_endpoint="/authentication/valid",
endpoint="/valid",
method="GET",
summary="Check access token is valid",
description="Check access token is valid",
is_auth_required=True, # Needs token validation
is_event_required=False,
endpoint_function=authentication_check_token_is_valid,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/refresh",
url_of_endpoint="/authentication/refresh",
endpoint="/refresh",
method="GET",
summary="Refresh credentials with access token",
description="Refresh credentials with access token",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_refresh_user_info,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/change-password",
url_of_endpoint="/authentication/change-password",
endpoint="/change-password",
method="POST",
summary="Change password with access token",
description="Change password with access token",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_change_password,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/create-password",
url_of_endpoint="/authentication/create-password",
endpoint="/create-password",
method="POST",
summary="Create password with password token",
description="Create password with password token",
is_auth_required=False, # Public endpoint
is_event_required=False,
endpoint_function=authentication_create_password,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/reset-password",
url_of_endpoint="/authentication/reset-password",
endpoint="/reset-password",
method="POST",
summary="Reset password with token",
description="Reset password with token",
is_auth_required=False, # Public endpoint
is_event_required=False,
endpoint_function=authentication_reset_password,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/disconnect",
url_of_endpoint="/authentication/disconnect",
endpoint="/disconnect",
method="POST",
summary="Disconnect user with access token",
description="Disconnect user with access token",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_disconnect_user,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/logout",
url_of_endpoint="/authentication/logout",
endpoint="/logout",
method="POST",
summary="Logout user with access token",
description="Logout user with access token",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_logout_user,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/remember",
url_of_endpoint="/authentication/remember",
endpoint="/remember",
method="POST",
summary="Refresh token with refresh token",
description="Refresh token with refresh token",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_refresher_token,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/forgot-password",
url_of_endpoint="/authentication/forgot-password",
endpoint="/forgot-password",
method="POST",
summary="Request password reset via email",
description="Request password reset via email",
is_auth_required=False, # Public endpoint
is_event_required=False,
endpoint_function=authentication_forgot_password,
),
EndpointFactoryConfig(
url_prefix=prefix,
url_endpoint="/avatar",
url_of_endpoint="/authentication/avatar",
endpoint="/avatar",
method="POST",
summary="Get user avatar with credentials",
description="Get user avatar with credentials",
is_auth_required=True, # Needs token_dict
is_event_required=False,
endpoint_function=authentication_download_avatar,
),
],
).as_dict()