From 55ada94cda518f2a86bf919f8487d75a1e363ed9 Mon Sep 17 00:00:00 2001 From: khushal Date: Wed, 22 Jan 2025 16:29:46 +0530 Subject: [PATCH] (20250122) Mail module adjustment for sending mails from whitelisted IPs. --- api/blueprints/message/mail/send/send.py | 33 +++++++++++++----------- cron/misc/reminders_for_work.py | 16 ++++++++++-- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/api/blueprints/message/mail/send/send.py b/api/blueprints/message/mail/send/send.py index 4d90afd..ec6087f 100644 --- a/api/blueprints/message/mail/send/send.py +++ b/api/blueprints/message/mail/send/send.py @@ -233,23 +233,26 @@ async def send_one_mail( # ┗┛┗┻┛┛┗┗ ┛ ┛┛┗┗┣┛ ┗┛┛┗┗ ┗┛┗ # ┛ - # Get the token based on the key: - auth_token = await current_app.mail_controller.get_token_from_key( - mongo_data_conn = current_app.data_mongo, - token_key = inbound_data.tokenKey - ) - user_info = CoreUserInfoModel(**kwargs["session_info"]) + # We test ownership only in API calls made from outside: + if inbound_headers["Remote-IP"] not in current_app.whitelisted_ips: - # We check if the token that was used to fetch the mail is owned by this user: - if not await token_check.is_authorized( + # Get the token based on the key: + auth_token = await current_app.mail_controller.get_token_from_key( mongo_data_conn = current_app.data_mongo, - user_info = user_info, - token_ids = [auth_token.authTokenId] - ): return ResponseModel( - status_code = StatusCodes.FAILED, - http_code = HttpCodes.UNAUTHORIZED, - message = "The account does not belong to this user." - ) + token_key = inbound_data.tokenKey + ) + user_info = CoreUserInfoModel(**kwargs["session_info"]) + + # We check if the token that was used to fetch the mail is owned by this user: + if not await token_check.is_authorized( + mongo_data_conn = current_app.data_mongo, + user_info = user_info, + token_ids = [auth_token.authTokenId] + ): return ResponseModel( + status_code = StatusCodes.FAILED, + http_code = HttpCodes.UNAUTHORIZED, + message = "The account does not belong to this user." + ) # ┏┓ ┓ ┳┳┓ •┓ # ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┓┃ diff --git a/cron/misc/reminders_for_work.py b/cron/misc/reminders_for_work.py index a571863..ff79da6 100644 --- a/cron/misc/reminders_for_work.py +++ b/cron/misc/reminders_for_work.py @@ -344,6 +344,9 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: :return: None """ + # Start with blank variables: + log_id = None + # Get a list of unique token-keys: unique_token_keys = chat_df["tokenKey"].unique().tolist() @@ -373,6 +376,9 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: } ) + # Get the log id from the response: + log_id = response.json()["logId"] + # If the API call failed: response.raise_for_status() @@ -384,7 +390,7 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: # Send out an alert if needed: if exception is not None: await send_telegram( - message = f"*Reminders For Work (Chat)*\n\nException: `{exception}`", + message = f"*Reminders For Work (Chat)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`", message_type = "error" ) @@ -400,6 +406,9 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None: :return: None """ + # Start with blank variables: + log_id = None + # Iterate over all the rows and send out the mails: for index, row in mail_df.iterrows(): @@ -428,6 +437,9 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None: json = input_json ) + # Get the log id from the response: + log_id = response.json()["logId"] + # If the API call failed: response.raise_for_status() @@ -439,7 +451,7 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None: # Send out an alert if needed: if exception is not None: await send_telegram( - message = f"*Reminders For Work (Mail)*\n\nException: `{exception}`", + message = f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`", message_type = "error" )