Skip to content

Commit 32f71c5

Browse files
authored
Add warrant object support in check many (#18)
* Clean up unused imports * Allow warrant objects along with dicts when checking multiple warrants * Add type hint for subject param in check
1 parent 7096761 commit 32f71c5

File tree

9 files changed

+41
-20
lines changed

9 files changed

+41
-20
lines changed

test/test_live.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,11 @@ def test_batch_create_delete_warrants(self):
684684
)
685685
self.assertEqual(user_has_permission2, False)
686686

687+
perm1_warrant = warrant.Warrant({"objectType": "permission", "objectId": permission1.id, "relation": "member", "subject": warrant.Subject("user", new_user.id)})
687688
user_has_permissions = warrant.Authz.check_many(
688689
warrant.CheckOp.ALL_OF,
689690
[
690-
{"objectType": permission1.object_type, "objectId": permission1.id, "relation": "member", "subject": {"objectType": new_user.object_type, "objectId": new_user.id}},
691+
perm1_warrant,
691692
{"objectType": permission2.object_type, "objectId": permission2.id, "relation": "member", "subject": {"objectType": new_user.object_type, "objectId": new_user.id}}
692693
],
693694
opts={"Warrant-Token": "latest"}
@@ -721,7 +722,7 @@ def test_batch_create_delete_warrants(self):
721722
user_has_permissions = warrant.Authz.check_many(
722723
warrant.CheckOp.ALL_OF,
723724
[
724-
{"objectType": permission1.object_type, "objectId": permission1.id, "relation": "member", "subject": {"objectType": new_user.object_type, "objectId": new_user.id}},
725+
perm1_warrant,
725726
{"objectType": permission2.object_type, "objectId": permission2.id, "relation": "member", "subject": {"objectType": new_user.object_type, "objectId": new_user.id}}
726727
],
727728
opts={"Warrant-Token": "latest"}

warrant/authz.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
import warrant
2-
from warrant import APIResource, Subject
2+
from warrant import APIResource, Subject, Warrant
33
from enum import Enum
4-
from typing import Any, Dict, List, Optional, Union
4+
from typing import Any, Dict, List
55

66

77
class CheckOp(str, Enum):
88
ANY_OF = "anyOf"
99
ALL_OF = "allOf"
1010

1111

12+
def map_warrant(warrant):
13+
if isinstance(warrant, Warrant):
14+
subject = {
15+
"objectType": warrant.subject.object_type,
16+
"objectId": warrant.subject.object_id
17+
}
18+
if warrant.subject.relation != "":
19+
subject["relation"] = warrant.subject.relation
20+
21+
return {
22+
"objectType": warrant.object_type,
23+
"objectId": warrant.object_id,
24+
"relation": warrant.relation,
25+
"subject": subject
26+
}
27+
else:
28+
return warrant
29+
30+
1231
class Authz(APIResource):
1332
@classmethod
14-
def check(cls, object_type: str, object_id: str, relation: str, subject, context: Dict[str, Any] = {}, opts: Dict[str, Any] = {}) -> bool:
33+
def check(cls, object_type: str, object_id: str, relation: str, subject: Subject | Dict[str, Any], context: Dict[str, Any] = {}, opts: Dict[str, Any] = {}) -> bool:
1534
warrantToCheck = {
1635
"objectType": object_type,
1736
"objectId": object_id,
@@ -38,10 +57,11 @@ def check(cls, object_type: str, object_id: str, relation: str, subject, context
3857
return False
3958

4059
@classmethod
41-
def check_many(cls, op: CheckOp, warrants: List[Dict[str, Any]], opts: Dict[str, Any] = {}):
60+
def check_many(cls, op: CheckOp, warrants: List[Dict[str, Any] | Warrant], opts: Dict[str, Any] = {}):
61+
mapped_warrants = list(map(map_warrant, warrants))
4262
payload = {
4363
"op": op,
44-
"warrants": warrants
64+
"warrants": mapped_warrants
4565
}
4666
json_resp = cls._post(uri="/v2/check", json_payload=payload, opts=opts)
4767
code = json_resp["code"]

warrant/feature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from warrant import APIResource, Subject, Warrant, Object, constants, ListResult
2-
from typing import Any, Dict, List, Optional
1+
from warrant import Subject, Warrant, Object, constants, ListResult
2+
from typing import Any, Dict, Optional
33

44

55
class Feature(Object):

warrant/permission.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from warrant import APIResource, Subject, Warrant, Object, constants, ListResult
2-
from typing import Any, Dict, List, Optional
1+
from warrant import Subject, Warrant, Object, constants, ListResult
2+
from typing import Any, Dict, Optional
33

44

55
class Permission(Object):

warrant/pricing_tier.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from warrant import APIResource, Feature, Subject, Warrant, Object, constants, ListResult
2-
from typing import Any, Dict, List, Optional
1+
from warrant import Feature, Subject, Warrant, Object, constants, ListResult
2+
from typing import Any, Dict
33

44

55
class PricingTier(Object):

warrant/role.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from warrant import APIResource, Permission, Subject, Warrant, Object, constants, ListResult
2-
from typing import Any, Dict, List, Optional
1+
from warrant import Permission, Subject, Warrant, Object, constants, ListResult
2+
from typing import Any, Dict, Optional
33

44

55
class Role(Object):

warrant/tenant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from warrant import APIResource, PricingTier, Feature, User, Authz, Subject, Warrant, Object, ListResult
1+
from warrant import PricingTier, Feature, User, Authz, Subject, Warrant, Object, ListResult
22
from typing import Any, Dict, List, Optional
33

44

warrant/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from warrant import APIResource, PricingTier, Feature, Role, Permission, Authz, Subject, Warrant, Object, constants, ListResult
2-
from typing import Any, Dict, List, Optional, Sequence
1+
from warrant import PricingTier, Feature, Role, Permission, Authz, Subject, Warrant, Object, constants, ListResult
2+
from typing import Any, Dict, List, Optional
33

44

55
class User(Object):

warrant/warrant.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from warrant import APIResource, WarrantException, ListResult
2-
from typing import Any, Dict, List, Optional
1+
from warrant import APIResource, ListResult
2+
from typing import Any, Dict, Optional
33

44

55
class Subject(object):

0 commit comments

Comments
 (0)