From c78ab00ca7c18e5852eabc14551eca8be1720d11 Mon Sep 17 00:00:00 2001 From: khushal Date: Thu, 16 Jan 2025 17:08:15 +0530 Subject: [PATCH] (20250116) Fixed auth-token bug for mail controllers. --- .../message/mail/oauth/callback_v2.py | 7 ++++-- controllers_v2/message/mail/base.py | 2 +- controllers_v2/message/mail/gmail.py | 24 +++++++++---------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/api/blueprints/message/mail/oauth/callback_v2.py b/api/blueprints/message/mail/oauth/callback_v2.py index 3ef1692..183e2c2 100644 --- a/api/blueprints/message/mail/oauth/callback_v2.py +++ b/api/blueprints/message/mail/oauth/callback_v2.py @@ -258,8 +258,11 @@ async def mail_auth_callback( # For failed authorization: return await render_template( "/message/mail/oauth/oauth_failure_v2.html", - mail_client = mail_client.title(), - failure_hint = f"Unknown error. Please use log-id '{kwargs.get('log_id')}' to check with the support team." + mail_client = mail_client, + failure_hint = ( + f"{client_response.message} " + f"Please use log-id '{kwargs.get('log_id')}' to check with the support team.".strip() + ) ) diff --git a/controllers_v2/message/mail/base.py b/controllers_v2/message/mail/base.py index fcc723a..bdfdebf 100644 --- a/controllers_v2/message/mail/base.py +++ b/controllers_v2/message/mail/base.py @@ -135,7 +135,7 @@ class MailController(CoreMessageController, ABC): # Prepare the combined base filter: sms_filter = {} for k, v in (base_filter or {}).items(): sms_filter[k] = v - sms_filter["serviceType"] = "sms" + sms_filter["serviceType"] = "email" # Invoke the parent's constructor: CoreMessageController.__init__( diff --git a/controllers_v2/message/mail/gmail.py b/controllers_v2/message/mail/gmail.py index e58a40e..620efd5 100644 --- a/controllers_v2/message/mail/gmail.py +++ b/controllers_v2/message/mail/gmail.py @@ -135,7 +135,7 @@ class GmailController(MailController): cache = cache, alert_url = alert_url, http_client = http_client, - base_filter = {"client": "nimbusSmsIndia"}, + base_filter = {"client": "gmail"}, debug = debug, debug_prefix = debug_prefix, debug_only_errors = debug_only_errors @@ -236,6 +236,16 @@ class GmailController(MailController): response.action = "authorized" response.message = "The user has given authorization." + pass + # We fetch the auth-token associated with this authorization loop: + auth_token = await self.get_token_from_key( + mongo_data_conn = mongo_data_conn, + token_key = inbound_data["state"] + ) + if not auth_token: + response.message = "Failed to load the auth-token for this flow." + return response + # Generate the tokens from the callback. Google sends all the needed params in the callback as the URL's query # params. We can simply use the exact URL that was hit to generate the tokens. In Quart (and Flask) this can be # achieved by 'request.url' like this: @@ -259,17 +269,7 @@ class GmailController(MailController): response.message = "Failed to get the user's profile from Gmail." return response - # Now we check if the email that the user originally claimed to authorize is the same as the one that gave the - # authorization. We must fetch the auth-token for that: - auth_token = await self.get_token_from_key( - mongo_data_conn = mongo_data_conn, - token_key = inbound_data["state"] - ) - if not auth_token: - response.message = "Failed to load the auth-token for this flow." - return response - - # If the two email ids don't match: + # We confirm if the expected email account and the one that gave authorization are the same: if auth_token.clientUserId["email"] != str(google_tokens.email): response.message = ( f"We were expecting authorization from '{auth_token.clientUserId['email']}', "