(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
+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: