Skip to content

Commit ec12bca

Browse files
committed
Use authenticated decorators properly
1 parent e0e85f1 commit ec12bca

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

app/backend/chat_history/cosmosdb.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import time
3-
from typing import Any, Dict, Union, cast
3+
from typing import Any, Dict, Union
44

55
from azure.cosmos.aio import ContainerProxy, CosmosClient
66
from azure.identity.aio import AzureDeveloperCliCredential, ManagedIdentityCredential
@@ -12,7 +12,7 @@
1212
CONFIG_COSMOS_HISTORY_CONTAINER,
1313
CONFIG_CREDENTIAL,
1414
)
15-
from decorators import authenticated, authenticated_path
15+
from decorators import authenticated
1616
from error import error_response
1717

1818
chat_history_cosmosdb_bp = Blueprint("chat_history_cosmos", __name__, static_folder="static")
@@ -54,7 +54,7 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
5454
if not current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED]:
5555
return jsonify({"error": "Chat history not enabled"}), 405
5656

57-
container = cast(ContainerProxy, current_app.config[CONFIG_COSMOS_HISTORY_CONTAINER])
57+
container: ContainerProxy = current_app.config[CONFIG_COSMOS_HISTORY_CONTAINER]
5858
if not container:
5959
return jsonify({"error": "Chat history not enabled"}), 405
6060

@@ -103,25 +103,25 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
103103
return error_response(error, "/chat_history/items")
104104

105105

106-
@chat_history_cosmosdb_bp.get("/chat_history/items/<path>")
107-
@authenticated_path
108-
async def get_chat_history_session(path: str, auth_claims: Dict[str, Any]):
106+
@chat_history_cosmosdb_bp.get("/chat_history/items/<item_id>")
107+
@authenticated
108+
async def get_chat_history_session(auth_claims: Dict[str, Any], item_id: str):
109109
if not current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED]:
110110
return jsonify({"error": "Chat history not enabled"}), 405
111111

112-
container = cast(ContainerProxy, current_app.config[CONFIG_COSMOS_HISTORY_CONTAINER])
112+
container: ContainerProxy = current_app.config[CONFIG_COSMOS_HISTORY_CONTAINER]
113113
if not container:
114114
return jsonify({"error": "Chat history not enabled"}), 405
115115

116-
if not path:
117-
return jsonify({"error": "Invalid path"}), 400
116+
if not item_id:
117+
return jsonify({"error": "Invalid item ID specifier"}), 400
118118

119119
entra_oid = auth_claims.get("oid")
120120
if not entra_oid:
121121
return jsonify({"error": "User OID not found"}), 401
122122

123123
try:
124-
res = await container.read_item(item=path, partition_key=entra_oid)
124+
res = await container.read_item(item=item_id, partition_key=entra_oid)
125125
return (
126126
jsonify(
127127
{
@@ -135,31 +135,31 @@ async def get_chat_history_session(path: str, auth_claims: Dict[str, Any]):
135135
200,
136136
)
137137
except Exception as error:
138-
return error_response(error, f"/chat_history/items/{path}")
138+
return error_response(error, f"/chat_history/items/{item_id}")
139139

140140

141-
@chat_history_cosmosdb_bp.delete("/chat_history/items/<path>")
142-
@authenticated_path
143-
async def delete_chat_history_session(path: str, auth_claims: Dict[str, Any]):
141+
@chat_history_cosmosdb_bp.delete("/chat_history/items/<item_id>")
142+
@authenticated
143+
async def delete_chat_history_session(auth_claims: Dict[str, Any], item_id: str):
144144
if not current_app.config[CONFIG_CHAT_HISTORY_COSMOS_ENABLED]:
145145
return jsonify({"error": "Chat history not enabled"}), 405
146146

147-
container = cast(ContainerProxy, current_app.config[CONFIG_COSMOS_HISTORY_CONTAINER])
147+
container: ContainerProxy = current_app.config[CONFIG_COSMOS_HISTORY_CONTAINER]
148148
if not container:
149149
return jsonify({"error": "Chat history not enabled"}), 405
150150

151-
if not path:
151+
if not item_id:
152152
return jsonify({"error": "Invalid path"}), 400
153153

154154
entra_oid = auth_claims.get("oid")
155155
if not entra_oid:
156156
return jsonify({"error": "User OID not found"}), 401
157157

158158
try:
159-
await container.delete_item(item=path, partition_key=entra_oid)
159+
await container.delete_item(item=item_id, partition_key=entra_oid)
160160
return jsonify({}), 200
161161
except Exception as error:
162-
return error_response(error, f"/chat_history/items/{path}")
162+
return error_response(error, f"/chat_history/items/{item_id}")
163163

164164

165165
@chat_history_cosmosdb_bp.before_app_serving
@@ -169,9 +169,9 @@ async def setup_clients():
169169
AZURE_CHAT_HISTORY_DATABASE = os.getenv("AZURE_CHAT_HISTORY_DATABASE")
170170
AZURE_CHAT_HISTORY_CONTAINER = os.getenv("AZURE_CHAT_HISTORY_CONTAINER")
171171

172-
azure_credential = cast(
173-
Union[AzureDeveloperCliCredential, ManagedIdentityCredential], current_app.config[CONFIG_CREDENTIAL]
174-
)
172+
azure_credential: Union[AzureDeveloperCliCredential, ManagedIdentityCredential] = current_app.config[
173+
CONFIG_CREDENTIAL
174+
]
175175

176176
if USE_CHAT_HISTORY_COSMOS:
177177
current_app.logger.info("USE_CHAT_HISTORY_COSMOS is true, setting up CosmosDB client")

app/backend/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def authenticated(route_fn: Callable[[Dict[str, Any]], Any]):
4343
"""
4444

4545
@wraps(route_fn)
46-
async def auth_handler():
46+
async def auth_handler(*args, **kwargs):
4747
auth_helper = current_app.config[CONFIG_AUTH_CLIENT]
4848
try:
4949
auth_claims = await auth_helper.get_auth_claims_if_enabled(request.headers)
5050
except AuthError:
5151
abort(403)
5252

53-
return await route_fn(auth_claims)
53+
return await route_fn(auth_claims, *args, **kwargs)
5454

5555
return auth_handler

0 commit comments

Comments
 (0)