(20241202) Testing out the modified auth process.

This commit is contained in:
2024-12-02 18:47:38 +05:30
parent aed746190d
commit bcbad8ecdb
10 changed files with 419 additions and 173 deletions
+32 -7
View File
@@ -164,19 +164,35 @@ async def mail_callback(
if tokens:
# Add information and:
# Get the e-mail id that granted authorization:
user_profile = await current_app.gmail_client.get_user_profile(tokens = tokens)
tokens.email = user_profile.data["emailAddress"] if user_profile.success else None
# The e-mail id that we requested access to and the one that granted us access should be the same:
placeholder_token = await current_app.mail_oauth_model.set_token(
mongo_conn = current_app.data_mongo,
account_identifier = inbound_data["state"]
)
if (
(not placeholder_token) or
placeholder_token["clientUserId"] != str(tokens.email)
): return await render_template(
"/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title(),
failure_hint = f"We were expecting authorization from '{placeholder_token['clientUserId']}' but got authorization from '{tokens.email}' instead."
)
# Save the tokens to the database
tokens_saved = await current_app.mail_oauth_model.set_token(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
session_token = inbound_headers.get("X-Session-Token"),
user_identifier = inbound_data["state"],
account_identifier = inbound_data["state"],
token = tokens.model_dump()
)
# tokens_saved = False
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
@@ -192,11 +208,20 @@ async def mail_callback(
# }
# )
# Return an HTML response:
return await render_template(
"/mail/oauth/oauth_success_v2.html" if tokens_saved else "/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title()
)
# Return an HTML response for success:
if tokens_saved:
return await render_template(
"/mail/oauth/oauth_success_v2.html",
mail_client = mail_client.title()
)
# Return an HTML response for failure:
else:
return await render_template(
"/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title(),
failure_hint = f"Unknown error. Please use log-id '{kwargs['logId']}' to check with the support team."
)
# *****************************************************************************************************************
+3 -2
View File
@@ -175,11 +175,12 @@ async def request_oauth_authorization_url(
# ┛
# Make a user identifier from the session info:
user_identifier = await current_app.mail_oauth_model.get_user_identifier(
user_identifier = await current_app.mail_oauth_model.get_account_identifier(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
session_token = inbound_headers["X-Session-Token"],
user_info = kwargs["session_info"],
email_id = inbound_data.mailId,
service_client = inbound_data.mailClient,
auth_type = "oauth"
)
@@ -204,7 +205,7 @@ async def request_oauth_authorization_url(
access_type = "offline",
approval_prompt = "force",
include_granted_scopes = "true",
user_email = None
user_email = inbound_data.mailId
)
# ┳┓
+56 -6
View File
@@ -63,6 +63,7 @@ from utils_v2.api.async_quart import (
# GMail-related utils:
from utils_v2.goog.gmail.gmail_client import SCOPES_GMAIL_MAIL_MANAGEMENT
from utils_v2.goog.models.data.auth_tokens import GoogleAuthTokens
# Common:
from shared import constants
@@ -113,7 +114,7 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
@mail_sync_bp.route("/sync", methods = ["GET"])
@mail_sync_bp.route("/sync", methods = ["POST"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
@@ -163,19 +164,65 @@ async def sync_mail(
)
# Start by assuming failure:
auth_url = None
mails_count = 0
# ┏┓ ┏┳┓ ┓
# ┃┓┏┓╋ ┃ ┏┓┃┏┏┓┏┓
# ┗┛┗ ┗ ┻ ┗┛┛┗┗ ┛┗
# Make a user identifier from the session info:
user_tokens = await current_app.mail_oauth_model.get_token(
user_auth = await current_app.mail_oauth_model.get_token(
mongo_conn = current_app.data_mongo,
user = kwargs["session_info"]
serviceType = "email",
user = kwargs["session_info"],
)
print("MAIL TOKEN(S):", json.to_string(user_tokens))
print("MAIL TOKEN(S):", json.to_string(user_auth, default = str))
if not user_auth: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.NOT_FOUND,
message = "user's email not connected"
)
# ┏┓┳┳┓ •┓
# ┃┓┃┃┃┏┓┓┃
# ┗┛┛ ┗┗┻┗┗
if user_auth["client"] == "gmail":
# Load the tokens into an object:
user_tokens = GoogleAuthTokens(**user_auth["token"])
# Try refreshing the tokens:
tokens_refreshed = await user_tokens.arefresh(
http_client = current_app.http_client,
client_id = current_app.gmail_client.client_id,
client_secret = current_app.gmail_client.client_secret,
force_refresh = False
)
# Update the token in the database if needed:
if tokens_refreshed: await current_app.mail_oauth_model.set_token(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
user_identifier = user_auth["_id"],
token = user_tokens.model_dump(),
session_token = inbound_headers["X-Session-Token"]
)
# Now we try to sync the mails:
mails_count = await current_app.mail_sync_model.sync(
mongo_conn = current_app.data_mongo,
user_info = kwargs["session_info"],
mail_client = current_app.gmail_client,
tokens = user_tokens,
llm = None,
force_sync = False,
start_date = inbound_data.startDate,
end_date = inbound_data.endDate,
max_count = inbound_data.maxCount
)
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
@@ -183,7 +230,10 @@ async def sync_mail(
# ┛
# Done here:
return ResponseModel(status_code = StatusCodes.OK)
return ResponseModel(
status_code = StatusCodes.OK if mails_count else StatusCodes.FAILED,
message = f"{mails_count} mail(s) sync'd"
)
# *****************************************************************************************************************