(20241127) Testing GMail auth.

This commit is contained in:
2024-11-27 20:14:04 +05:30
parent 269e36ec27
commit 2e7f1cb626
9 changed files with 336 additions and 238 deletions
+41 -30
View File
@@ -62,6 +62,9 @@ from utils_v2.api.async_quart import (
# Common:
from shared import constants
# Behaviour Models:
from models.behaviour.mail.oauth import MailOAuthModel
# For asynchronous activities:
import asyncio
@@ -105,7 +108,7 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
@mail_callback_bp.route("/callback/gmail", methods = ["POST", "GET"])
@mail_callback_bp.route("/callback/<mail_client>", methods = ["POST", "GET"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@log_request_to_mongo(
@@ -121,6 +124,7 @@ def init(blueprint_setup_state):
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@handle_cancelled_request()
async def mail_callback(
mail_client: str = None,
inbound_headers: dict = None,
inbound_data: dict = None,
inbound_files: dict = None,
@@ -136,39 +140,46 @@ async def mail_callback(
:return: A standard response structure.
"""
# Construct a message:
message = "🪝 *WEBHOOK/CALLBACK ALERT!* 🪝\n\n"
message += f"Method: *{request.method}*\nLog Id.: `{kwargs.get('log_id')}`\n\n"
message += "*Headers:*\n```json\n"
message += json.to_string({k: v for k, v in request.headers.items()})
message += "\n```\n"
message += "*Query Args:*\n```json\n"
message += json.to_string(request.args.to_dict())
message += "\n```\n"
message += "*JSON:*\n```json\n"
message += json.to_string(await request.get_json())
message += "\n```\n"
message += "*Form-Data:*\n```json\n"
message += json.to_string((await request.form).to_dict())
message += "\n```\n"
message += "*Form-Files:*\n```json\n"
message += json.to_string(inbound_files, default = str)
message += "\n```\n"
# Start by assuming failure:
tokens_saved = False
# Send a message on Telegram:
api_response = await current_app.http_client.post(
url = current_app.script_data["alerts"]["url"],
json = {
"message": message,
"type": "info",
"chatId": "1275560043" # ... KPS
}
)
# ┏┓ ┏┓┳┳┓ •┓
# ┣ ┏┓┏┓ ┃┓┃┃┃┏┓┓┃
# ┻ ┗┛┛ ┗┛┛ ┗┗┻┗┗
if mail_client == "gmail":
# Generate the tokens from the callback:
tokens = await current_app.gmail_client.get_authorization_tokens(
redirect_url = request.url
)
if tokens:
# Add information and :
user_profile = await current_app.gmail_client.get_user_profile(tokens = tokens)
tokens.email = user_profile.data["emailAddress"] if user_profile.success else None
# Save the tokens to the database
tokens_saved = await MailOAuthModel.set_token(
db_conn = current_app.data_mongo,
user_identifier = inbound_data["state"],
token = tokens.model_dump()
)
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
# Return a success response:
return ResponseModel(
status_code = StatusCodes.OK,
data = {"accepted": True}
status_code = StatusCodes.OK if tokens_saved else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if tokens_saved else HttpCodes.INTERNAL_SERVER_ERROR,
data = {
"mailClient": mail_client,
"authorized": True
}
)