auth updated routes tested & password is not yet tested
This commit is contained in:
@@ -27,7 +27,9 @@ from ApiLayers.Schemas import (
|
||||
Users,
|
||||
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:
|
||||
@@ -211,11 +213,9 @@ class Handlers:
|
||||
)
|
||||
|
||||
|
||||
class AuthenticationFunctions:
|
||||
class AuthenticationFunctions(BaseRouteModel):
|
||||
"""Class for handling authentication functions"""
|
||||
|
||||
context_retriever: Union[ContextRetrievers] = None
|
||||
|
||||
@classmethod # Requires no auth context
|
||||
def authentication_login_with_domain_and_creds(cls, request: Request, data: Any):
|
||||
"""
|
||||
@@ -292,13 +292,13 @@ class AuthenticationFunctions:
|
||||
return EndpointSuccessResponse(
|
||||
code="USER_INFO_REFRESHED", lang=cls.context_retriever.token.lang
|
||||
).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:
|
||||
return EndpointNotAcceptableResponse(
|
||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||
).as_dict(
|
||||
data={"user": found_user.get_dict()}
|
||||
data={}
|
||||
)
|
||||
|
||||
@classmethod # Requires no auth context
|
||||
@@ -315,7 +315,7 @@ class AuthenticationFunctions:
|
||||
return EndpointNotAcceptableResponse(
|
||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||
).as_dict(
|
||||
data={"user": found_user.get_dict()}
|
||||
data={}
|
||||
)
|
||||
|
||||
@classmethod # Requires not auth context
|
||||
@@ -349,14 +349,18 @@ class AuthenticationFunctions:
|
||||
return EndpointNotAcceptableResponse(
|
||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||
).as_dict(
|
||||
data={"user": found_user.get_dict()}
|
||||
data={}
|
||||
)
|
||||
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:
|
||||
registered_tokens.query.delete()
|
||||
UsersTokens.save(db=db)
|
||||
|
||||
RedisActions.delete(
|
||||
list_keys=[f"{RedisAuthKeys.AUTH}:*:{str(found_user.uu_id)}"]
|
||||
)
|
||||
return EndpointSuccessResponse(
|
||||
code="DISCONNECTED_USER", lang=cls.context_retriever.token.lang
|
||||
).as_dict(data={"user": found_user.get_dict()})
|
||||
@@ -372,23 +376,32 @@ class AuthenticationFunctions:
|
||||
return EndpointNotAcceptableResponse(
|
||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||
).as_dict(
|
||||
data={"user": found_user.get_dict()}
|
||||
data={}
|
||||
)
|
||||
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,
|
||||
db=db,
|
||||
)
|
||||
if registered_tokens.count:
|
||||
registered_tokens.query.delete()
|
||||
UsersTokens.save(db=db)
|
||||
TokenService.remove_token_with_domain(user=found_user, domain=data.domain)
|
||||
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()})
|
||||
|
||||
@classmethod # Requires not auth context
|
||||
def authentication_refresher_token(cls, data: Any):
|
||||
"""Refresh access token with refresher token"""
|
||||
def authentication_refresher_token(cls, request: Request, data: Any):
|
||||
"""
|
||||
Refresh access token with refresher token
|
||||
{
|
||||
"data": {
|
||||
"refresh_token": "string",
|
||||
"domain": "string"
|
||||
}
|
||||
}
|
||||
"""
|
||||
import arrow
|
||||
from ApiLayers.ApiServices.Token.token_handler import TokenService
|
||||
db = UsersTokens.new_session()
|
||||
@@ -397,15 +410,15 @@ class AuthenticationFunctions:
|
||||
domain=data.domain,
|
||||
db=db,
|
||||
).data
|
||||
language = request.headers.get("evyos-language", "tr")
|
||||
if not token_refresher:
|
||||
return EndpointNotAcceptableResponse(
|
||||
code="REFRESHER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||
code="REFRESHER_NOT_FOUND", lang=language
|
||||
).as_dict(
|
||||
data={"refresh_token": data.refresh_token}
|
||||
)
|
||||
|
||||
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(
|
||||
request=request,
|
||||
user=found_user,
|
||||
@@ -420,9 +433,12 @@ class AuthenticationFunctions:
|
||||
"access_token": token_created.get("access_token"),
|
||||
"refresh_token": data.refresh_token,
|
||||
}
|
||||
return EndpointSuccessResponse(
|
||||
code="TOKEN_REFRESH", lang=cls.context_retriever.token.lang
|
||||
).as_dict(data=response_data)
|
||||
return EndpointSuccessResponse(code="TOKEN_REFRESH", lang=language).as_dict(data=response_data)
|
||||
raise EndpointNotAcceptableResponse(
|
||||
code="USER_NOT_FOUND", lang=language
|
||||
).as_dict(
|
||||
data={}
|
||||
)
|
||||
|
||||
@classmethod # Requires not auth context
|
||||
def authentication_forgot_password(cls, data: Any):
|
||||
@@ -463,7 +479,7 @@ class AuthenticationFunctions:
|
||||
return cls.context_retriever.base
|
||||
|
||||
@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"""
|
||||
import arrow
|
||||
db = Users.new_session()
|
||||
@@ -471,19 +487,19 @@ class AuthenticationFunctions:
|
||||
Users.id == cls.context_retriever.token.user_id, db=db
|
||||
).data:
|
||||
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 = {
|
||||
"lang": cls.context_retriever.token.lang,
|
||||
"full_name": found_user.person.full_name,
|
||||
"avatar": found_user.avatar,
|
||||
"remember_me": found_user.remember_me,
|
||||
"expiry_ends": str(found_user.expiry_ends),
|
||||
"expired_str": expired_starts,
|
||||
"expired_int": int(expired_int),
|
||||
"expired_humanized": expired_starts,
|
||||
"expired_day": int(expired_int.days) * -1,
|
||||
}
|
||||
return EndpointSuccessResponse(
|
||||
code="USER_AVATAR", lang=cls.context_retriever.token.lang
|
||||
).as_dict(data=user_info)
|
||||
return EndpointNotAcceptableResponse(
|
||||
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
||||
).as_dict(data={"user": found_user.get_dict()})
|
||||
).as_dict(data={})
|
||||
|
||||
Reference in New Issue
Block a user