542 lines
22 KiB
Python
542 lines
22 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Monday, 2nd Dec., 2024
|
|
|
|
OBJECTIVE:
|
|
|
|
To define the interaction between the UI layer and the database connectivity in one place. Here we shall handle
|
|
all the activities for OAuth2.0 authorization requests for all the users of our service.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# My async utils:
|
|
from utils_v2.string import json
|
|
from utils_v2.date_time import date_time
|
|
from utils_v2.database.async_mysql_v2 import AsyncMySQL
|
|
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
|
|
|
# Base model:
|
|
from models.behaviour.base import BaseModel
|
|
|
|
# To work with MongoDB:
|
|
from bson import ObjectId
|
|
|
|
# To work with datatypes:
|
|
from typing import Literal
|
|
|
|
# To make deep-copies:
|
|
import copy
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** CLASSES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
class MailOAuthModel(BaseModel):
|
|
|
|
AUTH_COLLECTION = "_authTokens"
|
|
|
|
async def get_account_identifier(
|
|
self,
|
|
db_conn: AsyncMySQL,
|
|
mongo_conn: AsyncMongo,
|
|
user_info: dict,
|
|
email_id: str,
|
|
service_client: Literal["gmail"],
|
|
auth_type: Literal["oauth"],
|
|
session_token: str = None
|
|
) -> ObjectId:
|
|
|
|
"""
|
|
Stores params from the session info and gives an identifier to use in the authorization URL. Use this when the
|
|
user requests an authorization URL to link your service to another service (like GMail).
|
|
:param db_conn: The database connection (MariaDB) to use to perform the action.
|
|
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
|
|
:param user_info: The dictionary that has the user's session information.
|
|
:param email_id: The e-mail id that the user wants to connect to your service.
|
|
:param service_client: The name of the company or brand that is providing this service that is being integrated.
|
|
:param auth_type: To identify the type of authentication being done here. This could indicate simple password
|
|
authentication, more advance OAuth2.0 authentication, etc.
|
|
:param session_token: The session token of the user who requested this service.
|
|
:return: An ObjectId to later store the granted tokens.
|
|
"""
|
|
|
|
# Note down the timestamp at which this event occurred:
|
|
request_ts = date_time.get_current_utc_date_time(as_string = False)
|
|
|
|
# Get the identifier from the database:
|
|
mongo_json = await mongo_conn.find_one_and_update(
|
|
collection = MailOAuthModel.AUTH_COLLECTION,
|
|
filter = mongo_conn.dict_to_dot_notation({
|
|
"serviceType": "email",
|
|
"user": {
|
|
"entityId": user_info["entityId"],
|
|
"billingAccountId": user_info["billingAccountId"]
|
|
},
|
|
"clientUserId": {
|
|
"email": email_id
|
|
}
|
|
}),
|
|
update = {
|
|
"$set": {
|
|
"lastRequestTs": request_ts
|
|
},
|
|
"$setOnInsert": {
|
|
"version": "1.1.1",
|
|
"serviceType": "email",
|
|
"client": service_client,
|
|
"authType": auth_type,
|
|
"user": user_info,
|
|
"token": None,
|
|
"firstRefreshTs": None,
|
|
"lastRefreshTs": None,
|
|
"firstRequestTs": request_ts,
|
|
}
|
|
},
|
|
projection = {
|
|
"_id": True
|
|
},
|
|
upsert = True,
|
|
return_updated = True
|
|
)
|
|
|
|
# Tell MariaDB that an authorization request was initiated:
|
|
db_json = {}
|
|
if mongo_json is not None:
|
|
db_json = await self.call_procedure(
|
|
db_conn = db_conn,
|
|
proc_name = "entity_integration_save",
|
|
proc_args = (
|
|
user_info["entityId"], # ............................................ 'p_entity_id'
|
|
service_client, # ................................................... 'p_provider'
|
|
"Auth Requested", # ................................................. 'p_current_status'
|
|
"Auth URL Generated", # ............................................. 'p_last_action'
|
|
None, # ............................................................. 'p_display_name'
|
|
None, # ............................................................. 'p_display_picture'
|
|
str(mongo_json["_id"]), # ........................................... 'p_token_id'
|
|
json.to_string(python_data = {"email": None}, no_space = True), # ... 'p_notes'
|
|
user_info["userId"] # ............................................... 'p_created_by'
|
|
),
|
|
session_token = session_token
|
|
)
|
|
|
|
# Done here:
|
|
return mongo_json["_id"] if mongo_json and db_json.get("status") == 1 else None
|
|
|
|
async def set_token(
|
|
self,
|
|
db_conn: AsyncMySQL,
|
|
mongo_conn: AsyncMongo,
|
|
account_identifier: ObjectId | str,
|
|
email_id: str,
|
|
token: dict,
|
|
session_token: str = None
|
|
) -> bool:
|
|
|
|
"""
|
|
This method is to be called when the end user authorizes your service to connect to his third-party account. For
|
|
example, when the end user allows you to access his GMail account. USE THIS FOR UPDATING (REFRESHING) TOKENS
|
|
ALSO.
|
|
:param db_conn: The database connection (MariaDB) to use to perform the action.
|
|
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
|
|
:param account_identifier: The identifier granted by the 'get_account_identifier' method.
|
|
:param email_id: The e-mail id that the user tried to connect to your service. This should match the e-mail id
|
|
the user claimed he wants to connect when he used 'get_account_identifier'.
|
|
:param token: The token granted by the third-party service.
|
|
:param session_token: The session token of the user who requested this service.
|
|
:return: True if saved, False if failed.
|
|
"""
|
|
|
|
# Start by assuming failure:
|
|
token_saved = False
|
|
|
|
# Note down the timestamp at which this event occurred:
|
|
request_ts = date_time.get_current_utc_date_time(as_string = False)
|
|
|
|
# Save the token to MongoDB:
|
|
mongo_json = await mongo_conn.find_one_and_update(
|
|
collection = MailOAuthModel.AUTH_COLLECTION,
|
|
filter = mongo_conn.dict_to_dot_notation({
|
|
"_id": ObjectId(account_identifier),
|
|
"clientUserId": {
|
|
"email": email_id
|
|
}
|
|
}),
|
|
update = {
|
|
"$set": {
|
|
"token": token,
|
|
"lastRefreshTs": request_ts,
|
|
},
|
|
"$setOnInsert": {
|
|
"firstRefreshTs": request_ts,
|
|
}
|
|
},
|
|
projection = {"token": False},
|
|
return_updated = True,
|
|
upsert = False
|
|
)
|
|
|
|
# Tell MariaDB that the token was saved:
|
|
if mongo_json is not None:
|
|
token_notes = {
|
|
"email": token["email"],
|
|
"displayName": token["displayName"],
|
|
"displayPictureUrl": token["displayPictureUrl"],
|
|
}
|
|
db_json = await self.call_procedure(
|
|
db_conn = db_conn,
|
|
proc_name = "entity_integration_save",
|
|
proc_args = (
|
|
mongo_json["user"]["entityId"], # ............................... 'p_entity_id'
|
|
mongo_json["client"], # ......................................... 'p_provider'
|
|
"Auth Granted", # ............................................... 'p_current_status'
|
|
"Set Token", # .................................................. 'p_last_action'
|
|
token["displayName"], # ......................................... 'p_display_name'
|
|
token["displayPictureUrl"], # ................................... 'p_display_picture'
|
|
account_identifier, # ........................................... 'p_token_id'
|
|
json.to_string(python_data = token_notes, no_space = True), # ... 'p_notes'
|
|
mongo_json["user"]["userId"] # .................................. 'p_created_by'
|
|
),
|
|
session_token = session_token
|
|
)
|
|
if db_json["status"] == 1: token_saved = True
|
|
|
|
# Done here:
|
|
return token_saved
|
|
|
|
async def get_token(
|
|
self,
|
|
mongo_conn: AsyncMongo,
|
|
account_identifier: ObjectId | str = None,
|
|
**kwargs
|
|
) -> dict | None:
|
|
|
|
"""
|
|
To retrieve stored tokens from the database.
|
|
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
|
|
:param account_identifier: The identifier granted by the 'account_identifier' method.
|
|
:param kwargs: Any set of key-value pairs to build custom search criteria. This could be things like the user
|
|
info, the client, the type of authentication used, or even the kind of service.
|
|
: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.
|
|
"""
|
|
|
|
# Build the filter:
|
|
filter_json = {k: v for k, v in kwargs.items()}
|
|
if account_identifier: filter_json["_id"] = ObjectId(account_identifier)
|
|
|
|
# If there is no search criteria, we exit with failure:
|
|
if not filter_json: return None
|
|
|
|
# If there is some filtering possible,
|
|
# we fetch and return the token:
|
|
return await mongo_conn.find_one(
|
|
collection = self.AUTH_COLLECTION,
|
|
filter = filter_json,
|
|
projection = {
|
|
"_id": True,
|
|
"serviceType": True,
|
|
"authType": True,
|
|
"client": True,
|
|
"clientUserId": True,
|
|
"token": True
|
|
}
|
|
)
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import httpx
|
|
import asyncio
|
|
|
|
http_client = httpx.AsyncClient(
|
|
limits = httpx.Limits(
|
|
max_connections = 100, # ............ Maximum number of connections allowed in the pool.
|
|
max_keepalive_connections = 50, # ... Maximum number of connections that can be kept alive.
|
|
),
|
|
timeout = httpx.Timeout(
|
|
pool = 120.0, # .... Time to wait for a free connection from the pool.
|
|
connect = 2.5, # ... Time to wait for establishing a connection to the server.
|
|
write = 10.0, # .... Time to wait for sending data.
|
|
read = 2.5 # ....... Time to wait for receiving data.
|
|
)
|
|
)
|
|
|
|
script_cred = {
|
|
"mariaDb": {
|
|
"read": {
|
|
"host": "wtt.ditscentre.in",
|
|
"user": "caOffice",
|
|
"password": "jstArchon",
|
|
"database": "caOffice",
|
|
"poolSize": 4
|
|
},
|
|
"write": {
|
|
"host": "wtt.ditscentre.in",
|
|
"user": "caOffice",
|
|
"password": "jstArchon",
|
|
"database": "caOffice",
|
|
"poolSize": 4
|
|
}
|
|
},
|
|
"mongoDb": {
|
|
"logs": {
|
|
"connectionString": "mongodb://del.ditscentre.in:27017,wtt.ditscentre.in:27017,mum.arh.001.ditscentre.in:27017/admin?tls=true&tlsCAFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_ca.pem&tlsCertificateKeyFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_cert.pem&replicaSet=dits_mongod_rep&readPreference=primary&authMechanism=MONGODB-X509&authSource=%24external",
|
|
"dbName": "converse",
|
|
"poolSize": 10
|
|
},
|
|
"data": {
|
|
"connectionString": "mongodb://del.ditscentre.in:27017,wtt.ditscentre.in:27017,mum.arh.001.ditscentre.in:27017/admin?tls=true&tlsCAFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_ca.pem&tlsCertificateKeyFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_cert.pem&replicaSet=dits_mongod_rep&readPreference=primary&authMechanism=MONGODB-X509&authSource=%24external",
|
|
"dbName": "converse",
|
|
"poolSize": 10
|
|
},
|
|
"files": {
|
|
"connectionString": "mongodb://del.ditscentre.in:27017,wtt.ditscentre.in:27017,mum.arh.001.ditscentre.in:27017/admin?tls=true&tlsCAFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_ca.pem&tlsCertificateKeyFile=%2Fetc%2Fssl%2Fcerts%2Fmongo_data_cert.pem&replicaSet=dits_mongod_rep&readPreference=primary&authMechanism=MONGODB-X509&authSource=%24external",
|
|
"dbName": "converseStore",
|
|
"poolSize": 10
|
|
}
|
|
}
|
|
}
|
|
|
|
data_mongo = AsyncMongo(
|
|
connection_string = script_cred["mongoDb"]["data"]["connectionString"],
|
|
database_name = script_cred["mongoDb"]["data"]["dbName"],
|
|
max_connections = script_cred["mongoDb"]["data"]["poolSize"],
|
|
debug = True
|
|
)
|
|
|
|
sql_writer = AsyncMySQL(
|
|
pool_size = script_cred["mariaDb"]["write"]["poolSize"],
|
|
host = script_cred["mariaDb"]["write"]["host"],
|
|
user = script_cred["mariaDb"]["write"]["user"],
|
|
password = script_cred["mariaDb"]["write"]["password"],
|
|
database = script_cred["mariaDb"]["write"]["database"]
|
|
)
|
|
|
|
mail_oauth_model = MailOAuthModel(
|
|
cache = None,
|
|
alert_url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
|
|
http_client = http_client,
|
|
debug = True,
|
|
debug_prefix = "Mail-OAuth | ",
|
|
debug_only_errors = True
|
|
)
|
|
|
|
token_json = {
|
|
"accessToken": "ya29.a0AeDClZB1Z-3fLvQe48cI7QNd5vVaovqtlDtg_pYsJSuCyk2iqU8HH3F6PEwY0a4RXJJ369lDxW7wnA9nhesu7mWqv_GydpITo51Jcyx9XMgxSKCTJ2gUmo4FiuTbY8ro2HnI5Uq9hFwbSgOs9Xj50hjs8Y__r6OwoxGebYjRaCgYKAVESARMSFQHGX2MiXZcXxJicW2igbzoDhm93AQ0175",
|
|
"refreshToken": "1//0gkcQLiO8JZtNCgYIARAAGBASNwF-L9Irz4-KNxIw0vK3Y3rhDo_pkEJPGVRWqzz549gREEH-n8UlNjGu4gIILsWM8HPBoAX16oA",
|
|
"expiresAt": {
|
|
"$date": "2024-12-04T12:24:47.282Z"
|
|
},
|
|
"scopes": [
|
|
"https://www.googleapis.com/auth/gmail.modify",
|
|
"https://www.googleapis.com/auth/userinfo.profile",
|
|
"https://www.googleapis.com/auth/gmail.labels"
|
|
],
|
|
"email": "yatmeshdemo@gmail.com",
|
|
"displayName": "yatmesh",
|
|
"displayPictureUrl": "https://lh3.googleusercontent.com/a/ACg8ocK0jD9HdAxkRWZg0qdFrQwceGeWEK4BEFs93vbi8O62cZaJkf0=s100",
|
|
"labels": {
|
|
"CHAT": {
|
|
"id": "CHAT",
|
|
"name": "CHAT",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"SENT": {
|
|
"id": "SENT",
|
|
"name": "SENT",
|
|
"type": "system"
|
|
},
|
|
"INBOX": {
|
|
"id": "INBOX",
|
|
"name": "INBOX",
|
|
"type": "system"
|
|
},
|
|
"IMPORTANT": {
|
|
"id": "IMPORTANT",
|
|
"name": "IMPORTANT",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"TRASH": {
|
|
"id": "TRASH",
|
|
"name": "TRASH",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"DRAFT": {
|
|
"id": "DRAFT",
|
|
"name": "DRAFT",
|
|
"type": "system"
|
|
},
|
|
"SPAM": {
|
|
"id": "SPAM",
|
|
"name": "SPAM",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"CATEGORY_FORUMS": {
|
|
"id": "CATEGORY_FORUMS",
|
|
"name": "CATEGORY_FORUMS",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"CATEGORY_UPDATES": {
|
|
"id": "CATEGORY_UPDATES",
|
|
"name": "CATEGORY_UPDATES",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"CATEGORY_PERSONAL": {
|
|
"id": "CATEGORY_PERSONAL",
|
|
"name": "CATEGORY_PERSONAL",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"CATEGORY_PROMOTIONS": {
|
|
"id": "CATEGORY_PROMOTIONS",
|
|
"name": "CATEGORY_PROMOTIONS",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"CATEGORY_SOCIAL": {
|
|
"id": "CATEGORY_SOCIAL",
|
|
"name": "CATEGORY_SOCIAL",
|
|
"messageListVisibility": "hide",
|
|
"labelListVisibility": "labelHide",
|
|
"type": "system"
|
|
},
|
|
"STARRED": {
|
|
"id": "STARRED",
|
|
"name": "STARRED",
|
|
"type": "system"
|
|
},
|
|
"UNREAD": {
|
|
"id": "UNREAD",
|
|
"name": "UNREAD",
|
|
"type": "system"
|
|
},
|
|
"CA-Doc": {
|
|
"id": "Label_5",
|
|
"name": "CA-Doc",
|
|
"messageListVisibility": "show",
|
|
"labelListVisibility": "labelShow",
|
|
"type": "user",
|
|
"color": {
|
|
"textColor": "#434343",
|
|
"backgroundColor": "#e7e7e7"
|
|
}
|
|
},
|
|
"CA-AI": {
|
|
"id": "Label_6",
|
|
"name": "CA-AI",
|
|
"messageListVisibility": "show",
|
|
"labelListVisibility": "labelShow",
|
|
"type": "user",
|
|
"color": {
|
|
"textColor": "#434343",
|
|
"backgroundColor": "#e7e7e7"
|
|
}
|
|
},
|
|
"TCAOFF": {
|
|
"id": "Label_7",
|
|
"name": "TCAOFF",
|
|
"messageListVisibility": "show",
|
|
"labelListVisibility": "labelShow",
|
|
"type": "user",
|
|
"color": {
|
|
"textColor": "#434343",
|
|
"backgroundColor": "#e7e7e7"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async def main():
|
|
|
|
await mail_oauth_model.set_token(
|
|
db_conn = sql_writer,
|
|
mongo_conn = data_mongo,
|
|
account_identifier = ObjectId("67503139a7804fcbc6c22e14"),
|
|
email_id = "yatmeshdemo@gmail.com",
|
|
token = token_json,
|
|
session_token = None
|
|
)
|
|
|
|
asyncio.run(main())
|