Skip to content

Commit 0fb70d2

Browse files
cleanup
1 parent b344d14 commit 0fb70d2

File tree

4 files changed

+38
-67
lines changed

4 files changed

+38
-67
lines changed

src/workos/authorization.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010
from workos.types.authorization.organization_role import OrganizationRole
1111
from workos.types.authorization.permission import Permission
12+
from workos.types.authorization.resource_identifier import ResourceIdentifier
1213
from workos.types.authorization.role import Role, RoleList
1314
from workos.types.list_resource import (
1415
ListArgs,
@@ -169,9 +170,7 @@ def check(
169170
organization_membership_id: str,
170171
*,
171172
permission_slug: str,
172-
resource_id: Optional[str] = None,
173-
resource_external_id: Optional[str] = None,
174-
resource_type_slug: Optional[str] = None,
173+
resource: ResourceIdentifier,
175174
) -> SyncOrAsync[AccessEvaluation]: ...
176175

177176

@@ -457,26 +456,10 @@ def check(
457456
organization_membership_id: str,
458457
*,
459458
permission_slug: str,
460-
resource_id: Optional[str] = None,
461-
resource_external_id: Optional[str] = None,
462-
resource_type_slug: Optional[str] = None,
459+
resource: ResourceIdentifier,
463460
) -> AccessEvaluation:
464-
if resource_id is not None and resource_external_id is not None:
465-
raise ValueError(
466-
"resource_id and resource_external_id are mutually exclusive"
467-
)
468-
if resource_external_id is not None and resource_type_slug is None:
469-
raise ValueError(
470-
"resource_type_slug is required when resource_external_id is provided"
471-
)
472-
473461
json: Dict[str, Any] = {"permission_slug": permission_slug}
474-
if resource_id is not None:
475-
json["resource_id"] = resource_id
476-
if resource_external_id is not None:
477-
json["resource_external_id"] = resource_external_id
478-
if resource_type_slug is not None:
479-
json["resource_type_slug"] = resource_type_slug
462+
json.update(resource)
480463

481464
response = self._http_client.request(
482465
f"authorization/organization_memberships/{organization_membership_id}/check",
@@ -769,26 +752,10 @@ async def check(
769752
organization_membership_id: str,
770753
*,
771754
permission_slug: str,
772-
resource_id: Optional[str] = None,
773-
resource_external_id: Optional[str] = None,
774-
resource_type_slug: Optional[str] = None,
755+
resource: ResourceIdentifier,
775756
) -> AccessEvaluation:
776-
if resource_id is not None and resource_external_id is not None:
777-
raise ValueError(
778-
"resource_id and resource_external_id are mutually exclusive"
779-
)
780-
if resource_external_id is not None and resource_type_slug is None:
781-
raise ValueError(
782-
"resource_type_slug is required when resource_external_id is provided"
783-
)
784-
785757
json: Dict[str, Any] = {"permission_slug": permission_slug}
786-
if resource_id is not None:
787-
json["resource_id"] = resource_id
788-
if resource_external_id is not None:
789-
json["resource_external_id"] = resource_external_id
790-
if resource_type_slug is not None:
791-
json["resource_type_slug"] = resource_type_slug
758+
json.update(resource)
792759

793760
response = await self._http_client.request(
794761
f"authorization/organization_memberships/{organization_membership_id}/check",

src/workos/types/authorization/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
)
1414
from workos.types.authorization.permission import Permission
1515
from workos.types.authorization.resource import Resource
16+
from workos.types.authorization.resource_identifier import (
17+
ResourceIdentifier,
18+
ResourceIdentifierByExternalId,
19+
ResourceIdentifierById,
20+
)
1621
from workos.types.authorization.role import (
1722
Role,
1823
RoleList,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Union
2+
3+
from typing_extensions import TypedDict
4+
5+
6+
class ResourceIdentifierById(TypedDict):
7+
resource_id: str
8+
9+
10+
class ResourceIdentifierByExternalId(TypedDict):
11+
resource_external_id: str
12+
resource_type_slug: str
13+
14+
15+
ResourceIdentifier = Union[ResourceIdentifierById, ResourceIdentifierByExternalId]

tests/test_authorization_check.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import pytest
44
from tests.utils.syncify import syncify
55
from workos.authorization import AsyncAuthorization, Authorization
6+
from workos.types.authorization.resource_identifier import (
7+
ResourceIdentifierByExternalId,
8+
ResourceIdentifierById,
9+
)
610

711

812
@pytest.mark.sync_and_async(Authorization, AsyncAuthorization)
@@ -31,7 +35,7 @@ def test_check_authorized(
3135
self.authorization.check(
3236
"om_01ABC",
3337
permission_slug="documents:read",
34-
resource_id="res_01ABC",
38+
resource=ResourceIdentifierById(resource_id="res_01ABC"),
3539
)
3640
)
3741

@@ -52,7 +56,7 @@ def test_check_unauthorized(
5256
self.authorization.check(
5357
"om_01ABC",
5458
permission_slug="documents:write",
55-
resource_id="res_01ABC",
59+
resource=ResourceIdentifierById(resource_id="res_01ABC"),
5660
)
5761
)
5862

@@ -70,7 +74,7 @@ def test_check_with_resource_id(
7074
self.authorization.check(
7175
"om_01ABC",
7276
permission_slug="documents:read",
73-
resource_id="res_01XYZ",
77+
resource=ResourceIdentifierById(resource_id="res_01XYZ"),
7478
)
7579
)
7680

@@ -90,8 +94,10 @@ def test_check_with_resource_external_id(
9094
self.authorization.check(
9195
"om_01ABC",
9296
permission_slug="documents:read",
93-
resource_external_id="ext_doc_123",
94-
resource_type_slug="document",
97+
resource=ResourceIdentifierByExternalId(
98+
resource_external_id="ext_doc_123",
99+
resource_type_slug="document",
100+
),
95101
)
96102
)
97103

@@ -112,32 +118,10 @@ def test_check_url_construction(
112118
self.authorization.check(
113119
"om_01MEMBERSHIP",
114120
permission_slug="admin:access",
121+
resource=ResourceIdentifierById(resource_id="res_01ABC"),
115122
)
116123
)
117124

118125
assert request_kwargs["url"].endswith(
119126
"/authorization/organization_memberships/om_01MEMBERSHIP/check"
120127
)
121-
assert request_kwargs["json"] == {"permission_slug": "admin:access"}
122-
123-
def test_check_raises_when_both_resource_identifiers_provided(self):
124-
with pytest.raises(ValueError, match="mutually exclusive"):
125-
syncify(
126-
self.authorization.check(
127-
"om_01ABC",
128-
permission_slug="documents:read",
129-
resource_id="res_01ABC",
130-
resource_external_id="ext_doc_123",
131-
resource_type_slug="document",
132-
)
133-
)
134-
135-
def test_check_raises_when_external_id_without_type_slug(self):
136-
with pytest.raises(ValueError, match="resource_type_slug is required"):
137-
syncify(
138-
self.authorization.check(
139-
"om_01ABC",
140-
permission_slug="documents:read",
141-
resource_external_id="ext_doc_123",
142-
)
143-
)

0 commit comments

Comments
 (0)