Skip to content
Open
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
311 changes: 310 additions & 1 deletion src/workos/authorization.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from typing import Any, Dict, Optional, Protocol, Sequence
from functools import partial
from typing import Any, Dict, Literal, Optional, Protocol, Sequence

from pydantic import TypeAdapter

from workos.types.authorization.environment_role import (
EnvironmentRole,
EnvironmentRoleList,
)
from workos.types.authorization.organization_membership import (
AuthorizationOrganizationMembership,
)
from workos.types.authorization.organization_role import OrganizationRole
from workos.types.authorization.permission import Permission
from workos.types.authorization.resource import Resource
from workos.types.authorization.resource_identifier import ParentResourceIdentifier
from workos.types.authorization.role import Role, RoleList
from workos.types.list_resource import (
ListArgs,
Expand Down Expand Up @@ -41,6 +47,29 @@ class PermissionListFilters(ListArgs, total=False):
]


class ResourcesForMembershipListFilters(ListArgs, total=False):
permission_slug: str


ResourcesForMembershipListResource = WorkOSListResource[
Resource,
ResourcesForMembershipListFilters,
ListMetadata,
]


class MembershipsForResourceListFilters(ListArgs, total=False):
permission_slug: str
assignment: Optional[Literal["direct", "indirect"]]


MembershipsForResourceListResource = WorkOSListResource[
AuthorizationOrganizationMembership,
MembershipsForResourceListFilters,
ListMetadata,
]


class AuthorizationModule(Protocol):
"""Offers methods through the WorkOS Authorization service."""

Expand Down Expand Up @@ -161,6 +190,44 @@ def add_environment_role_permission(
permission_slug: str,
) -> SyncOrAsync[EnvironmentRole]: ...

