auth updated routes tested & password is not yet tested
This commit is contained in:
parent
a0b1b1bef9
commit
f7eedb5ea0
|
|
@ -6,18 +6,16 @@
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="b5202e0c-6ddf-4a56-a13a-e18798c4c7cf" name="Changes" comment="">
|
<list default="true" id="b5202e0c-6ddf-4a56-a13a-e18798c4c7cf" name="Changes" comment="">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/ApiLayers/AllConfigs/Redis/configs.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/AllConfigs/Redis/configs.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/ApiLayers/ApiServices/Token/token_handler.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/ApiServices/Token/token_handler.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/ApiLayers/ApiValidations/Response/default_response.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/ApiValidations/Response/default_response.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/Request/Auth/login.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/Request/Auth/login.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/Request/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/Request/__init__.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/Response/authentication/auth.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/Response/authentication/auth.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/Response/authentication/auth.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/Response/authentication/auth.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/set_defaults/language_setters.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/set_defaults/language_setters.py" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/api_events.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/api_events.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/api_events.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/api_events.py" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/auth.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/auth.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/function_handlers.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/function_handlers.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/function_handlers.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/function_handlers.py" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/info.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/info.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/models.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/models.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/models.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/models.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Events/Engine/abstract_class.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/Engine/abstract_class.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Events/Engine/abstract_class.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/Engine/abstract_class.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Services/Email/send_email.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/Email/send_email.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Events/base_request_model.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/base_request_model.py" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/Services/PostgresDb/Models/filter_functions.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/PostgresDb/Models/filter_functions.py" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/Services/Redis/Models/access.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/Redis/Models/access.py" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
|
@ -42,14 +40,14 @@
|
||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.git.unshallow": "true",
|
"RunOnceActivity.git.unshallow": "true",
|
||||||
"git-widget-placeholder": "development",
|
"git-widget-placeholder": "development",
|
||||||
"last_opened_file_path": "/home/berkay/git-gitea-evyos/wag-managment-api-service-version-5/ApiLayers/LanguageModels/templates"
|
"last_opened_file_path": "/home/berkay/git-gitea-evyos/wag-managment-api-service-version-5/ApiLayers/LanguageModels/templates"
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="$PROJECT_DIR$/ApiLayers/LanguageModels/templates" />
|
<recent name="$PROJECT_DIR$/ApiLayers/LanguageModels/templates" />
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
from typing import List, Union, TypeVar, Dict, Any, TYPE_CHECKING
|
from typing import List, Union, TypeVar, Dict, Any, TYPE_CHECKING
|
||||||
|
|
||||||
|
import arrow
|
||||||
|
|
||||||
from ApiLayers.AllConfigs.Token.config import Auth
|
from ApiLayers.AllConfigs.Token.config import Auth
|
||||||
from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
|
from ApiLayers.ApiLibrary.common.line_number import get_line_number_for_error
|
||||||
from ApiLayers.ApiLibrary.date_time_actions.date_functions import DateTimeLocal
|
from ApiLayers.ApiLibrary.date_time_actions.date_functions import DateTimeLocal
|
||||||
|
|
@ -51,11 +53,7 @@ class TokenService:
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_user_tokens(cls, user: Users) -> RedisResponse:
|
def _get_user_tokens(cls, user: Users) -> RedisResponse:
|
||||||
"""Get all tokens for a user from Redis."""
|
"""Get all tokens for a user from Redis."""
|
||||||
return RedisActions.get_json(
|
return RedisActions.get_json(list_keys=[f"*:{str(user.uu_id)}"])
|
||||||
list_keys=AccessToken(
|
|
||||||
userUUID=user.uu_id,
|
|
||||||
).to_list()
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def do_occupant_login(
|
def do_occupant_login(
|
||||||
|
|
@ -147,6 +145,7 @@ class TokenService:
|
||||||
userUUID=user.uu_id,
|
userUUID=user.uu_id,
|
||||||
accessToken=cls._create_access_token(),
|
accessToken=cls._create_access_token(),
|
||||||
)
|
)
|
||||||
|
cls.remove_token_with_domain(user=user, domain=model.get("domain"))
|
||||||
redis_action = RedisActions.set_json(
|
redis_action = RedisActions.set_json(
|
||||||
list_keys=access_object.to_list(),
|
list_keys=access_object.to_list(),
|
||||||
value=model,
|
value=model,
|
||||||
|
|
@ -282,8 +281,9 @@ class TokenService:
|
||||||
remember: bool,
|
remember: bool,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""Set access token to redis and handle user session."""
|
"""Set access token to redis and handle user session."""
|
||||||
|
from ApiLayers.AllConfigs.Token.config import Auth
|
||||||
cls.remove_token_with_domain(user=user, domain=domain)
|
cls.remove_token_with_domain(user=user, domain=domain)
|
||||||
Users.client_arrow = DateTimeLocal(is_client=True, timezone=user.local_timezone)
|
# Users.client_arrow = DateTimeLocal(is_client=True, timezone=user.local_timezone)
|
||||||
login_dict, db_session = {}, UsersTokens.new_session()
|
login_dict, db_session = {}, UsersTokens.new_session()
|
||||||
if user.is_occupant: # Handle login based on user type
|
if user.is_occupant: # Handle login based on user type
|
||||||
login_dict = cls.do_occupant_login(
|
login_dict = cls.do_occupant_login(
|
||||||
|
|
@ -295,30 +295,35 @@ class TokenService:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Handle remember me functionality
|
# Handle remember me functionality
|
||||||
|
user.remember_me = bool(remember)
|
||||||
if remember:
|
if remember:
|
||||||
|
users_token_created = cls._create_access_token(access=False)
|
||||||
|
login_dict["refresh_token"] = users_token_created
|
||||||
users_token = UsersTokens.find_or_create(
|
users_token = UsersTokens.find_or_create(
|
||||||
db=db_session,
|
db=db_session,
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
token_type="RememberMe",
|
token_type="RememberMe",
|
||||||
token=cls._create_access_token(access=False),
|
|
||||||
domain=domain,
|
domain=domain,
|
||||||
)
|
)
|
||||||
if users_token.meta_data.get("created"):
|
if users_token.meta_data.created:
|
||||||
user.remember_me = True
|
users_token.token = users_token_created
|
||||||
|
users_token.save(db=db_session)
|
||||||
else:
|
else:
|
||||||
if UsersTokens.filter_all(
|
if arrow.now() > arrow.get(str(users_token.expires_at)): # Check if token is expired
|
||||||
|
users_token.token = users_token_created
|
||||||
|
users_token.expires_at = str(arrow.now().datetime + Auth.TOKEN_EXPIRE_DAY_1)
|
||||||
|
users_token.save(db=db_session)
|
||||||
|
else:
|
||||||
|
login_dict["refresh_token"] = users_token.token
|
||||||
|
else:
|
||||||
|
already_refresher = UsersTokens.filter_all(
|
||||||
UsersTokens.user_id == user.id,
|
UsersTokens.user_id == user.id,
|
||||||
UsersTokens.token_type == "RememberMe",
|
UsersTokens.token_type == "RememberMe",
|
||||||
UsersTokens.domain == domain,
|
UsersTokens.domain == domain,
|
||||||
db=db_session,
|
db=db_session,
|
||||||
).data:
|
)
|
||||||
UsersTokens.filter_all(
|
if already_refresher.count:
|
||||||
UsersTokens.user_id == user.id,
|
already_refresher.query.delete(synchronize_session=False)
|
||||||
UsersTokens.token_type == "RememberMe",
|
|
||||||
UsersTokens.domain == domain,
|
|
||||||
db=db_session,
|
|
||||||
).query.delete(synchronize_session=False)
|
|
||||||
user.remember_me = False
|
|
||||||
user.save(db=db_session)
|
user.save(db=db_session)
|
||||||
return {**login_dict, "user": user.get_dict()}
|
return {**login_dict, "user": user.get_dict()}
|
||||||
|
|
||||||
|
|
@ -399,7 +404,6 @@ class TokenService:
|
||||||
return EmployeeTokenObject(**redis_object)
|
return EmployeeTokenObject(**redis_object)
|
||||||
elif redis_object.get("user_type") == UserType.occupant.value:
|
elif redis_object.get("user_type") == UserType.occupant.value:
|
||||||
return OccupantTokenObject(**redis_object)
|
return OccupantTokenObject(**redis_object)
|
||||||
|
|
||||||
raise HTTPExceptionApi(
|
raise HTTPExceptionApi(
|
||||||
error_code="",
|
error_code="",
|
||||||
lang="en",
|
lang="en",
|
||||||
|
|
|
||||||
|
|
@ -49,12 +49,20 @@ authResponses = {
|
||||||
"message": "Password changed successfully.",
|
"message": "Password changed successfully.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"DISCONNECTED_USER": {
|
"LOGOUT_USER": {
|
||||||
"tr": {
|
"tr": {
|
||||||
"message": "Kullanıcı başarılı bir şekilde çıkış yaptı.",
|
"message": "Kullanıcı başarılı bir şekilde çıkış yaptı.",
|
||||||
},
|
},
|
||||||
"en": {
|
"en": {
|
||||||
"message": "User logged out successfully.",
|
"message": "User successfully logged out.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"DISCONNECTED_USER": {
|
||||||
|
"tr": {
|
||||||
|
"message": "Kullanıcı tüm cihazlardan başarılı bir şekilde çıkış yaptı.",
|
||||||
|
},
|
||||||
|
"en": {
|
||||||
|
"message": "User successfully logged out of all devices.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"USER_NOT_FOUND": {
|
"USER_NOT_FOUND": {
|
||||||
|
|
@ -72,5 +80,21 @@ authResponses = {
|
||||||
"en": {
|
"en": {
|
||||||
"message": "Password reset request created successfully.",
|
"message": "Password reset request created successfully.",
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
"USER_AVATAR": {
|
||||||
|
"tr": {
|
||||||
|
"message": "Kullanıcı avatarı data blogunda belirtildiği şekildedir.",
|
||||||
|
},
|
||||||
|
"en": {
|
||||||
|
"message": "User avatar is as specified in the data block.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"TOKEN_REFRESH": {
|
||||||
|
"tr": {
|
||||||
|
"message": "Token başarılı bir şekilde yenilendi.",
|
||||||
|
},
|
||||||
|
"en": {
|
||||||
|
"message": "Token successfully refreshed.",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ authentication_disconnect_user_event.endpoint_callable = (
|
||||||
authentication_logout_user_event = Event(
|
authentication_logout_user_event = Event(
|
||||||
name="authentication_logout_user_event",
|
name="authentication_logout_user_event",
|
||||||
key="g1j8i6j7-9k4h-0h6l-4i3j-2j0k1k0j0i0k",
|
key="g1j8i6j7-9k4h-0h6l-4i3j-2j0k1k0j0i0k",
|
||||||
request_validator=None, # TODO: Add request validator
|
request_validator=AuthenticationRequestModels.LogoutRequestModel,
|
||||||
language_models=[],
|
language_models=[],
|
||||||
# response_validator=None, # TODO: Add response validator
|
# response_validator=None, # TODO: Add response validator
|
||||||
description="Logout user session",
|
description="Logout user session",
|
||||||
|
|
@ -140,9 +140,9 @@ authentication_logout_user_event.endpoint_callable = (
|
||||||
authentication_refresher_token_event = Event(
|
authentication_refresher_token_event = Event(
|
||||||
name="authentication_refresher_token_event",
|
name="authentication_refresher_token_event",
|
||||||
key="h2k9j7k8-0l5i-1i7m-5j4k-3k1l2l1k1j1l",
|
key="h2k9j7k8-0l5i-1i7m-5j4k-3k1l2l1k1j1l",
|
||||||
request_validator=None, # TODO: Add request validator
|
request_validator=AuthenticationRequestModels.RefresherRequestModel, # TODO: Add request validator
|
||||||
language_models=[],
|
language_models=[],
|
||||||
# response_validator=None, # TODO: Add response validator
|
# response_validator=None,
|
||||||
description="Refresh authentication token",
|
description="Refresh authentication token",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ AuthenticationCheckTokenEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_check_token_is_valid():
|
def authentication_check_token_is_valid(request: Request):
|
||||||
context_retriever = ContextRetrievers(func=authentication_check_token_is_valid)
|
context_retriever = ContextRetrievers(func=authentication_check_token_is_valid)
|
||||||
function = AuthenticationCheckTokenEventMethods.retrieve_event(
|
function = AuthenticationCheckTokenEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_check_token_event.key}"
|
event_function_code=f"{authentication_check_token_event.key}"
|
||||||
|
|
@ -142,7 +142,7 @@ AuthenticationRefreshEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_refresh_user_info():
|
def authentication_refresh_user_info(request: Request):
|
||||||
context_retriever = ContextRetrievers(func=authentication_refresh_user_info)
|
context_retriever = ContextRetrievers(func=authentication_refresh_user_info)
|
||||||
function = AuthenticationRefreshEventMethods.retrieve_event(
|
function = AuthenticationRefreshEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_refresh_user_info_event.key}"
|
event_function_code=f"{authentication_refresh_user_info_event.key}"
|
||||||
|
|
@ -169,7 +169,7 @@ AuthenticationChangePasswordEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_change_password_event_callable(data: EndpointBaseRequestModel):
|
def authentication_change_password_event_callable(request: Request, data: EndpointBaseRequestModel):
|
||||||
context_retriever = ContextRetrievers(
|
context_retriever = ContextRetrievers(
|
||||||
func=authentication_change_password_event_callable
|
func=authentication_change_password_event_callable
|
||||||
)
|
)
|
||||||
|
|
@ -198,7 +198,7 @@ AuthenticationCreatePasswordEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_create_password(data: EndpointBaseRequestModel):
|
def authentication_create_password(request: Request, data: EndpointBaseRequestModel):
|
||||||
context_retriever = ContextRetrievers(func=authentication_create_password)
|
context_retriever = ContextRetrievers(func=authentication_create_password)
|
||||||
function = AuthenticationCreatePasswordEventMethods.retrieve_event(
|
function = AuthenticationCreatePasswordEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_create_password_event.key}"
|
event_function_code=f"{authentication_create_password_event.key}"
|
||||||
|
|
@ -227,13 +227,18 @@ AuthenticationDisconnectUserEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_disconnect_user(data: EndpointBaseRequestModel):
|
def authentication_disconnect_user(request: Request):
|
||||||
context_retriever = ContextRetrievers(func=authentication_disconnect_user)
|
context_retriever = ContextRetrievers(func=authentication_disconnect_user)
|
||||||
function = AuthenticationDisconnectUserEventMethods.retrieve_event(
|
function = AuthenticationDisconnectUserEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_disconnect_user_event.key}"
|
event_function_code=f"{authentication_disconnect_user_event.key}"
|
||||||
)
|
)
|
||||||
AuthenticationFunctions.context_retriever = context_retriever
|
AuthenticationFunctions.context_retriever = context_retriever
|
||||||
return function.endpoint_callable(data=data)
|
return function.endpoint_callable()
|
||||||
|
|
||||||
|
|
||||||
|
AuthenticationDisconnectUserEventMethods.endpoint_callable = (
|
||||||
|
authentication_disconnect_user
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
AuthenticationLogoutEventMethods = MethodToEvent(
|
AuthenticationLogoutEventMethods = MethodToEvent(
|
||||||
|
|
@ -249,13 +254,14 @@ AuthenticationLogoutEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_logout_user(data: EndpointBaseRequestModel):
|
def authentication_logout_user(request: Request, data: EndpointBaseRequestModel):
|
||||||
context_retriever = ContextRetrievers(func=authentication_logout_user)
|
context_retriever = ContextRetrievers(func=authentication_logout_user)
|
||||||
function = AuthenticationLogoutEventMethods.retrieve_event(
|
function = AuthenticationLogoutEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_logout_user_event.key}"
|
event_function_code=f"{authentication_logout_user_event.key}"
|
||||||
)
|
)
|
||||||
|
validated_data = function.REQUEST_VALIDATOR(**data.data)
|
||||||
AuthenticationFunctions.context_retriever = context_retriever
|
AuthenticationFunctions.context_retriever = context_retriever
|
||||||
return function.endpoint_callable(data=data)
|
return function.endpoint_callable(data=validated_data)
|
||||||
|
|
||||||
|
|
||||||
AuthenticationLogoutEventMethods.endpoint_callable = authentication_logout_user
|
AuthenticationLogoutEventMethods.endpoint_callable = authentication_logout_user
|
||||||
|
|
@ -268,7 +274,7 @@ AuthenticationRefreshTokenEventMethods = MethodToEvent(
|
||||||
},
|
},
|
||||||
headers=[],
|
headers=[],
|
||||||
errors=[],
|
errors=[],
|
||||||
decorators_list=[MiddlewareModule.auth_required],
|
decorators_list=[],
|
||||||
url="/refresh-token",
|
url="/refresh-token",
|
||||||
method="POST",
|
method="POST",
|
||||||
summary="Refresh token",
|
summary="Refresh token",
|
||||||
|
|
@ -276,13 +282,12 @@ AuthenticationRefreshTokenEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_refresher_token(data: EndpointBaseRequestModel):
|
def authentication_refresher_token(request: Request, data: EndpointBaseRequestModel):
|
||||||
context_retriever = ContextRetrievers(func=authentication_refresher_token)
|
|
||||||
function = AuthenticationRefreshTokenEventMethods.retrieve_event(
|
function = AuthenticationRefreshTokenEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_refresher_token_event.key}"
|
event_function_code=f"{authentication_refresher_token_event.key}"
|
||||||
)
|
)
|
||||||
AuthenticationFunctions.context_retriever = context_retriever
|
validated_data = function.REQUEST_VALIDATOR(**data.data)
|
||||||
return function.endpoint_callable(data=data)
|
return function.endpoint_callable(request=request, data=validated_data)
|
||||||
|
|
||||||
|
|
||||||
AuthenticationRefreshTokenEventMethods.endpoint_callable = (
|
AuthenticationRefreshTokenEventMethods.endpoint_callable = (
|
||||||
|
|
@ -304,7 +309,7 @@ AuthenticationForgotPasswordEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_forgot_password(data: EndpointBaseRequestModel):
|
def authentication_forgot_password(request: Request, data: EndpointBaseRequestModel):
|
||||||
context_retriever = ContextRetrievers(func=authentication_forgot_password)
|
context_retriever = ContextRetrievers(func=authentication_forgot_password)
|
||||||
function = AuthenticationForgotPasswordEventMethods.retrieve_event(
|
function = AuthenticationForgotPasswordEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_forgot_password_event.key}"
|
event_function_code=f"{authentication_forgot_password_event.key}"
|
||||||
|
|
@ -333,7 +338,7 @@ AuthenticationResetPasswordEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_reset_password(data: EndpointBaseRequestModel):
|
def authentication_reset_password(request: Request, data: EndpointBaseRequestModel):
|
||||||
context_retriever = ContextRetrievers(func=authentication_reset_password)
|
context_retriever = ContextRetrievers(func=authentication_reset_password)
|
||||||
function = AuthenticationResetPasswordEventMethods.retrieve_event(
|
function = AuthenticationResetPasswordEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_reset_password_event.key}"
|
event_function_code=f"{authentication_reset_password_event.key}"
|
||||||
|
|
@ -362,7 +367,7 @@ AuthenticationDownloadAvatarEventMethods = MethodToEvent(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authentication_download_avatar():
|
def authentication_download_avatar(request: Request):
|
||||||
context_retriever = ContextRetrievers(func=authentication_download_avatar)
|
context_retriever = ContextRetrievers(func=authentication_download_avatar)
|
||||||
function = AuthenticationDownloadAvatarEventMethods.retrieve_event(
|
function = AuthenticationDownloadAvatarEventMethods.retrieve_event(
|
||||||
event_function_code=f"{authentication_download_avatar_event.key}"
|
event_function_code=f"{authentication_download_avatar_event.key}"
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,9 @@ from ApiLayers.Schemas import (
|
||||||
Users,
|
Users,
|
||||||
UsersTokens,
|
UsersTokens,
|
||||||
)
|
)
|
||||||
from Events.base_request_model import ContextRetrievers, TokenDictType
|
from Events.base_request_model import TokenDictType, BaseRouteModel
|
||||||
|
from Services.Redis.Actions.actions import RedisActions
|
||||||
|
from ApiLayers.AllConfigs.Redis.configs import RedisAuthKeys
|
||||||
|
|
||||||
|
|
||||||
class Handlers:
|
class Handlers:
|
||||||
|
|
@ -211,11 +213,9 @@ class Handlers:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class AuthenticationFunctions:
|
class AuthenticationFunctions(BaseRouteModel):
|
||||||
"""Class for handling authentication functions"""
|
"""Class for handling authentication functions"""
|
||||||
|
|
||||||
context_retriever: Union[ContextRetrievers] = None
|
|
||||||
|
|
||||||
@classmethod # Requires no auth context
|
@classmethod # Requires no auth context
|
||||||
def authentication_login_with_domain_and_creds(cls, request: Request, data: Any):
|
def authentication_login_with_domain_and_creds(cls, request: Request, data: Any):
|
||||||
"""
|
"""
|
||||||
|
|
@ -292,13 +292,13 @@ class AuthenticationFunctions:
|
||||||
return EndpointSuccessResponse(
|
return EndpointSuccessResponse(
|
||||||
code="USER_INFO_REFRESHED", lang=cls.context_retriever.token.lang
|
code="USER_INFO_REFRESHED", lang=cls.context_retriever.token.lang
|
||||||
).as_dict({
|
).as_dict({
|
||||||
"access_token": cls.context_retriever.token, "user": found_user.get_dict(),
|
"access_token": cls.context_retriever.get_token, "user": found_user.get_dict(),
|
||||||
})
|
})
|
||||||
if not found_user:
|
if not found_user:
|
||||||
return EndpointNotAcceptableResponse(
|
return EndpointNotAcceptableResponse(
|
||||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(
|
).as_dict(
|
||||||
data={"user": found_user.get_dict()}
|
data={}
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod # Requires no auth context
|
@classmethod # Requires no auth context
|
||||||
|
|
@ -315,7 +315,7 @@ class AuthenticationFunctions:
|
||||||
return EndpointNotAcceptableResponse(
|
return EndpointNotAcceptableResponse(
|
||||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(
|
).as_dict(
|
||||||
data={"user": found_user.get_dict()}
|
data={}
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod # Requires not auth context
|
@classmethod # Requires not auth context
|
||||||
|
|
@ -349,14 +349,18 @@ class AuthenticationFunctions:
|
||||||
return EndpointNotAcceptableResponse(
|
return EndpointNotAcceptableResponse(
|
||||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(
|
).as_dict(
|
||||||
data={"user": found_user.get_dict()}
|
data={}
|
||||||
)
|
)
|
||||||
registered_tokens = UsersTokens.filter_all(
|
registered_tokens = UsersTokens.filter_all(
|
||||||
UsersTokens.user_id == cls.context_retriever.token.id, db=db
|
UsersTokens.user_id == cls.context_retriever.token.user_id, db=db
|
||||||
)
|
)
|
||||||
if registered_tokens.count:
|
if registered_tokens.count:
|
||||||
registered_tokens.query.delete()
|
registered_tokens.query.delete()
|
||||||
UsersTokens.save(db=db)
|
UsersTokens.save(db=db)
|
||||||
|
|
||||||
|
RedisActions.delete(
|
||||||
|
list_keys=[f"{RedisAuthKeys.AUTH}:*:{str(found_user.uu_id)}"]
|
||||||
|
)
|
||||||
return EndpointSuccessResponse(
|
return EndpointSuccessResponse(
|
||||||
code="DISCONNECTED_USER", lang=cls.context_retriever.token.lang
|
code="DISCONNECTED_USER", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(data={"user": found_user.get_dict()})
|
).as_dict(data={"user": found_user.get_dict()})
|
||||||
|
|
@ -372,23 +376,32 @@ class AuthenticationFunctions:
|
||||||
return EndpointNotAcceptableResponse(
|
return EndpointNotAcceptableResponse(
|
||||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(
|
).as_dict(
|
||||||
data={"user": found_user.get_dict()}
|
data={}
|
||||||
)
|
)
|
||||||
registered_tokens = UsersTokens.filter_all_system(
|
registered_tokens = UsersTokens.filter_all_system(
|
||||||
UsersTokens.user_id == cls.context_retriever.token.id,
|
UsersTokens.user_id == cls.context_retriever.token.user_id,
|
||||||
UsersTokens.domain == cls.context_retriever.token.domain,
|
UsersTokens.domain == cls.context_retriever.token.domain,
|
||||||
db=db,
|
db=db,
|
||||||
)
|
)
|
||||||
if registered_tokens.count:
|
if registered_tokens.count:
|
||||||
registered_tokens.query.delete()
|
registered_tokens.query.delete()
|
||||||
UsersTokens.save(db=db)
|
UsersTokens.save(db=db)
|
||||||
|
TokenService.remove_token_with_domain(user=found_user, domain=data.domain)
|
||||||
return EndpointSuccessResponse(
|
return EndpointSuccessResponse(
|
||||||
code="DISCONNECTED_USER", lang=cls.context_retriever.token.lang
|
code="LOGOUT_USER", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(data={"user": found_user.get_dict()})
|
).as_dict(data={"user": found_user.get_dict()})
|
||||||
|
|
||||||
@classmethod # Requires not auth context
|
@classmethod # Requires not auth context
|
||||||
def authentication_refresher_token(cls, data: Any):
|
def authentication_refresher_token(cls, request: Request, data: Any):
|
||||||
"""Refresh access token with refresher token"""
|
"""
|
||||||
|
Refresh access token with refresher token
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"refresh_token": "string",
|
||||||
|
"domain": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
import arrow
|
import arrow
|
||||||
from ApiLayers.ApiServices.Token.token_handler import TokenService
|
from ApiLayers.ApiServices.Token.token_handler import TokenService
|
||||||
db = UsersTokens.new_session()
|
db = UsersTokens.new_session()
|
||||||
|
|
@ -397,15 +410,15 @@ class AuthenticationFunctions:
|
||||||
domain=data.domain,
|
domain=data.domain,
|
||||||
db=db,
|
db=db,
|
||||||
).data
|
).data
|
||||||
|
language = request.headers.get("evyos-language", "tr")
|
||||||
if not token_refresher:
|
if not token_refresher:
|
||||||
return EndpointNotAcceptableResponse(
|
return EndpointNotAcceptableResponse(
|
||||||
code="REFRESHER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
code="REFRESHER_NOT_FOUND", lang=language
|
||||||
).as_dict(
|
).as_dict(
|
||||||
data={"refresh_token": data.refresh_token}
|
data={"refresh_token": data.refresh_token}
|
||||||
)
|
)
|
||||||
|
|
||||||
if found_user := Users.filter_one(Users.id == token_refresher.user_id, db=db).data:
|
if found_user := Users.filter_one(Users.id == token_refresher.user_id, db=db).data:
|
||||||
request = cls.context_retriever.request
|
|
||||||
token_created = TokenService.set_access_token_to_redis(
|
token_created = TokenService.set_access_token_to_redis(
|
||||||
request=request,
|
request=request,
|
||||||
user=found_user,
|
user=found_user,
|
||||||
|
|
@ -420,9 +433,12 @@ class AuthenticationFunctions:
|
||||||
"access_token": token_created.get("access_token"),
|
"access_token": token_created.get("access_token"),
|
||||||
"refresh_token": data.refresh_token,
|
"refresh_token": data.refresh_token,
|
||||||
}
|
}
|
||||||
return EndpointSuccessResponse(
|
return EndpointSuccessResponse(code="TOKEN_REFRESH", lang=language).as_dict(data=response_data)
|
||||||
code="TOKEN_REFRESH", lang=cls.context_retriever.token.lang
|
raise EndpointNotAcceptableResponse(
|
||||||
).as_dict(data=response_data)
|
code="USER_NOT_FOUND", lang=language
|
||||||
|
).as_dict(
|
||||||
|
data={}
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod # Requires not auth context
|
@classmethod # Requires not auth context
|
||||||
def authentication_forgot_password(cls, data: Any):
|
def authentication_forgot_password(cls, data: Any):
|
||||||
|
|
@ -463,7 +479,7 @@ class AuthenticationFunctions:
|
||||||
return cls.context_retriever.base
|
return cls.context_retriever.base
|
||||||
|
|
||||||
@classmethod # Requires not auth context
|
@classmethod # Requires not auth context
|
||||||
def authentication_download_avatar(cls, data: Any):
|
def authentication_download_avatar(cls):
|
||||||
"""Download avatar icon and profile info of user"""
|
"""Download avatar icon and profile info of user"""
|
||||||
import arrow
|
import arrow
|
||||||
db = Users.new_session()
|
db = Users.new_session()
|
||||||
|
|
@ -471,19 +487,19 @@ class AuthenticationFunctions:
|
||||||
Users.id == cls.context_retriever.token.user_id, db=db
|
Users.id == cls.context_retriever.token.user_id, db=db
|
||||||
).data:
|
).data:
|
||||||
expired_starts = str(arrow.now() - arrow.get(str(found_user.expiry_ends)))
|
expired_starts = str(arrow.now() - arrow.get(str(found_user.expiry_ends)))
|
||||||
expired_int = arrow.now() - arrow.get(str(found_user.expiry_ends)).days
|
expired_int = arrow.now().datetime - arrow.get(str(found_user.expiry_ends)).datetime
|
||||||
user_info = {
|
user_info = {
|
||||||
"lang": cls.context_retriever.token.lang,
|
"lang": cls.context_retriever.token.lang,
|
||||||
"full_name": found_user.person.full_name,
|
"full_name": found_user.person.full_name,
|
||||||
"avatar": found_user.avatar,
|
"avatar": found_user.avatar,
|
||||||
"remember_me": found_user.remember_me,
|
"remember_me": found_user.remember_me,
|
||||||
"expiry_ends": str(found_user.expiry_ends),
|
"expiry_ends": str(found_user.expiry_ends),
|
||||||
"expired_str": expired_starts,
|
"expired_humanized": expired_starts,
|
||||||
"expired_int": int(expired_int),
|
"expired_day": int(expired_int.days) * -1,
|
||||||
}
|
}
|
||||||
return EndpointSuccessResponse(
|
return EndpointSuccessResponse(
|
||||||
code="USER_AVATAR", lang=cls.context_retriever.token.lang
|
code="USER_AVATAR", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(data=user_info)
|
).as_dict(data=user_info)
|
||||||
return EndpointNotAcceptableResponse(
|
return EndpointNotAcceptableResponse(
|
||||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||||
).as_dict(data={"user": found_user.get_dict()})
|
).as_dict(data={})
|
||||||
|
|
|
||||||
|
|
@ -7,5 +7,5 @@ authentication_page_info = PageInfo(
|
||||||
description={"en": "Authentication"},
|
description={"en": "Authentication"},
|
||||||
icon="",
|
icon="",
|
||||||
parent="",
|
parent="",
|
||||||
url="",
|
url=""
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ from ApiLayers.ApiValidations.Request import (
|
||||||
Login,
|
Login,
|
||||||
EmployeeSelection,
|
EmployeeSelection,
|
||||||
OccupantSelection,
|
OccupantSelection,
|
||||||
|
Logout,
|
||||||
CreatePassword,
|
CreatePassword,
|
||||||
ChangePassword,
|
ChangePassword,
|
||||||
Forgot,
|
Forgot,
|
||||||
|
Remember,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -13,6 +15,8 @@ class AuthenticationRequestModels:
|
||||||
SelectCompanyOrOccupantTypeSuperUserRequestModel = {
|
SelectCompanyOrOccupantTypeSuperUserRequestModel = {
|
||||||
"EmployeeSelection": EmployeeSelection, "OccupantSelection": OccupantSelection,
|
"EmployeeSelection": EmployeeSelection, "OccupantSelection": OccupantSelection,
|
||||||
}
|
}
|
||||||
|
RefresherRequestModel = Remember
|
||||||
|
LogoutRequestModel = Logout
|
||||||
|
|
||||||
|
|
||||||
class AuthenticationResponseModels:
|
class AuthenticationResponseModels:
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ class PageInfo:
|
||||||
BUTTON_NAME: str
|
BUTTON_NAME: str
|
||||||
PAGE_URL: str
|
PAGE_URL: str
|
||||||
PAGEINFO: Dict[str, Any]
|
PAGEINFO: Dict[str, Any]
|
||||||
|
URL: str = ""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -18,13 +19,15 @@ class PageInfo:
|
||||||
title: Dict[str, Any],
|
title: Dict[str, Any],
|
||||||
description: Dict[str, Any],
|
description: Dict[str, Any],
|
||||||
icon: str,
|
icon: str,
|
||||||
parent: str
|
parent: str,
|
||||||
|
url: str,
|
||||||
):
|
):
|
||||||
self.NAME = name
|
self.NAME = name
|
||||||
self.TITLE = title
|
self.TITLE = title
|
||||||
self.DESCRIPTION = description
|
self.DESCRIPTION = description
|
||||||
self.ICON = icon
|
self.ICON = icon
|
||||||
self.PARENT = parent
|
self.PARENT = parent
|
||||||
|
self.URL = url
|
||||||
|
|
||||||
|
|
||||||
class Event:
|
class Event:
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,10 @@ from ApiLayers.ApiValidations.Custom.token_objects import (
|
||||||
OccupantTokenObject,
|
OccupantTokenObject,
|
||||||
)
|
)
|
||||||
from ApiLayers.ApiValidations.Custom.wrapper_contexts import AuthContext, EventContext
|
from ApiLayers.ApiValidations.Custom.wrapper_contexts import AuthContext, EventContext
|
||||||
|
from ApiLayers.AllConfigs.Token.config import Auth
|
||||||
|
|
||||||
|
|
||||||
TokenDictType = Union[
|
TokenDictType = Union[EmployeeTokenObject, OccupantTokenObject]
|
||||||
EmployeeTokenObject, OccupantTokenObject
|
|
||||||
] # Type aliases for common types
|
|
||||||
|
|
||||||
|
|
||||||
class EndpointBaseRequestModel(BaseModel):
|
class EndpointBaseRequestModel(BaseModel):
|
||||||
|
|
@ -81,3 +80,13 @@ class ContextRetrievers:
|
||||||
def base(self) -> Optional[dict[str, Any]]:
|
def base(self) -> Optional[dict[str, Any]]:
|
||||||
"""Retrieve base request model from a function."""
|
"""Retrieve base request model from a function."""
|
||||||
return getattr(self.context, "base", None)
|
return getattr(self.context, "base", None)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_token(self) -> Optional[str]:
|
||||||
|
"""Retrieve access key from a function."""
|
||||||
|
return getattr(self.request, "headers", {}).get(Auth.ACCESS_TOKEN_TAG, None)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseRouteModel:
|
||||||
|
|
||||||
|
context_retriever: Union[ContextRetrievers] = None
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class AccessToken(BaseRedisModel):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def count(self):
|
def count(self):
|
||||||
return 2
|
return 3
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def delimiter(self):
|
def delimiter(self):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue