Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-3318 [FEAT] Platform API key post-merge fixes (#1863)

Deepak K41137d89512b
diff --git a/backend/permissions/permission.py b/backend/permissions/permission.py
index a48982d9f..19388e6c3 100644
--- a/backend/permissions/permission.py
+++ b/backend/permissions/permission.py
@@ -7,18 +7,25 @@ from rest_framework.views import APIView
from utils.user_context import UserContext
-def _is_service_account_safe_request(request: Request) -> bool:
- """Allow service accounts through for safe (read-only) HTTP methods."""
- return getattr(request.user, "is_service_account", False) and (
- request.method in permissions.SAFE_METHODS
- )
+def _is_service_account(request: Request) -> bool:
+ """Allow service accounts through for all non-DELETE methods.
+
+ Two constraints are enforced upstream in the authentication middleware
+ before this check is ever reached:
+ 1. DELETE is blocked for all API keys.
+ 2. Write methods (POST/PUT/PATCH) are blocked for READ-only API keys.
+
+ Therefore any service-account request that arrives here is permitted to
+ proceed regardless of HTTP method.
+ """
+ return getattr(request.user, "is_service_account", False)
class IsOwner(permissions.BasePermission):
"""Custom permission to only allow owners of an object."""
def has_object_permission(self, request: Request, view: APIView, obj: Any) -> bool:
- if _is_service_account_safe_request(request):
+ if _is_service_account(request):
return True
return True if obj.created_by == request.user else False
@@ -35,7 +42,7 @@ class IsOwnerOrSharedUser(permissions.BasePermission):
"""Custom permission to only allow owners and shared users of an object."""
def has_object_permission(self, request: Request, view: APIView, obj: Any) -> bool:
- if _is_service_account_safe_request(request):
+ if _is_service_account(request):
return True
return (
True
@@ -53,7 +60,7 @@ class IsOwnerOrSharedUserOrSharedToOrg(permissions.BasePermission):
"""
def has_object_permission(self, request: Request, view: APIView, obj: Any) -> bool:
- if _is_service_account_safe_request(request):
+ if _is_service_account(request):
return True
return (
True
diff --git a/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py
index 3b15792a9..b3dcbc95d 100644
--- a/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py
+++ b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py
@@ -1662,7 +1662,7 @@ class PromptStudioHelper:
tool_metadata = import_data["tool_metadata"]
tool_settings = import_data["tool_settings"]
- return CustomTool.objects.create(
+ tool = CustomTool.objects.create(
tool_name=tool_name,
description=tool_metadata["description"],
author=tool_metadata["author"],
@@ -1697,6 +1697,32 @@ class PromptStudioHelper:
organization=organization,
)
+ # When a service account creates a tool, add the API key owner
+ # as a shared user so they can see it in the UI.
+ if getattr(user, "is_service_account", False):
+ from platform_api.models import PlatformApiKey
+
+ try:
+ key = PlatformApiKey.objects.get(api_user=user)
+ if key.created_by:
+ tool.shared_users.add(key.created_by)
+ else:
+ logger.warning(
+ "PlatformApiKey for service account %s has no "
+ "created_by while creating tool %s",
+ user.id,
+ tool.tool_id,
+ )
+ except PlatformApiKey.DoesNotExist:
+ logger.warning(
+ "No PlatformApiKey found for service account %s "
+ "while creating tool %s",
+ user.id,
+ tool.tool_id,
+ )
+
+ return tool
+
@staticmethod
def create_profile_manager(
import_data: dict, selected_adapters: dict, new_tool: CustomTool, user
diff --git a/backend/utils/user_session.py b/backend/utils/user_session.py
index c311f2404..bfc7e96df 100644
--- a/backend/utils/user_session.py
+++ b/backend/utils/user_session.py
@@ -10,7 +10,14 @@ class UserSessionUtils:
@staticmethod
def get_organization_id(request: HttpRequest) -> str | None:
session_org_id = request.session.get("organization")
- requested_org_id = request.organization_id
+ requested_org_id = getattr(request, "organization_id", None)
+
+ # For Bearer token (API key) requests the middleware has already
+ # validated the key's org. Return the URL org unconditionally —
+ # ignore any stale session cookie that may also be present.
+ if getattr(request, "platform_api_key", None):
+ return requested_org_id
+
if requested_org_id and (session_org_id != requested_org_id):
return None
return session_org_id