def list_resources_for_membership(
self,
organization_membership_id: str,
*,
permission_slug: str,
parent_resource: ParentResourceIdentifier,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> SyncOrAsync[ResourcesForMembershipListResource]: ...

def list_memberships_for_resource(
self,
resource_id: str,
*,
permission_slug: str,
assignment: Optional[Literal["direct", "indirect"]] = None,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> SyncOrAsync[MembershipsForResourceListResource]: ...

def list_memberships_for_resource_by_external_id(
self,
organization_id: str,
resource_type_slug: str,
external_id: str,
*,
permission_slug: str,
assignment: Optional[Literal["direct", "indirect"]] = None,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> SyncOrAsync[MembershipsForResourceListResource]: ...


class Authorization(AuthorizationModule):
_http_client: SyncHTTPClient
Expand Down Expand Up @@ -437,6 +504,127 @@ def add_environment_role_permission(

return EnvironmentRole.model_validate(response)

def list_resources_for_membership(
self,
organization_membership_id: str,
*,
permission_slug: str,
parent_resource: ParentResourceIdentifier,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> ResourcesForMembershipListResource:
list_params: ResourcesForMembershipListFilters = {
"limit": limit,
"before": before,
"after": after,
"order": order,
"permission_slug": permission_slug,
}

http_params: Dict[str, Any] = {**list_params}
http_params.update(parent_resource)

response = self._http_client.request(
f"authorization/organization_memberships/{organization_membership_id}/resources",
method=REQUEST_METHOD_GET,
params=http_params,
)

return WorkOSListResource[
Resource, ResourcesForMembershipListFilters, ListMetadata
](
list_method=partial(
self.list_resources_for_membership,
organization_membership_id,
parent_resource=parent_resource,
),
list_args=list_params,
**ListPage[Resource](**response).model_dump(),
)

def list_memberships_for_resource(
self,
resource_id: str,
*,
permission_slug: str,
assignment: Optional[Literal["direct", "indirect"]] = None,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> MembershipsForResourceListResource:
list_params: MembershipsForResourceListFilters = {
"limit": limit,
"before": before,
"after": after,
"order": order,
"permission_slug": permission_slug,
}
if assignment is not None:
list_params["assignment"] = assignment

response = self._http_client.request(
f"authorization/resources/{resource_id}/organization_memberships",
method=REQUEST_METHOD_GET,
params=list_params,
)

return WorkOSListResource[
AuthorizationOrganizationMembership,
MembershipsForResourceListFilters,
ListMetadata,
](
list_method=partial(self.list_memberships_for_resource, resource_id),
list_args=list_params,
**ListPage[AuthorizationOrganizationMembership](**response).model_dump(),
)

def list_memberships_for_resource_by_external_id(
self,
organization_id: str,
resource_type_slug: str,
external_id: str,
*,
permission_slug: str,
assignment: Optional[Literal["direct", "indirect"]] = None,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> MembershipsForResourceListResource:
list_params: MembershipsForResourceListFilters = {
"limit": limit,
"before": before,
"after": after,
"order": order,
"permission_slug": permission_slug,
}
if assignment is not None:
list_params["assignment"] = assignment

response = self._http_client.request(
f"authorization/organizations/{organization_id}/resources/{resource_type_slug}/{external_id}/organization_memberships",
method=REQUEST_METHOD_GET,
params=list_params,
)

return WorkOSListResource[
AuthorizationOrganizationMembership,
MembershipsForResourceListFilters,
ListMetadata,
](
list_method=partial(
self.list_memberships_for_resource_by_external_id,
organization_id,
resource_type_slug,
external_id,
),
list_args=list_params,
**ListPage[AuthorizationOrganizationMembership](**response).model_dump(),
)


class AsyncAuthorization(AuthorizationModule):
_http_client: AsyncHTTPClient
Expand Down Expand Up @@ -712,3 +900,124 @@ async def add_environment_role_permission(
)

return EnvironmentRole.model_validate(response)

async def list_resources_for_membership(
self,
organization_membership_id: str,
*,
permission_slug: str,
parent_resource: ParentResourceIdentifier,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> ResourcesForMembershipListResource:
list_params: ResourcesForMembershipListFilters = {
"limit": limit,
"before": before,
"after": after,
"order": order,
"permission_slug": permission_slug,
}

http_params: Dict[str, Any] = {**list_params}
http_params.update(parent_resource)

response = await self._http_client.request(
f"authorization/organization_memberships/{organization_membership_id}/resources",
method=REQUEST_METHOD_GET,
params=http_params,
)

return WorkOSListResource[
Resource, ResourcesForMembershipListFilters, ListMetadata
](
list_method=partial(
self.list_resources_for_membership,
organization_membership_id,
parent_resource=parent_resource,
),
list_args=list_params,
**ListPage[Resource](**response).model_dump(),
)

async def list_memberships_for_resource(
self,
resource_id: str,
*,
permission_slug: str,
assignment: Optional[Literal["direct", "indirect"]] = None,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
) -> MembershipsForResourceListResource:
list_params: MembershipsForResourceListFilters = {
"limit": limit,
"before": before,
"after": after,
"order": order,
"permission_slug": permission_slug,
}
if assignment is not None:
list_params["assignment"] = assignment

response = await self._http_client.request(
f"authorization/resources/{resource_id}/organization_memberships",
method=REQUEST_METHOD_GET,
params=list_params,
)

return WorkOSListResource[
AuthorizationOrganizationMembership,
MembershipsForResourceListFilters,
ListMetadata,
](
list_method=partial(self.list_memberships_for_resource, resource_id),
list_args=list_params,
**ListPage[AuthorizationOrganizationMembership](**response).model_dump(),
)

async def list_memberships_for_resource_by_external_id(
self,
organization_id: str,
resource_type_slug: str,
external_id: str,
*,
permission_slug: str,
assignment: Optional[Literal["direct", "indirect"]] = None,
limit: int = DEFAULT_LIST_RESPONSE_LIMIT,
before: Optional[str] = None,
after: Optional[str] = None,
order: PaginationOrder = "desc",
Comment on lines +989 to +992

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would ideally like to move this to a PAginationOptions object, but want to keep things consistence with above

) -> MembershipsForResourceListResource:
list_params: MembershipsForResourceListFilters = {
"limit": limit,
"before": before,
"after": after,
"order": order,
"permission_slug": permission_slug,
}
if assignment is not None:
list_params["assignment"] = assignment

response = await self._http_client.request(
f"authorization/organizations/{organization_id}/resources/{resource_type_slug}/{external_id}/organization_memberships",
method=REQUEST_METHOD_GET,
params=list_params,
)

return WorkOSListResource[
AuthorizationOrganizationMembership,
MembershipsForResourceListFilters,
ListMetadata,
](
list_method=partial(
self.list_memberships_for_resource_by_external_id,
organization_id,
resource_type_slug,
external_id,
),
list_args=list_params,
**ListPage[AuthorizationOrganizationMembership](**response).model_dump(),
)
5 changes: 5 additions & 0 deletions src/workos/types/authorization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
)
from workos.types.authorization.permission import Permission
from workos.types.authorization.resource import Resource
from workos.types.authorization.resource_identifier import (
ParentResourceIdentifier,
ParentResourceIdentifierByExternalId,
ParentResourceIdentifierById,
)
from workos.types.authorization.role import (
Role,
RoleList,
Expand Down
17 changes: 17 additions & 0 deletions src/workos/types/authorization/resource_identifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Union

from typing_extensions import TypedDict


class ParentResourceIdentifierById(TypedDict):
parent_resource_id: str


class ParentResourceIdentifierByExternalId(TypedDict):
parent_resource_type_slug: str
parent_resource_external_id: str


ParentResourceIdentifier = Union[
ParentResourceIdentifierById, ParentResourceIdentifierByExternalId
]
Loading