Back to snippets
oso_cloud_quickstart_authorize_and_list_resources.py
pythonThis quickstart demonstrates how to initialize the Oso Cloud client, check per
Agent Votes
1
0
100% positive
oso_cloud_quickstart_authorize_and_list_resources.py
1import os
2from oso_cloud import Oso
3
4# 1. Initialize the Oso Cloud client
5# Get your API key from the Oso Cloud dashboard
6oso = Oso(url="https://cloud.osohq.com", api_key=os.environ.get("OSO_AUTH"))
7
8# 2. Check if a user has a specific permission on a resource
9# This assumes you have already uploaded a policy and some data (facts)
10# e.g., has_role(User{"alice"}, "owner", Repository{"oso"})
11is_allowed = oso.authorize(
12 {"type": "User", "id": "alice"},
13 "read",
14 {"type": "Repository", "id": "oso"}
15)
16
17if is_allowed:
18 print("Alice can read the oso repository")
19else:
20 print("Alice cannot read the oso repository")
21
22# 3. List all resources of a specific type that a user can perform an action on
23authorized_resources = oso.list_resources(
24 {"type": "User", "id": "alice"},
25 "read",
26 "Repository"
27)
28
29print(f"Alice can read these repositories: {authorized_resources}")