(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
+2 -2
View File
@@ -161,7 +161,7 @@ async def handle_gmail_callback() -> render_template:
# if they don't match, we reject the authorization:
auth_token = await current_app.mail_controller.get_token(
mongo_conn = current_app.data_mongo,
token_id = g.inbound_data["state"]
token_key = g.inbound_data["state"]
)
if (
(not auth_token) or
@@ -218,7 +218,7 @@ async def handle_gmail_callback() -> render_template:
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
session_token = g.inbound_headers.get("X-Session-Token"),
token_id = g.inbound_data["state"],
token_key = g.inbound_data["state"],
auth_token = auth_token
)
+8 -8
View File
@@ -170,12 +170,13 @@ async def request_oauth_authorization_url(
# Start by assuming failure:
auth_url = None
# ┏┓ ┏┳┓ ┓
# ┃┓┏┓┏┓┏┓┏┓┏┓╋┏┓ ┃ ┏┓┃┏┏┓┏┓ ┃
# ┗┛┗ ┛┗┗ ┛ ┗┻┗┗ ┻ ┗┛┛┗┗ ┛┗ ┻┗┻
# ┏┓ ┏┳┓ ┓ ┓┏
# ┃┓┏┓┏┓┏┓┏┓┏┓╋┏┓ ┃ ┏┓┃┏┏┓┏┓ ┃┫ ┏┓┓┏
# ┗┛┗ ┛┗┗ ┛ ┗┻┗┗ ┻ ┗┛┛┗┗ ┛┗ ┛┗┛┗ ┗┫
# ┛
# Make a user identifier from the session info:
token_id = await current_app.mail_controller.get_token_id(
token_key = await current_app.mail_controller.get_token_key(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
auth_token = CoreAuthTokenModel(
@@ -189,13 +190,12 @@ async def request_oauth_authorization_url(
),
session_token = inbound_headers["X-Session-Token"]
)
if token_id is None:
if token_key is None:
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.INTERNAL_SERVER_ERROR,
message = "failed to generate token id"
message = "failed to generate token key"
)
token_id = str(token_id)
# ┏┓ ┏┓┳┳┓ •┓
# ┣ ┏┓┏┓ ┃┓┃┃┃┏┓┓┃
@@ -206,7 +206,7 @@ async def request_oauth_authorization_url(
# Get the authorization URL:
auth_url = await current_app.gmail_client.get_authorization_url(
scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
state = token_id,
state = str(token_key),
access_type = "offline",
approval_prompt = "force",
include_granted_scopes = "true",
+29 -2
View File
@@ -156,17 +156,44 @@ async def get_one_mail(
:return: A standard response structure.
"""
# ┏┓ ┓ ┏┓┓ ┓
# ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
# ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
# If the session token is invalid/expired:
if kwargs.get("session_info") is None:
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED
http_code = HttpCodes.UNAUTHORIZED,
messge = "invalid session"
)
# ┏┓ ┓ ┏┳┓ ┓
# ┣ ┏┓╋┏┣┓ ┃ ┏┓┃┏┏┓┏┓┏
# ┻ ┗ ┗┗┛┗ ┻ ┗┛┛┗┗ ┛┗┛
# We first load the authorization tokens:
auth_token = await current_app.mail_controller.get_token(
mongo_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey,
)
# If we failed to load the authorization tokens:
if not auth_token:
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"no such token key '{inbound_data.tokenKey}'"
)
# ┏┓ ┓ ┳┳┓ •┓
# ┣ ┏┓╋┏┣┓ ┃┃┃┏┓┓┃
# ┻ ┗ ┗┗┛┗ ┛ ┗┗┻┗┗
# Get the mail:
message = await current_app.mail_controller.get_one_mail(
mongo_conn = current_app.data_mongo,
token_id = inbound_data.tokenId,
token_id = auth_token.authTokenId,
message_id = inbound_data.messageId
)
+20 -8
View File
@@ -163,16 +163,28 @@ async def list_mails(
:return: A standard response structure.
"""
# ┏┓ ┓ ┏┓┓ ┓
# ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
# ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
# If the session token is invalid/expired:
if await token_check.is_not_authorized(
if kwargs.get("session_info") is None:
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
messge = "invalid session"
)
# ┏┓ ┓• ┳┳┓ •┓
# ┣ ┏┓┃┓┏╋ ┃┃┃┏┓┓┃┏
# ┗┛┛┗┗┗┛┗ ┛ ┗┗┻┗┗┛
# Get the token ids from the token keys:
auth_tokens = await current_app.mail_controller.get_tokens(
mongo_conn = current_app.data_mongo,
user_info = kwargs.get("session_info"),
token_ids = inbound_data.tokenIds
): return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = "user not authorized to use this token"
token_keys = inbound_data.tokenKeys
)
token_ids = [t.authTokenId for t in auth_tokens]
# Build the additional filter:
additional_filter = {}
@@ -182,7 +194,7 @@ async def list_mails(
# Get the mails:
mails_list = await current_app.mail_controller.list_mails(
mongo_conn = current_app.data_mongo,
token_ids = inbound_data.tokenIds,
token_ids = token_ids,
limit = inbound_data.count,
skip = inbound_data.fromCount,
additional_filter = additional_filter
+1 -1
View File
@@ -146,7 +146,7 @@ async def sync_mails(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
user_info = user_info,
token_id = inbound_data.tokenId,
token_key = inbound_data.tokenKey,
llm = current_app.llm,
force_sync = inbound_data.forceSync,
start_date = inbound_data.startDate,
+17 -2
View File
@@ -156,6 +156,10 @@ async def update_mail_tags(
:return: A standard response structure.
"""
# ┏┓ ┓ ┏┓┓ ┓
# ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
# ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
# If the session token is invalid/expired:
if kwargs.get("session_info") is None:
return ResponseModel(
@@ -163,10 +167,21 @@ async def update_mail_tags(
http_code = HttpCodes.UNAUTHORIZED
)
# Get the mail:
# ┳┳ ┓ ┳┳┓ •┓
# ┃┃┏┓┏┫┏┓╋┏┓ ┃┃┃┏┓┓┃
# ┗┛┣┛┗┻┗┻┗┗ ┛ ┗┗┻┗┗
# ┛
# get the token id from the token key:
auth_token = await current_app.mail_controller.get_token(
mongo_conn = current_app.data_mongo,
token_key = inbound_data.tokenKey
)
# Update the mail:
success = await current_app.mail_controller.update_tags(
mongo_conn = current_app.data_mongo,
token_id = inbound_data.tokenId,
token_id = auth_token.authTokenId,
message_id = inbound_data.messageId,
unset_tags = inbound_data.unsetTags,
set_tags = inbound_data.setTags