Skip to content

Commit

Permalink
feat: add authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
cowan-macady committed Jan 20, 2023
1 parent 2982050 commit 961701d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
4 changes: 1 addition & 3 deletions indykite_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ def main():
is_authorized_property_parser = subparsers.add_parser("is_authorized_property")
is_authorized_property_parser.add_argument("property_type", help="Digital Twin Identity Property")
is_authorized_property_parser.add_argument("property_value", help="Digital Twin Identity Property value")
is_authorized_property_parser.add_argument("tenant_id", help="Tenant id (gid)")

args = parser.parse_args()
local = args.local
Expand Down Expand Up @@ -1669,10 +1668,9 @@ def main():
elif command == "is_authorized_property":
property_type = args.property_type #e.g "email"
property_value = args.property_value #e.g [email protected]
tenant_id = args.tenant_id
resources = [IsAuthorizedResource("resourceID", "LabelName"), IsAuthorizedResource("resource2ID", "LabelName")]
actions = ["ACTION"]
is_authorized = client_authorization.is_authorized_property_filter(property_type, property_value, tenant_id,
is_authorized = client_authorization.is_authorized_property_filter(property_type, property_value,
resources=resources, actions=actions)
if is_authorized:
print_response(is_authorized)
Expand Down
5 changes: 2 additions & 3 deletions indykite_sdk/authorization/is_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ def is_authorized_token(self, access_token, resources=[], actions=[]):
return response


def is_authorized_property_filter(self, type_filter, value, tenant_id, resources=[], actions=[]):
def is_authorized_property_filter(self, type_filter, value, resources=[], actions=[]):
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
digital_twin_identifier=pb2_ident.DigitalTwinIdentifier(
property_filter=pb2_ident.PropertyFilter(
type=str(type_filter),
value=pb2_struct.Value(string_value=value),
tenant_id=str(tenant_id)
value=pb2_struct.Value(string_value=value)
)
),
resources=request_resource(resources),
Expand Down
20 changes: 7 additions & 13 deletions tests/test_is_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,11 @@ def test_is_authorized_property_wrong_property():
client = AuthorizationClient()
assert client is not None

type_filter = "email"
type_filter = "phone"
email_value = "[email protected]"
tenant_id = data.get_tenant()
resources = [IsAuthorizedResource("resourceID", "LabelName"), IsAuthorizedResource("resource2ID", "LabelName")]
actions = ["ACTION"]
response = client.is_authorized_property_filter(type_filter, email_value, tenant_id, resources, actions)
response = client.is_authorized_property_filter(type_filter, email_value, resources, actions)
assert response is None


Expand All @@ -154,10 +153,9 @@ def test_is_authorized_property_wrong_resources():

type_filter = "email"
email_value = "[email protected]"
tenant_id = data.get_tenant()
actions = ["ACTION"]
resources = [{"resourceID", "LabelName"}]
response = client.is_authorized_property_filter(type_filter, email_value, tenant_id, resources, actions)
response = client.is_authorized_property_filter(type_filter, email_value, resources, actions)
assert response is None


Expand All @@ -167,14 +165,12 @@ def test_is_authorized_property_success():

type_filter = "email"
email_value = "[email protected]"
tenant_id = data.get_tenant()
resources = [IsAuthorizedResource("resourceID", "LabelName"), IsAuthorizedResource("resource2ID", "LabelName")]
actions = ["ACTION"]
digital_twin_identifier = pb2_ident.DigitalTwinIdentifier(
property_filter=pb2_ident.PropertyFilter(
type=str(type_filter),
value=pb2_struct.Value(string_value=email_value),
tenant_id=str(tenant_id)
value=pb2_struct.Value(string_value=email_value)
)
)

Expand All @@ -183,7 +179,7 @@ def mocked_is_authorized(request: pb2.IsAuthorizedRequest):
return pb2.IsAuthorizedResponse()

client.stub.IsAuthorized = mocked_is_authorized
response = client.is_authorized_property_filter(type_filter, email_value, tenant_id, resources, actions)
response = client.is_authorized_property_filter(type_filter, email_value, resources, actions)
assert response is not None


Expand All @@ -193,14 +189,12 @@ def test_is_authorized_property_empty():

type_filter = "email"
email_value = "[email protected]"
tenant_id = data.get_tenant()
resources = [IsAuthorizedResource("resourceID", "LabelName"), IsAuthorizedResource("resource2ID", "LabelName")]
actions = ["ACTION"]
digital_twin_identifier = pb2_ident.DigitalTwinIdentifier(
property_filter=pb2_ident.PropertyFilter(
type=str(type_filter),
value=pb2_struct.Value(string_value=email_value),
tenant_id=str(tenant_id)
value=pb2_struct.Value(string_value=email_value)
)
)

Expand All @@ -209,5 +203,5 @@ def mocked_is_authorized(request: pb2.IsAuthorizedRequest):
return None

client.stub.IsAuthorized = mocked_is_authorized
response = client.is_authorized_property_filter(type_filter, email_value, tenant_id, resources, actions)
response = client.is_authorized_property_filter(type_filter, email_value, resources, actions)
assert response is None

0 comments on commit 961701d

Please sign in to comment.