(20241214) testing token keys instead of direct ids.

This commit is contained in:
2024-12-14 11:55:05 +05:30
parent 3a03dd4a61
commit c673c3511f
14 changed files with 164 additions and 60 deletions
+26 -18
View File
@@ -238,7 +238,7 @@ class MailController:
# ┗┛┛┗┗┻┗┛┗┗━•┗┛
@staticmethod
async def get_token_id(
async def get_token_key(
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
auth_token: CoreAuthTokenModel,
@@ -246,7 +246,7 @@ class MailController:
) -> ObjectId:
# Simply call the core model:
return await current_app.core_auth_token_controller.get_token_id(
return await current_app.core_auth_token_controller.get_token_key(
db_conn = db_conn,
mongo_conn = mongo_conn,
auth_token = auth_token,
@@ -260,7 +260,7 @@ class MailController:
async def set_token(
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
token_id: ObjectId | str,
token_key: ObjectId | str,
auth_token: CoreAuthTokenModel,
session_token: str = None
) -> bool:
@@ -269,7 +269,7 @@ class MailController:
return await current_app.core_auth_token_controller.set_token(
db_conn = db_conn,
mongo_conn = mongo_conn,
token_id = token_id,
token_key = token_key,
auth_token = auth_token,
token_notes = {
"email": auth_token.token["email"],
@@ -282,13 +282,25 @@ class MailController:
@staticmethod
async def get_token(
mongo_conn: AsyncMongo,
token_id: ObjectId | str = None,
token_key: ObjectId | str = None,
) -> CoreAuthTokenModel | None:
# Simply call the core model:
return await current_app.core_auth_token_controller.get_token(
mongo_conn = mongo_conn,
token_id = token_id
token_key = token_key
)
@staticmethod
async def get_tokens(
mongo_conn: AsyncMongo,
token_keys: ObjectId | str = None,
) -> List[CoreAuthTokenModel] | None:
# Simply call the core model:
return await current_app.core_auth_token_controller.get_tokens(
mongo_conn = mongo_conn,
token_keys = token_keys
)
# ┏┓ ┳┳┓
@@ -303,7 +315,6 @@ class MailController:
self,
mongo_conn: AsyncMongo,
user_info: CoreUserInfoModel,
token_id: ObjectId,
auth_token: CoreAuthTokenModel,
mail_client: AsyncGMailClient,
google_tokens: GoogleAuthTokens,
@@ -320,11 +331,11 @@ class MailController:
if not force_sync:
mail_records = await current_app.core_message_controller.get_previews(
mongo_conn = mongo_conn,
token_ids = [ObjectId(token_id)],
token_ids = [ObjectId(auth_token.authTokenId)],
limit = 1,
skip = 0,
additional_filter = {
"tokenId": ObjectId(token_id),
"tokenId": ObjectId(auth_token.authTokenId),
"serviceType": auth_token.serviceType,
"client": auth_token.client,
"clientMessageId": message_id
@@ -357,7 +368,7 @@ class MailController:
mail_message = CoreMessageModel(
ts = client_response.data["ts"],
syncTs = date_time.get_current_utc_date_time(as_string = False),
tokenId = token_id,
tokenId = auth_token.authTokenId,
serviceType = auth_token.serviceType,
client = auth_token.client,
clientMessageId = message_id,
@@ -398,7 +409,6 @@ class MailController:
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
user_info: CoreUserInfoModel,
token_id: ObjectId,
auth_token: CoreAuthTokenModel,
mail_client: AsyncGMailClient,
llm: LLMController = None,
@@ -427,7 +437,7 @@ class MailController:
await self.set_token(
db_conn = db_conn,
mongo_conn = mongo_conn,
token_id = token_id,
token_key = auth_token.key,
auth_token = auth_token,
session_token = session_token
)
@@ -454,7 +464,6 @@ class MailController:
self.__sync_one_gmail(
mongo_conn = mongo_conn,
user_info = user_info,
token_id = token_id,
auth_token = auth_token,
mail_client = mail_client,
google_tokens = google_tokens,
@@ -478,7 +487,7 @@ class MailController:
mongo_operations.append(
ReplaceOne(
filter = {
"tokenId": ObjectId(token_id),
"tokenId": ObjectId(auth_token.authTokenId),
"serviceType": auth_token.serviceType,
"client": auth_token.client,
"clientMessageId": result.mailMessage.clientMessageId
@@ -514,7 +523,7 @@ class MailController:
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
user_info: CoreUserInfoModel,
token_id: ObjectId | str,
token_key: ObjectId | str,
llm: LLMController = None,
force_sync: bool = False,
start_date: datetime.datetime = None,
@@ -533,12 +542,12 @@ class MailController:
# We first load the authorization tokens:
auth_token = await self.get_token(
mongo_conn = mongo_conn,
token_id = token_id,
token_key = token_key,
)
# If we failed to load the authorization tokens:
if not auth_token:
sync_results.message = f"no such token id '{token_id}'"
sync_results.message = f"no such token key '{token_key}'"
return sync_results
# ┏┓ ┏┓┳┳┓ •┓
@@ -550,7 +559,6 @@ class MailController:
db_conn = db_conn,
mongo_conn = mongo_conn,
user_info = user_info,
token_id = token_id,
auth_token = auth_token,
mail_client = current_app.gmail_client,
llm = llm,
+14 -13
View File
@@ -99,7 +99,7 @@ class AuthTokenController(BaseModel):
# For MongoDB:
AUTH_COLLECTION = "_authTokens"
async def get_token_id(
async def get_token_key(
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
@@ -141,6 +141,7 @@ class AuthTokenController(BaseModel):
"syncFreq": auth_token.syncFreq
},
"$setOnInsert": {
"key": auth_token.key,
"serviceType": auth_token.serviceType,
"client": auth_token.client,
"authType": auth_token.authType,
@@ -173,7 +174,7 @@ class AuthTokenController(BaseModel):
"Auth Requested", # ............................................. 'p_last_action'
None, # ......................................................... 'p_display_name'
None, # ......................................................... 'p_display_picture'
str(mongo_json["_id"]), # ....................................... 'p_token_id'
str(auth_token.key), # .......................................... 'p_token_id'
json.to_string(python_data = token_notes, no_space = True), # ... 'p_notes'
auth_token.user.userId # ........................................ 'p_created_by'
),
@@ -181,13 +182,13 @@ class AuthTokenController(BaseModel):
)
# Done here:
return mongo_json["_id"] if mongo_json and db_json.get("status") == 1 else None
return auth_token.key if mongo_json and db_json.get("status") == 1 else None
async def set_token(
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
token_id: ObjectId | str,
token_key: ObjectId | str,
auth_token: CoreAuthTokenModel,
token_notes: dict,
session_token: str = None
@@ -199,7 +200,7 @@ class AuthTokenController(BaseModel):
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 token_id: The identifier granted by the 'get_token_id' method.
:param token_key: The identifier granted by the 'get_token_key' method.
:param auth_token: The actual auth/token data to be saved to the database.
:param token_notes: Any notes to feed into MariaDB with the token identifier.
:param session_token: The session token of the user who requested this service.
@@ -217,7 +218,7 @@ class AuthTokenController(BaseModel):
mongo_json = await mongo_conn.find_one_and_update(
collection = self.AUTH_COLLECTION,
filter = mongo_conn.dict_to_dot_notation({
"_id": ObjectId(token_id),
"key": ObjectId(token_key),
"clientUserId": auth_token.clientUserId
}),
update = [{
@@ -257,7 +258,7 @@ class AuthTokenController(BaseModel):
"Auth Granted", # ............................................... 'p_last_action'
auth_token.token.get("displayName"), # .......................... 'p_display_name'
auth_token.token.get("displayPictureUrl"), # .................... 'p_display_picture'
token_id, # ..................................................... 'p_token_id'
token_key, # .................................................... 'p_token_id'
json.to_string(python_data = token_notes, no_space = True), # ... 'p_notes'
auth_token.user.userId # ........................................ 'p_created_by'
),
@@ -271,13 +272,13 @@ class AuthTokenController(BaseModel):
async def get_token(
self,
mongo_conn: AsyncMongo,
token_id: ObjectId | str = None,
token_key: ObjectId | str = None,
) -> CoreAuthTokenModel | None:
"""
To retrieve stored tokens from the database. One token at a time.
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
:param token_id: The identifier granted by the 'get_token_id' method.
:param token_key: The identifier granted by the 'get_token_key' method.
: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.
"""
@@ -285,7 +286,7 @@ class AuthTokenController(BaseModel):
# If there is some filtering possible, we fetch the token:
token = await mongo_conn.find_one(
collection = self.AUTH_COLLECTION,
filter = {"_id": ObjectId(token_id)},
filter = {"key": ObjectId(token_key)},
)
# Done here:
@@ -294,13 +295,13 @@ class AuthTokenController(BaseModel):
async def get_tokens(
self,
mongo_conn: AsyncMongo,
token_ids: List[ObjectId | str] = None,
token_keys: List[ObjectId | str] = None,
) -> List[CoreAuthTokenModel]:
"""
To retrieve stored tokens from the database. Multiple tokens at a time.
:param mongo_conn: The database connection (MongoDB) to use to perform the action.
:param token_ids: the identifiers granted by the 'get_token_id' method.
:param token_keys: the identifiers granted by the 'get_token_key' method.
: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.
"""
@@ -308,7 +309,7 @@ class AuthTokenController(BaseModel):
# If there is some filtering possible, we fetch the token:
tokens = await mongo_conn.find_many(
collection = self.AUTH_COLLECTION,
filter = {"_id": {"$in": [ObjectId(t) for t in token_ids]}}
filter = {"key": {"$in": [ObjectId(k) for k in token_keys]}}
)
# Done here: