(20250123) Listing permitted from disabled accounts, new activity not allowed.

This commit is contained in:
2025-01-23 12:54:52 +05:30
parent fe48b79c09
commit 44ff7719f3
14 changed files with 66 additions and 54 deletions
+1 -4
View File
@@ -153,10 +153,7 @@ async def invoke_llm(
# If the session token is invalid/expired: # If the session token is invalid/expired:
if kwargs.get("session_info") is None: if kwargs.get("session_info") is None:
return ResponseModel( return constants.API_RESPONSE_UNAUTHORIZED
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED
)
# ┳ ┓ # ┳ ┓
# ┃┏┓┓┏┏┓┃┏┏┓ # ┃┏┓┓┏┏┓┃┏┏┓
+1 -4
View File
@@ -158,10 +158,7 @@ async def disable_auth_token(
# If the session token is invalid/expired: # If the session token is invalid/expired:
if kwargs.get("session_info") is None: if kwargs.get("session_info") is None:
return ResponseModel( return constants.API_RESPONSE_UNAUTHORIZED
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED
)
# Get the user's info: # Get the user's info:
user_info = CoreUserInfoModel(**kwargs.get("session_info")) user_info = CoreUserInfoModel(**kwargs.get("session_info"))
@@ -169,8 +169,10 @@ async def payment_list(
# Get the token ids from the token keys: # Get the token ids from the token keys:
auth_tokens = await current_app.payments_controller.get_tokens_from_keys( auth_tokens = await current_app.payments_controller.get_tokens_from_keys(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_keys = inbound_data.tokenKeys token_keys = inbound_data.tokenKeys,
must_be_active = False
) )
if not auth_tokens: return constants.API_RESPONSE_NO_AUTH_TOKEN
token_ids = [t.authTokenId for t in auth_tokens] token_ids = [t.authTokenId for t in auth_tokens]
# Build the additional filter: # Build the additional filter:
@@ -164,13 +164,10 @@ async def request_payment(
# Get the token from the token key: # Get the token from the token key:
auth_token = await current_app.payments_controller.get_token_from_key( auth_token = await current_app.payments_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey token_key = inbound_data.tokenKey,
) must_be_active = True
if auth_token is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"No such token key."
) )
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# Get the user's info: # Get the user's info:
user_info = CoreUserInfoModel(**kwargs["session_info"]) user_info = CoreUserInfoModel(**kwargs["session_info"])
@@ -167,13 +167,10 @@ async def request_oauth_authorization_url(
# Get the auth-token: # Get the auth-token:
auth_token = await current_app.trading_controller.get_token_from_key( auth_token = await current_app.trading_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey token_key = inbound_data.tokenKey,
) must_be_active = True
if auth_token is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"No such token key."
) )
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# Start by assuming failure: # Start by assuming failure:
symbol_list = TradingSymbolListBrokerResponse() symbol_list = TradingSymbolListBrokerResponse()
+3 -1
View File
@@ -171,8 +171,10 @@ async def list_chat_messages(
auth_tokens = await current_app.chat_controller.get_tokens_from_keys( auth_tokens = await current_app.chat_controller.get_tokens_from_keys(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_keys = inbound_data.tokenKeys, token_keys = inbound_data.tokenKeys,
limit = len(inbound_data.tokenKeys) limit = len(inbound_data.tokenKeys),
must_be_active = False
) )
if not auth_tokens: return constants.API_RESPONSE_NO_AUTH_TOKEN
token_ids = [ObjectId(t.authTokenId) for t in auth_tokens] token_ids = [ObjectId(t.authTokenId) for t in auth_tokens]
# Check if these tokens belong to the user claiming ownership: # Check if these tokens belong to the user claiming ownership:
+3 -6
View File
@@ -217,13 +217,10 @@ async def send_chat_messages_api(
# Get the token from the token key: # Get the token from the token key:
auth_token = await current_app.chat_controller.get_token_from_key( auth_token = await current_app.chat_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey token_key = inbound_data.tokenKey,
) must_be_active = True
if auth_token is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"No such token key."
) )
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# ┏┓ ┓ ┓ ┳┳┓ # ┏┓ ┓ ┓ ┳┳┓
# ┗┓┏┓┏┓┏┫ ╋┣┓┏┓ ┃┃┃┏┓┏┏┏┓┏┓┏┓ # ┗┓┏┓┏┓┏┫ ╋┣┓┏┓ ┃┃┃┏┓┏┏┏┓┏┓┏┓
@@ -182,8 +182,10 @@ async def list_mails(
# Get the token ids from the token keys: # Get the token ids from the token keys:
auth_tokens = await current_app.mail_controller.get_tokens_from_keys( auth_tokens = await current_app.mail_controller.get_tokens_from_keys(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_keys = inbound_data.tokenKeys token_keys = inbound_data.tokenKeys,
must_be_active = False
) )
if not auth_tokens: return constants.API_RESPONSE_NO_AUTH_TOKEN
token_ids = [t.authTokenId for t in auth_tokens] token_ids = [t.authTokenId for t in auth_tokens]
# Build the additional filter: # Build the additional filter:
+5 -11
View File
@@ -222,11 +222,7 @@ async def send_one_mail(
if ( if (
kwargs.get("session_info") is None and kwargs.get("session_info") is None and
inbound_headers["Remote-IP"] not in current_app.whitelisted_ips inbound_headers["Remote-IP"] not in current_app.whitelisted_ips
): ): return constants.API_RESPONSE_UNAUTHORIZED
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED
)
# ┏┓ ┓ • ┏┓┓ ┓ # ┏┓ ┓ • ┏┓┓ ┓
# ┃┃┓┏┏┏┓┏┓┏┓┏┣┓┓┏┓ ┃ ┣┓┏┓┏┃┏ # ┃┃┓┏┏┏┓┏┓┏┓┏┣┓┓┏┓ ┃ ┣┓┏┓┏┃┏
@@ -236,8 +232,10 @@ async def send_one_mail(
# Get the token based on the key: # Get the token based on the key:
auth_token = await current_app.mail_controller.get_token_from_key( auth_token = await current_app.mail_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey token_key = inbound_data.tokenKey,
must_be_active = True
) )
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# Get the user's info: # Get the user's info:
if inbound_headers["Remote-IP"] not in current_app.whitelisted_ips: if inbound_headers["Remote-IP"] not in current_app.whitelisted_ips:
@@ -249,11 +247,7 @@ async def send_one_mail(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
user_info = user_info, user_info = user_info,
token_ids = [auth_token.authTokenId] token_ids = [auth_token.authTokenId]
): return ResponseModel( ): return constants.API_RESPONSE_UNAUTHORIZED
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = "The account does not belong to this user."
)
# ┏┓ ┓ ┳┳┓ •┓ # ┏┓ ┓ ┳┳┓ •┓
# ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┓┃ # ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┓┃
@@ -154,7 +154,9 @@ async def sync_mails(
auth_token = await current_app.mail_controller.get_token_from_key( auth_token = await current_app.mail_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey, token_key = inbound_data.tokenKey,
must_be_active = True
) )
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# Figure out the client connector: # Figure out the client connector:
match auth_token.client: match auth_token.client:
+5 -6
View File
@@ -157,10 +157,7 @@ async def list_sms_messages(
# If the session token is invalid/expired: # If the session token is invalid/expired:
if kwargs.get("session_info") is None: if kwargs.get("session_info") is None:
return ResponseModel( return constants.API_RESPONSE_UNAUTHORIZED
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED
)
# ┏┓ ┓ • ┏┓┓ ┓ # ┏┓ ┓ • ┏┓┓ ┓
# ┃┃┓┏┏┏┓┏┓┏┓┏┣┓┓┏┓ ┃ ┣┓┏┓┏┃┏ # ┃┃┓┏┏┏┓┏┓┏┓┏┣┓┓┏┓ ┃ ┣┓┏┓┏┃┏
@@ -171,13 +168,15 @@ async def list_sms_messages(
auth_tokens = await current_app.sms_controller.get_tokens_from_keys( auth_tokens = await current_app.sms_controller.get_tokens_from_keys(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_keys = inbound_data.tokenKeys, token_keys = inbound_data.tokenKeys,
limit = len(inbound_data.tokenKeys) limit = len(inbound_data.tokenKeys),
must_be_active = False
) )
if not auth_tokens: return constants.API_RESPONSE_NO_AUTH_TOKEN
token_ids = [ObjectId(t.authTokenId) for t in auth_tokens] token_ids = [ObjectId(t.authTokenId) for t in auth_tokens]
# Check if these tokens belong to the user claiming ownership: # Check if these tokens belong to the user claiming ownership:
if not await token_check.is_authorized( if not await token_check.is_authorized(
mongo_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
user_info = CoreUserInfoModel(**kwargs["session_info"]), user_info = CoreUserInfoModel(**kwargs["session_info"]),
token_ids = token_ids token_ids = token_ids
): return ResponseModel( ): return ResponseModel(
+3 -6
View File
@@ -222,13 +222,10 @@ async def send_sms_messages_api(
# Get the token from the token key: # Get the token from the token key:
auth_token = await current_app.sms_controller.get_token_from_key( auth_token = await current_app.sms_controller.get_token_from_key(
mongo_data_conn = current_app.data_mongo, mongo_data_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey token_key = inbound_data.tokenKey,
) must_be_active = True
if auth_token is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"No such token key."
) )
if not auth_token: return constants.API_RESPONSE_NO_AUTH_TOKEN
# ┏┓ ┓ ┏┳┓┓ ┏┓┳┳┓┏┓ # ┏┓ ┓ ┏┳┓┓ ┏┓┳┳┓┏┓
# ┗┓┏┓┏┓┏┫ ┃ ┣┓┏┓ ┗┓┃┃┃┗┓ # ┗┓┏┓┏┓┏┫ ┃ ┣┓┏┓ ┗┓┃┃┃┗┓
+13 -2
View File
@@ -416,7 +416,6 @@ class CoreAuthTokenController(CoreBaseModel):
""" """
This is to simply modify the status of an account to efficiently activate/deactivate it. This is to simply modify the status of an account to efficiently activate/deactivate it.
NOTE: The operation succeeds ONLY IF the new status of the account is different from the existing status.
:param sql_conn: The database connection (MariaDB) to use to perform the action. :param sql_conn: The database connection (MariaDB) to use to perform the action.
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action. :param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param token_key: The identifier granted by the 'generate_token_key' method. :param token_key: The identifier granted by the 'generate_token_key' method.
@@ -428,12 +427,12 @@ class CoreAuthTokenController(CoreBaseModel):
# Fetch the token from the key: # Fetch the token from the key:
additional_filter = additional_filter or {} additional_filter = additional_filter or {}
# additional_filter["status"] = {"$ne": new_status}
if self._service_type is not None: additional_filter["serviceType"] = self._service_type if self._service_type is not None: additional_filter["serviceType"] = self._service_type
if self._client is not None: additional_filter["client"] = self._client if self._client is not None: additional_filter["client"] = self._client
auth_token = await self.get_token_from_key( auth_token = await self.get_token_from_key(
mongo_data_conn = mongo_data_conn, mongo_data_conn = mongo_data_conn,
token_key = token_key, token_key = token_key,
must_be_active = False,
additional_filter = additional_filter additional_filter = additional_filter
) )
@@ -462,6 +461,7 @@ class CoreAuthTokenController(CoreBaseModel):
self, self,
mongo_data_conn: AsyncMongo, mongo_data_conn: AsyncMongo,
token_id: ObjectId | str = None, token_id: ObjectId | str = None,
must_be_active: bool = True,
additional_filter: dict = None additional_filter: dict = None
) -> CoreAuthTokenModel | None: ) -> CoreAuthTokenModel | None:
@@ -469,6 +469,7 @@ class CoreAuthTokenController(CoreBaseModel):
To retrieve stored tokens from the database. One token at a time. To retrieve stored tokens from the database. One token at a time.
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action. :param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param token_id: The identifier of the document that holds the token's details. :param token_id: The identifier of the document that holds the token's details.
:param must_be_active: Set this to False if you want to allow pending and disabled accounts to be retrieved.
:param additional_filter: Any addition filters to use. :param additional_filter: Any addition filters to use.
:return: The retrieved record that has the token, and information about the service and client if found, else :return: The retrieved record that has the token, and information about the service and client if found, else
None when there is no matching record. None when there is no matching record.
@@ -480,6 +481,7 @@ class CoreAuthTokenController(CoreBaseModel):
for k, v in self._base_filter.items(): filter_json[k] = v for k, v in self._base_filter.items(): filter_json[k] = v
if additional_filter: if additional_filter:
for k, v in additional_filter.items(): filter_json[k] = v for k, v in additional_filter.items(): filter_json[k] = v
if must_be_active: filter_json["status"] = "active"
# If there is some filtering possible, we fetch the token: # If there is some filtering possible, we fetch the token:
token = await mongo_data_conn.find_one( token = await mongo_data_conn.find_one(
@@ -494,6 +496,7 @@ class CoreAuthTokenController(CoreBaseModel):
self, self,
mongo_data_conn: AsyncMongo, mongo_data_conn: AsyncMongo,
token_key: ObjectId | str = None, token_key: ObjectId | str = None,
must_be_active: bool = True,
additional_filter: dict = None additional_filter: dict = None
) -> CoreAuthTokenModel | None: ) -> CoreAuthTokenModel | None:
@@ -501,6 +504,7 @@ class CoreAuthTokenController(CoreBaseModel):
To retrieve stored tokens from the database. One token at a time. To retrieve stored tokens from the database. One token at a time.
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action. :param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param token_key: The identifier granted by the 'generate_token_key' method. :param token_key: The identifier granted by the 'generate_token_key' method.
:param must_be_active: Set this to False if you want to allow pending and disabled accounts to be retrieved.
:param additional_filter: Any addition filters to use. :param additional_filter: Any addition filters to use.
:return: The retrieved record that has the token, and information about the service and client if found, else :return: The retrieved record that has the token, and information about the service and client if found, else
None when there is no matching record. None when there is no matching record.
@@ -512,6 +516,7 @@ class CoreAuthTokenController(CoreBaseModel):
for k, v in self._base_filter.items(): filter_json[k] = v for k, v in self._base_filter.items(): filter_json[k] = v
if additional_filter: if additional_filter:
for k, v in additional_filter.items(): filter_json[k] = v for k, v in additional_filter.items(): filter_json[k] = v
if must_be_active: filter_json["status"] = "active"
# If there is some filtering possible, we fetch the token: # If there is some filtering possible, we fetch the token:
token = await mongo_data_conn.find_one( token = await mongo_data_conn.find_one(
@@ -554,6 +559,7 @@ class CoreAuthTokenController(CoreBaseModel):
mongo_data_conn: AsyncMongo, mongo_data_conn: AsyncMongo,
token_ids: List[ObjectId | str] = None, token_ids: List[ObjectId | str] = None,
limit: int = 100, limit: int = 100,
must_be_active: bool = True,
additional_filter: dict = None additional_filter: dict = None
) -> List[CoreAuthTokenModel]: ) -> List[CoreAuthTokenModel]:
@@ -562,6 +568,7 @@ class CoreAuthTokenController(CoreBaseModel):
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action. :param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param token_ids: The identifier of the document that holds the token's details. :param token_ids: The identifier of the document that holds the token's details.
:param limit: The max. no. of records to pick. :param limit: The max. no. of records to pick.
:param must_be_active: Set this to False if you want to allow pending and disabled accounts to be retrieved.
:param additional_filter: Any addition filters to use. :param additional_filter: Any addition filters to use.
:return: The retrieved record that has the token, and information about the service and client if found, else :return: The retrieved record that has the token, and information about the service and client if found, else
None when there is no matching record. None when there is no matching record.
@@ -573,6 +580,7 @@ class CoreAuthTokenController(CoreBaseModel):
for k, v in self._base_filter.items(): filter_json[k] = v for k, v in self._base_filter.items(): filter_json[k] = v
if additional_filter: if additional_filter:
for k, v in additional_filter.items(): filter_json[k] = v for k, v in additional_filter.items(): filter_json[k] = v
if must_be_active: filter_json["status"] = "active"
# If there is some filtering possible, we fetch the token: # If there is some filtering possible, we fetch the token:
tokens = await mongo_data_conn.find_many( tokens = await mongo_data_conn.find_many(
@@ -589,6 +597,7 @@ class CoreAuthTokenController(CoreBaseModel):
mongo_data_conn: AsyncMongo, mongo_data_conn: AsyncMongo,
token_keys: List[ObjectId | str] = None, token_keys: List[ObjectId | str] = None,
limit: int = 100, limit: int = 100,
must_be_active: bool = True,
additional_filter: dict = None additional_filter: dict = None
) -> List[CoreAuthTokenModel]: ) -> List[CoreAuthTokenModel]:
@@ -597,6 +606,7 @@ class CoreAuthTokenController(CoreBaseModel):
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action. :param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param token_keys: the identifiers granted by the 'generate_token_key' method. :param token_keys: the identifiers granted by the 'generate_token_key' method.
:param limit: The max. no. of records to pick. :param limit: The max. no. of records to pick.
:param must_be_active: Set this to False if you want to allow pending and disabled accounts to be retrieved.
:param additional_filter: Any addition filters to use. :param additional_filter: Any addition filters to use.
:return: The retrieved record that has the token, and information about the service and client if found, else :return: The retrieved record that has the token, and information about the service and client if found, else
None when there is no matching record. None when there is no matching record.
@@ -608,6 +618,7 @@ class CoreAuthTokenController(CoreBaseModel):
for k, v in self._base_filter.items(): filter_json[k] = v for k, v in self._base_filter.items(): filter_json[k] = v
if additional_filter: if additional_filter:
for k, v in additional_filter.items(): filter_json[k] = v for k, v in additional_filter.items(): filter_json[k] = v
if must_be_active: filter_json["status"] = "active"
# If there is some filtering possible, we fetch the token: # If there is some filtering possible, we fetch the token:
tokens = await mongo_data_conn.find_many( tokens = await mongo_data_conn.find_many(
+18
View File
@@ -36,6 +36,10 @@ import sys
sys.path.append(".") sys.path.append(".")
sys.path.append("..") sys.path.append("..")
# My utils:
from utils_v2.api.codes import StatusCodes, HttpCodes
from utils_v2.api.response import ResponseModel
# System-level activities: # System-level activities:
import os import os
@@ -50,6 +54,20 @@ import urllib
# ***************************************************************************************************************** # *****************************************************************************************************************
# Project variables:
APP_VERSION = "1.0.0" APP_VERSION = "1.0.0"
PROJECT_NAME = "utils" PROJECT_NAME = "utils"
MODULE_NAME = "converse" MODULE_NAME = "converse"
# Standard responses:
API_RESPONSE_UNAUTHORIZED = ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = "The account does not belong to this user."
)
API_RESPONSE_NO_AUTH_TOKEN = ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.NOT_FOUND,
message = "No such integration(s) found."
)