(20241129) Started talking to MariaDB for integration.

This commit is contained in:
2024-11-29 12:58:39 +05:30
parent 4ed028e486
commit aab700b3ed
11 changed files with 902 additions and 33 deletions
+78 -21
View File
@@ -38,8 +38,12 @@ 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
@@ -87,31 +91,32 @@ import copy
# *****************************************************************************************************************
class MailOAuthModel:
class MailOAuthModel(BaseModel):
AUTH_COLLECTION = "_authTokens"
def __init__(self):
pass
@staticmethod
async def get_id(
db_conn: AsyncMongo,
async def get_user_identifier(
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
user_info: dict,
service_type: Literal["email", "chat"],
service_client: Literal["gmail"],
auth_type: Literal["oauth"]
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 to use to perform the action.
: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 service_type: The type of service being provided.
: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.
"""
@@ -119,7 +124,7 @@ class MailOAuthModel:
request_ts = date_time.get_current_utc_date_time(as_string = False)
# Get the identifier from the database:
db_json = await db_conn.find_one_and_update(
mongo_json = await mongo_conn.find_one_and_update(
collection = MailOAuthModel.AUTH_COLLECTION,
filter = {
"serviceType": service_type,
@@ -150,30 +155,59 @@ class MailOAuthModel:
return_updated = True
)
# Done here:
return db_json["_id"] if db_json else None
# 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'
None, # ..................... '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
@staticmethod
async def set_token(
db_conn: AsyncMongo,
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
user_info: dict,
user_identifier: ObjectId | str,
token: dict
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.
:param db_conn: The database connection to use to perform the action.
:param user_identifier: The identifier granted by the 'get_id' method.
: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 user_identifier: The identifier granted by the 'get_user_identifier' method.
:param token: The token granted by the third-party service.
:return:
: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 the database:
token_saved = await db_conn.update_one(
# Save the token to MongoDB:
mongo_json = await mongo_conn.find_one_and_update(
collection = MailOAuthModel.AUTH_COLLECTION,
filter = {"_id": ObjectId(user_identifier)},
update = {
@@ -181,9 +215,32 @@ class MailOAuthModel:
"token": token,
"firstRefreshTs": request_ts,
}
}
},
projection = {"token": False},
return_updated = True,
upsert = False
)
# Tell MariaDB that the token was saved:
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'
mongo_json["client"], # .... 'p_provider'
"Auth Granted", # .......... 'p_current_status'
"Set Token", # ............. 'p_last_action'
None, # .................... 'p_display_name'
None, # .................... 'p_display_picture'
user_identifier, # ......... 'p_token_id'
token["email"], # .......... 'p_notes'
user_info["userId"] # ...... 'p_created_by'
),
session_token = session_token
)
if db_json["status"] == 1: token_saved = True
# Done here:
return token_saved
+2 -2
View File
@@ -10,7 +10,7 @@
OBJECTIVE:
Here we perform on-time mail syncing activities for our users.
Here we perform one-time mail syncing activities for our users.
REFERENCES:
@@ -86,7 +86,7 @@ import copy
# *****************************************************************************************************************
class MailOAuthModel:
class MailSyncModel:
AUTH_COLLECTION = "_authTokens"