|
|
|
|
@@ -288,25 +288,30 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
"""Refresh user info using access token"""
|
|
|
|
|
if cls.context_retriever.token:
|
|
|
|
|
db = Users.new_session()
|
|
|
|
|
if found_user := Users.filter_one(Users.id == cls.context_retriever.token.user_id, db=db).data:
|
|
|
|
|
if found_user := Users.filter_one(
|
|
|
|
|
Users.id == cls.context_retriever.token.user_id, db=db
|
|
|
|
|
).data:
|
|
|
|
|
return EndpointSuccessResponse(
|
|
|
|
|
code="USER_INFO_REFRESHED", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict({
|
|
|
|
|
"access_token": cls.context_retriever.get_token, "user": found_user.get_dict(),
|
|
|
|
|
})
|
|
|
|
|
).as_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={}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={})
|
|
|
|
|
|
|
|
|
|
@classmethod # Requires no auth context
|
|
|
|
|
def authentication_change_password(cls, data: Any):
|
|
|
|
|
"""Change password with access token"""
|
|
|
|
|
if cls.context_retriever.token:
|
|
|
|
|
db = Users.new_session()
|
|
|
|
|
if found_user := Users.filter_one(Users.id == cls.context_retriever.token.user_id, db=db).data:
|
|
|
|
|
if found_user := Users.filter_one(
|
|
|
|
|
Users.id == cls.context_retriever.token.user_id, db=db
|
|
|
|
|
).data:
|
|
|
|
|
found_user.set_password(data.new_password)
|
|
|
|
|
return EndpointSuccessResponse(
|
|
|
|
|
code="PASSWORD_CHANGED", lang=cls.context_retriever.token.lang
|
|
|
|
|
@@ -314,9 +319,7 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
if not found_user:
|
|
|
|
|
return EndpointNotAcceptableResponse(
|
|
|
|
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={})
|
|
|
|
|
|
|
|
|
|
@classmethod # Requires not auth context
|
|
|
|
|
def authentication_create_password(cls, data: Any):
|
|
|
|
|
@@ -325,11 +328,9 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
if not data.re_password == data.password:
|
|
|
|
|
return EndpointNotAcceptableResponse(
|
|
|
|
|
code="PASSWORD_NOT_MATCH", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={"password": data.password, "re_password": data.re_password}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={"password": data.password, "re_password": data.re_password})
|
|
|
|
|
if found_user := Users.filter_one(
|
|
|
|
|
Users.password_token == data.password_token, db=db
|
|
|
|
|
Users.password_token == data.password_token, db=db
|
|
|
|
|
).data:
|
|
|
|
|
found_user.create_password(found_user=found_user, password=data.password)
|
|
|
|
|
found_user.password_token = ""
|
|
|
|
|
@@ -346,11 +347,9 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
Users.id == cls.context_retriever.token.user_id, db=db
|
|
|
|
|
).data
|
|
|
|
|
if not found_user:
|
|
|
|
|
return EndpointNotAcceptableResponse(
|
|
|
|
|
return EndpointNotAcceptableResponse(
|
|
|
|
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={})
|
|
|
|
|
registered_tokens = UsersTokens.filter_all(
|
|
|
|
|
UsersTokens.user_id == cls.context_retriever.token.user_id, db=db
|
|
|
|
|
)
|
|
|
|
|
@@ -375,9 +374,7 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
if not found_user:
|
|
|
|
|
return EndpointNotAcceptableResponse(
|
|
|
|
|
code="USER_NOT_FOUND", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={})
|
|
|
|
|
registered_tokens = UsersTokens.filter_all_system(
|
|
|
|
|
UsersTokens.user_id == cls.context_retriever.token.user_id,
|
|
|
|
|
UsersTokens.domain == cls.context_retriever.token.domain,
|
|
|
|
|
@@ -404,6 +401,7 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
"""
|
|
|
|
|
import arrow
|
|
|
|
|
from ApiLayers.ApiServices.Token.token_handler import TokenService
|
|
|
|
|
|
|
|
|
|
db = UsersTokens.new_session()
|
|
|
|
|
token_refresher: UsersTokens = UsersTokens.filter_by_one(
|
|
|
|
|
token=data.refresh_token,
|
|
|
|
|
@@ -414,11 +412,11 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
if not token_refresher:
|
|
|
|
|
return EndpointNotAcceptableResponse(
|
|
|
|
|
code="REFRESHER_NOT_FOUND", lang=language
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={"refresh_token": data.refresh_token}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(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:
|
|
|
|
|
token_created = TokenService.set_access_token_to_redis(
|
|
|
|
|
request=request,
|
|
|
|
|
user=found_user,
|
|
|
|
|
@@ -427,51 +425,63 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
)
|
|
|
|
|
found_user.last_agent = request.headers.get("User-Agent", None)
|
|
|
|
|
found_user.last_platform = request.headers.get("Origin", None)
|
|
|
|
|
found_user.last_remote_addr = getattr(request, "remote_addr", None) or request.headers.get("X-Forwarded-For", None)
|
|
|
|
|
found_user.last_remote_addr = getattr(
|
|
|
|
|
request, "remote_addr", None
|
|
|
|
|
) or request.headers.get("X-Forwarded-For", None)
|
|
|
|
|
found_user.last_seen = str(arrow.now())
|
|
|
|
|
response_data = {
|
|
|
|
|
"access_token": token_created.get("access_token"),
|
|
|
|
|
"refresh_token": data.refresh_token,
|
|
|
|
|
}
|
|
|
|
|
return EndpointSuccessResponse(code="TOKEN_REFRESH", lang=language).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={}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={})
|
|
|
|
|
|
|
|
|
|
@classmethod # Requires not auth context
|
|
|
|
|
def authentication_forgot_password(cls, data: Any):
|
|
|
|
|
"""Send an email to user for a valid password reset token"""
|
|
|
|
|
import arrow
|
|
|
|
|
from ApiLayers.ApiServices.Token.token_handler import TokenService
|
|
|
|
|
from ApiLayers.AllConfigs.Templates.password_templates import change_your_password_template
|
|
|
|
|
from ApiLayers.AllConfigs.Templates.password_templates import (
|
|
|
|
|
change_your_password_template,
|
|
|
|
|
)
|
|
|
|
|
from Services.Email.send_email import email_sender
|
|
|
|
|
|
|
|
|
|
from config import ApiStatic
|
|
|
|
|
|
|
|
|
|
db = Users.new_session()
|
|
|
|
|
request = cls.context_retriever.request
|
|
|
|
|
found_user: Users = Users.check_user_exits(access_key=data.access_key, domain=data.domain)
|
|
|
|
|
found_user: Users = Users.check_user_exits(
|
|
|
|
|
access_key=data.access_key, domain=data.domain
|
|
|
|
|
)
|
|
|
|
|
forgot_key = TokenService._create_access_token(access=False)
|
|
|
|
|
forgot_link = ApiStatic.forgot_link(forgot_key=forgot_key)
|
|
|
|
|
send_email_completed = email_sender.send_email(
|
|
|
|
|
subject=f"Dear {found_user.user_tag}, your forgot password link has been sent.",
|
|
|
|
|
receivers=[str(found_user.email)],
|
|
|
|
|
html=change_your_password_template(user_name=found_user.user_tag, forgot_link=forgot_link),
|
|
|
|
|
html=change_your_password_template(
|
|
|
|
|
user_name=found_user.user_tag, forgot_link=forgot_link
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
if not send_email_completed:
|
|
|
|
|
return EndpointBadRequestResponse(
|
|
|
|
|
code="EMAIL_NOT_SENT", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={"email": found_user.email}
|
|
|
|
|
)
|
|
|
|
|
).as_dict(data={"email": found_user.email})
|
|
|
|
|
found_user.password_token = forgot_key
|
|
|
|
|
found_user.password_token_is_valid = str(arrow.now().shift(days=1))
|
|
|
|
|
found_user.save(db=db)
|
|
|
|
|
return EndpointSuccessResponse(
|
|
|
|
|
code="FORGOT_PASSWORD", lang=cls.context_retriever.token.lang
|
|
|
|
|
).as_dict(data={"user": found_user.get_dict(), "forgot_link": forgot_link, "token": forgot_key})
|
|
|
|
|
).as_dict(
|
|
|
|
|
data={
|
|
|
|
|
"user": found_user.get_dict(),
|
|
|
|
|
"forgot_link": forgot_link,
|
|
|
|
|
"token": forgot_key,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod # Requires not auth context
|
|
|
|
|
def authentication_reset_password(cls, data: Any):
|
|
|
|
|
@@ -482,12 +492,15 @@ class AuthenticationFunctions(BaseRouteModel):
|
|
|
|
|
def authentication_download_avatar(cls):
|
|
|
|
|
"""Download avatar icon and profile info of user"""
|
|
|
|
|
import arrow
|
|
|
|
|
|
|
|
|
|
db = Users.new_session()
|
|
|
|
|
if found_user := Users.filter_one(
|
|
|
|
|
Users.id == cls.context_retriever.token.user_id, db=db
|
|
|
|
|
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().datetime - arrow.get(str(found_user.expiry_ends)).datetime
|
|
|
|
|
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,
|
|
|
|
|
|