(20241205) Migrating from the field "accountId" to "tokenId" to identify auth tokens.

This commit is contained in:
2024-12-05 11:19:23 +05:30
parent f835143ef4
commit 5c36f90e98
15 changed files with 478 additions and 47 deletions
+21 -12
View File
@@ -95,7 +95,7 @@ class MailOAuthModel(BaseModel):
AUTH_COLLECTION = "_authTokens"
async def get_account_identifier(
async def get_token_id(
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
@@ -186,7 +186,7 @@ class MailOAuthModel(BaseModel):
self,
db_conn: AsyncMySQL,
mongo_conn: AsyncMongo,
account_identifier: ObjectId | str,
token_id: ObjectId | str,
email_id: str,
token: dict,
session_token: str = None
@@ -198,9 +198,9 @@ class MailOAuthModel(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 account_identifier: The identifier granted by the 'get_account_identifier' method.
:param token_id: The identifier granted by the 'get_token_id' 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'.
the user claimed he wants to connect when he used 'get_token_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.
@@ -216,7 +216,7 @@ class MailOAuthModel(BaseModel):
mongo_json = await mongo_conn.find_one_and_update(
collection = MailOAuthModel.AUTH_COLLECTION,
filter = mongo_conn.dict_to_dot_notation({
"_id": ObjectId(account_identifier),
"_id": ObjectId(token_id),
"clientUserId": {
"email": email_id
}
@@ -225,9 +225,18 @@ class MailOAuthModel(BaseModel):
"$set": {
"token": token,
"lastRefreshTs": request_ts,
},
"$setOnInsert": {
"firstRefreshTs": request_ts,
"firstRefreshTs": {
"$cond": {
"if": {
"$or": [
{"$eq": ["$fieldName", None]},
{"$eq": [{"$type": "$fieldName"}, "missing"]}
]
},
"then": request_ts,
"else": "$firstRefreshTs"
}
}
}
},
projection = {"token": False},
@@ -252,7 +261,7 @@ class MailOAuthModel(BaseModel):
"Set Token", # .................................................. 'p_last_action'
token["displayName"], # ......................................... 'p_display_name'
token["displayPictureUrl"], # ................................... 'p_display_picture'
account_identifier, # ........................................... 'p_token_id'
token_id, # ..................................................... 'p_token_id'
json.to_string(python_data = token_notes, no_space = True), # ... 'p_notes'
mongo_json["user"]["userId"] # .................................. 'p_created_by'
),
@@ -266,14 +275,14 @@ class MailOAuthModel(BaseModel):
async def get_token(
self,
mongo_conn: AsyncMongo,
account_identifier: ObjectId | str = None,
token_id: 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 token_id: The identifier granted by the 'get_token_id' 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
@@ -282,7 +291,7 @@ class MailOAuthModel(BaseModel):
# Build the filter:
filter_json = {k: v for k, v in kwargs.items()}
if account_identifier: filter_json["_id"] = ObjectId(account_identifier)
if token_id: filter_json["_id"] = ObjectId(token_id)
# If there is no search criteria, we exit with failure:
if not filter_json: return None