Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/account/authentication_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def set_user_organization(
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[
Expand All @@ -212,12 +213,17 @@ def set_user_organization(
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
Expand Down
41 changes: 41 additions & 0 deletions backend/account/authentication_helper.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -36,3 +43,37 @@ def list_of_members_from_user_model(
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}"
)
7 changes: 5 additions & 2 deletions backend/middleware/remove_allow_header.py
Original file line number Diff line number Diff line change
@@ -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(
Comment thread
nehabagdia marked this conversation as resolved.
self, request: HttpRequest, response: HttpResponse
) -> HttpResponse:
response.headers.pop("Allow", None)
return response
24 changes: 19 additions & 5 deletions backend/platform_settings/platform_auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
chandrasekharan-zipstack marked this conversation as resolved.
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.
Expand Down