(20241129) ??

This commit is contained in:
2024-11-29 18:19:27 +05:30
parent a99b3f19c2
commit 0c05dcf6c5
4 changed files with 19 additions and 99 deletions
+14 -92
View File
@@ -10,7 +10,8 @@
OBJECTIVE:
Here we perform one-time mail syncing activities for our users.
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:
@@ -37,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
@@ -86,105 +91,22 @@ import copy
# *****************************************************************************************************************
class MailSyncModel:
class MailReadModel(BaseModel):
AUTH_COLLECTION = "_authTokens"
def __init__(self):
pass
@staticmethod
async def get_id(
db_conn: AsyncMongo,
async def read_one(
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 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.
: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:
db_json = await db_conn.find_one_and_update(
collection = MailOAuthModel.AUTH_COLLECTION,
filter = {
"serviceType": service_type,
"client": service_client,
"authType": auth_type,
"user": user_info,
},
update = {
"$set": {
"lastRequestTs": request_ts
},
"$setOnInsert": {
"version": "1.0.0",
"serviceType": service_type,
"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
)
# Done here:
return db_json["_id"] if db_json else None
@staticmethod
async def set_token(
db_conn: AsyncMongo,
user_identifier: ObjectId | str,
token: dict
) -> 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 token: The token granted by the third-party service.
:return:
"""
# 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(
collection = MailOAuthModel.AUTH_COLLECTION,
filter = {"_id": ObjectId(user_identifier)},
update = {
"$set": {
"token": token,
"firstRefreshTs": request_ts,
}
}
)
# Done here:
return token_saved
pass
# *****************************************************************************************************************