Skip to content

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

add initial default platform key while creating a new organization (#63)

ali26a6e6568217
diff --git a/backend/account/authentication_controller.py b/backend/account/authentication_controller.py
index f434a6a03..bc13270d1 100644
--- a/backend/account/authentication_controller.py
+++ b/backend/account/authentication_controller.py
@@ -187,6 +187,7 @@ class AuthenticationController:
self, request: Request, organization_id: str
) -> Response:
user: User = request.user
+ new_organization = False
organization_ids = CacheService.get_user_organizations(user.user_id)
if not organization_ids:
z_organizations: list[
@@ -212,12 +213,17 @@ class AuthenticationController:
organization_data.display_name,
organization_data.id,
)
+ new_organization = True
except IntegrityError:
raise DuplicateData(
f"{ErrorMessage.ORGANIZATION_EXIST}, \
{ErrorMessage.DUPLICATE_API}"
)
self.create_tenant_user(organization=organization, user=user)
+ if new_organization:
+ self.authentication_helper.create_initial_platform_key(
+ user=user, organization=organization
+ )
user_info: Optional[UserInfo] = self.get_user_info(request)
serialized_user_info = SetOrganizationsResponseSerializer(
user_info
diff --git a/backend/account/authentication_helper.py b/backend/account/authentication_helper.py
index 69a80dcd4..6642ac238 100644
--- a/backend/account/authentication_helper.py
+++ b/backend/account/authentication_helper.py
@@ -1,9 +1,16 @@
+import logging
from typing import Any
from account.constants import DefaultOrg
from account.dto import CallbackData, MemberData, OrganizationData
+from account.models import Organization, User
+from platform_settings.platform_auth_service import (
+ PlatformAuthenticationService,
+)
from rest_framework.request import Request
+logger = logging.getLogger(__name__)
+
class AuthenticationHelper:
def __init__(self) -> None:
@@ -36,3 +43,37 @@ class AuthenticationHelper:
members.append(MemberData(user_id=user_id, email=email, name=name))
return members
+
+ def create_initial_platform_key(
+ self, user: User, organization: Organization
+ ) -> None:
+ """Create an initial platform key for the given user and organization.
+
+ This method generates a new platform key with the specified parameters
+ and saves it to the database. The generated key is set as active and
+ assigned the name "Key #1". The key is associated with the provided
+ user and organization.
+
+ Parameters:
+ user (User): The user for whom the platform key is being created.
+ organization (Organization):
+ The organization to which the platform key belongs.
+
+ Raises:
+ Exception: If an error occurs while generating the platform key.
+
+ Returns:
+ None
+ """
+ try:
+ PlatformAuthenticationService.generate_platform_key(
+ is_active=True,
+ key_name="Key #1",
+ user=user,
+ organization=organization,
+ )
+ except Exception:
+ logger.error(
+ "Failed to create default platform key for "
+ f"organization {organization.organization_id}"
+ )
diff --git a/backend/middleware/remove_allow_header.py b/backend/middleware/remove_allow_header.py
index ffdc7d589..ccd8bcb9e 100644
--- a/backend/middleware/remove_allow_header.py
+++ b/backend/middleware/remove_allow_header.py
@@ -1,7 +1,10 @@
+from django.http import HttpRequest, HttpResponse
from django.utils.deprecation import MiddlewareMixin
class RemoveAllowHeaderMiddleware(MiddlewareMixin):
- def process_response(self, request, response):
- response.headers.pop('Allow', None)
+ def process_response(
+ self, request: HttpRequest, response: HttpResponse
+ ) -> HttpResponse:
+ response.headers.pop("Allow", None)
return response
diff --git a/backend/platform_settings/platform_auth_service.py b/backend/platform_settings/platform_auth_service.py
index 8ad74b20e..7258b982f 100644
--- a/backend/platform_settings/platform_auth_service.py
+++ b/backend/platform_settings/platform_auth_service.py
@@ -23,21 +23,35 @@ class PlatformAuthenticationService:
@staticmethod
def generate_platform_key(
- is_active: bool, key_name: str, user: User
+ is_active: bool,
+ key_name: str,
+ user: User,
+ organization: Optional[Organization] = None,
) -> dict[str, Any]:
"""Method to support generation of new platform key. Throws error when
maximum count is exceeded. Forbids for user other than admin
permission.
Args:
- key (str): Value of the key
+ key_name (str): Value of the key
is_active (bool): By default the key is False
- organization_id (str): Org the key belongs to.
+ user (User): User object representing the user generating the key
+ organization (Optional[Organization], optional):
+ Org the key belongs to. Defaults to None.
Returns:
- PlatformKey
+ dict[str, Any]:
+ A dictionary containing the generated platform key details,
+ including the id, key name, and key value.
+ Raises:
+ DuplicateData: If a platform key with the same key name
+ already exists for the organization.
+ InternalServiceError: If an internal error occurs while
+ generating the platform key.
"""
- organization = connection.get_tenant()
+ organization = organization or connection.tenant
+ if not organization:
+ raise InternalServiceError("No valid organization provided")
try:
# TODO : Add encryption to Platform keys
# id is added here to avoid passing of keys in transactions.