Skip to content

Commit f38ebcd

Browse files
authored
Add all methods for roles, permissions, pricing tiers, features and w… (#9)
* Add all methods for roles, permissions, pricing tiers, features and warrants * Add lint + release jobs
1 parent ad79cb0 commit f38ebcd

File tree

14 files changed

+972
-333
lines changed

14 files changed

+972
-333
lines changed

.github/workflows/ci.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Warrant Python
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v3
14+
with:
15+
fetch-depth: 2
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.9'
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install flake8 pytest
24+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
25+
- name: Lint
26+
run: |
27+
flake8 . --max-line-length=200 --extend-ignore=F401

.github/workflows/release.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Warrant Python
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v[0-9]+.[0-9]+.[0-9]+'
7+
jobs:
8+
release:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v3
13+
with:
14+
fetch-depth: 2
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.9'
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install flake8 pytest
23+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
24+
- name: Build distributions
25+
run: |
26+
python -m pip install build
27+
python -m build --sdist --wheel
28+
- name: Publish to PyPI
29+
uses: pypa/gh-action-pypi-publish@release/v1
30+
with:
31+
password: ${{ secrets.PYPI_TOKEN }}

examples/example.py

Lines changed: 212 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,212 @@
1-
from warrant import *
2-
3-
def make_warrant_requests(api_key):
4-
client = WarrantClient(api_key)
5-
6-
# Create users, tenants, roles, permissions
7-
user1 = client.create_user()
8-
print("Created user with generated id: " + user1)
9-
provided_user_id = "custom_user_100"
10-
user2 = client.create_user(provided_user_id)
11-
print("Created user with provided id: " + user2)
12-
tenant1 = client.create_tenant("custom_tenant_210")
13-
print("Created tenant with provided id: " + tenant1)
14-
admin_role = client.create_role("admin1")
15-
print("Created role: " + admin_role)
16-
permission1 = client.create_permission("create_report")
17-
print("Created permission: " + permission1)
18-
permission2 = client.create_permission("delete_report")
19-
print("Created permission: " + permission2)
20-
print("Assigned role " + client.assign_role_to_user(user1, admin_role) + " to user " + user1)
21-
print("Assigned permission " + client.assign_permission_to_user(user1, permission1) + " to user " + user1)
22-
print("Assigned permission " + client.assign_permission_to_role(admin_role, permission2) + " to role " + admin_role)
23-
print("Created authorization session token for user " + user1 + ": " + client.create_authorization_session(AuthorizationSession(user_id=user1)))
24-
print("Created authorization session token for user " + user2 + ": " + client.create_authorization_session(AuthorizationSession(user_id=user2)))
25-
print("Assigned permission " + client.assign_permission_to_user(user2, "view-self-service-dashboard") + " to user " + user2)
26-
print("Created self service session for user " + user2 + ": " + client.create_self_service_session(SelfServiceSession(user_id=user2, tenant_id=tenant1), "http://example.com"))
27-
28-
# Create and test warrants
29-
user1_subject = Subject("user", user1)
30-
print("--- Testing Warrants ---")
31-
print(client.create_warrant(object_type="tenant", object_id=tenant1, relation="member", subject=user1_subject))
32-
subject_to_check = Subject("user", user1)
33-
warrants_to_check = [Warrant("tenant", tenant1, "member", subject_to_check)]
34-
is_authorized = client.is_authorized(WarrantCheck(warrants_to_check, "allOf"))
35-
print(f"Tenant check authorization result: {is_authorized}")
36-
role_warrants_to_check = [Warrant("role", admin_role, "member", subject_to_check)]
37-
role_check = client.is_authorized(WarrantCheck(role_warrants_to_check, "allOf"))
38-
print(f"Admin role check authorization result: {role_check}")
39-
permission_warrants_to_check = [Warrant("permission", permission1, "member", subject_to_check)]
40-
permission_check = client.is_authorized(WarrantCheck(permission_warrants_to_check, "allOf"))
41-
print(f"create_report permission check authorization result: {permission_check}")
42-
role_subject = Subject("role", admin_role)
43-
role_permission_warrants_to_check = [Warrant("permission", permission2, "parent", role_subject)]
44-
role_permission_check = client.is_authorized(WarrantCheck(role_permission_warrants_to_check, "allOf"))
45-
print(f"create_report role/permission check authorization result: {role_permission_check}")
46-
print(f"List all warrants: {client.list_warrants()}")
47-
48-
# Query all warrants for user1
49-
print(f"List all warrants for user1: {client.query_warrants(user1_subject)}")
50-
51-
# Delete users, tenants, roles, permissions
52-
client.remove_permission_from_role(admin_role, permission2)
53-
client.remove_permission_from_user(user1, permission1)
54-
client.remove_role_from_user(user1, admin_role)
55-
client.delete_user(user1)
56-
print("Deleted user " + user1)
57-
client.delete_user(user2)
58-
print("Deleted user " + user2)
59-
client.delete_tenant(tenant1)
60-
print("Deleted tenant " + tenant1)
61-
client.delete_role(admin_role)
62-
print("Deleted role " + admin_role)
63-
client.delete_permission(permission1)
64-
print("Deleted permission " + permission1)
65-
client.delete_permission(permission2)
66-
print("Deleted permission " + permission2)
67-
68-
if __name__ == '__main__':
69-
# Replace with your Warrant api key
70-
api_key = "API_KEY"
71-
make_warrant_requests(api_key)
1+
import warrant
2+
3+
"""
4+
Common usage examples for:
5+
- Users
6+
- Tenants
7+
- Roles, Permissions (RBAC)
8+
- Pricing Tiers, Features
9+
"""
10+
11+
# Replace with your own API key to run example
12+
warrant.api_key = "YOUR_KEY"
13+
14+
"""
15+
Users & Tenants
16+
"""
17+
# Create some users
18+
print("---------- Users & Tenants ----------")
19+
user1 = warrant.User.create()
20+
user2 = warrant.User.create(id="custom_user_id_1")
21+
print(f"Created users: [{user1.id}, {user2.id}]")
22+
23+
# Create tenants & assign the users to them (multitenancy)
24+
tenant1 = warrant.Tenant.create(id="dunder_mifflin")
25+
tenant2 = warrant.Tenant.create(id="big_box_paper")
26+
print(f"Created tenants: [{tenant1.id}, {tenant2.id}]")
27+
tenant1.assign_user(user1.id)
28+
print(f"Assigned user [{user1.id}] to tenant [{tenant1.id}]")
29+
tenant2.assign_user(user2.id)
30+
print(f"Assigned user [{user2.id}] to tenant [{tenant2.id}]")
31+
tenant1_users = ""
32+
for u in tenant1.list_users():
33+
tenant1_users += u.id + " "
34+
print(f"Verify users for [{tenant1.id}]: [{tenant1_users}]")
35+
tenant2_users = ""
36+
for u in tenant2.list_users():
37+
tenant2_users += u.id + " "
38+
print(f"Verify users for [{tenant2.id}]: [{tenant2_users}]")
39+
print("\n")
40+
41+
42+
"""
43+
Roles & Permissions (Role Based Access Control)
44+
"""
45+
# Create roles
46+
print("---------- Role Based Access Control ----------")
47+
admin_role = warrant.Role.create(id="admin1")
48+
viewer_role = warrant.Role.create(id="viewer")
49+
print(f"Created roles: [{admin_role.id}, {viewer_role.id}]")
50+
51+
# Create permissions
52+
create_report_perm = warrant.Permission.create(id="create_report")
53+
delete_report_perm = warrant.Permission.create(id="delete_report")
54+
view_report_perm = warrant.Permission.create(id="view_report")
55+
special_perm = warrant.Permission.create(id="special_perm")
56+
print(f"Created permissions: [{create_report_perm.id}, {delete_report_perm.id}, {view_report_perm.id}, {special_perm.id}]")
57+
58+
# Assign permissions to roles:
59+
# 'create_report', 'delete_report', 'view_report' -> 'admin' role
60+
# 'view_report' -> 'viewer' role
61+
admin_role.assign_permission(create_report_perm.id)
62+
admin_role.assign_permission(delete_report_perm.id)
63+
admin_role.assign_permission(view_report_perm.id)
64+
admin_role_perms = ""
65+
for p in admin_role.list_permissions():
66+
admin_role_perms += p.id + " "
67+
print(f"Assigned permissions to [{admin_role.id}] role: [{admin_role_perms}]")
68+
viewer_role.assign_permission(view_report_perm.id)
69+
viewer_role_perms = ""
70+
for p in viewer_role.list_permissions():
71+
viewer_role_perms += p.id + " "
72+
print(f"Assigned permissions to [{viewer_role.id}] role: [{viewer_role_perms}]")
73+
74+
# Assign roles & permissions to users:
75+
# 'admin' role and 'special_perm' permission -> 'user1'
76+
# 'viewer' role -> 'user2'
77+
user1.assign_role(admin_role.id)
78+
print(f"Assigned role [{admin_role.id}] to user [{user1.id}]")
79+
user1.assign_permission(special_perm.id)
80+
print(f"Assigned permission [{special_perm.id}] to user [{user1.id}]")
81+
user2.assign_role(viewer_role.id)
82+
print(f"Assigned role [{viewer_role.id}] to user [{user2.id}]")
83+
84+
# RBAC checks
85+
print(f"Does user [{user1.id}] have the [{create_report_perm.id}] permission? (should be true) -> {user1.has_permission(create_report_perm.id)}")
86+
print(f"Does user [{user1.id}] have the [{delete_report_perm.id}] permission? (should be true) -> {user1.has_permission(delete_report_perm.id)}")
87+
print(f"Does user [{user1.id}] have the [{view_report_perm.id}] permission? (should be true) -> {user1.has_permission(view_report_perm.id)}")
88+
print(f"Does user [{user1.id}] have the [{special_perm.id}] permission? (should be true) -> {user1.has_permission(special_perm.id)}")
89+
90+
print(f"Does user [{user2.id}] have the [{create_report_perm.id}] permission? (should be false) -> {user2.has_permission(create_report_perm.id)}")
91+
print(f"Does user [{user2.id}] have the [{delete_report_perm.id}] permission? (should be false) -> {user2.has_permission(delete_report_perm.id)}")
92+
print(f"Does user [{user2.id}] have the [{view_report_perm.id}] permission? (should be true) -> {user2.has_permission(view_report_perm.id)}")
93+
print(f"Does user [{user2.id}] have the [{special_perm.id}] permission? (should be false) -> {user2.has_permission(special_perm.id)}")
94+
print("\n")
95+
96+
97+
"""
98+
Pricing Tiers & Features
99+
"""
100+
# Create pricing tiers
101+
print("---------- Pricing Tiers & Features ----------")
102+
enterprise_tier = warrant.PricingTier.create("enterprise")
103+
free_tier = warrant.PricingTier.create("free")
104+
print(f"Created pricing tiers: [{enterprise_tier.id}, {free_tier.id}]")
105+
106+
# Create features
107+
analytics_feature = warrant.Feature.create("analytics")
108+
dashboard_feature = warrant.Feature.create("dashboard")
109+
print(f"Created features: [{analytics_feature.id}, {dashboard_feature.id}]")
110+
111+
# Assign features to pricing tiers:
112+
# 'analytics' feature -> 'enterprise' tier
113+
# 'dashboard' feature -> 'free' tier
114+
enterprise_tier.assign_feature(analytics_feature.id)
115+
enterprise_tier_features = ""
116+
for f in enterprise_tier.list_features():
117+
enterprise_tier_features += f.id + " "
118+
print(f"Assigned features to [{enterprise_tier.id}] tier: [{enterprise_tier_features}]")
119+
free_tier.assign_feature(dashboard_feature.id)
120+
free_tier_features = ""
121+
for f in free_tier.list_features():
122+
free_tier_features += f.id + " "
123+
print(f"Assigned features to [{free_tier.id}] tier: [{free_tier_features}]")
124+
125+
# Assign tiers to users:
126+
# 'enterprise' tier -> 'user1'
127+
# 'free' tier -> 'user2'
128+
user1.assign_pricing_tier(enterprise_tier.id)
129+
print(f"Assigned tier [{enterprise_tier.id}] to user [{user1.id}]")
130+
user2.assign_pricing_tier(free_tier.id)
131+
print(f"Assigned tier [{free_tier.id}] to user [{user2.id}]")
132+
133+
# Pricing tiers checks
134+
print(f"Does [{user1.id}] have access to the [{analytics_feature.id}] feature? (should be true) -> {user1.has_feature(analytics_feature.id)}")
135+
print(f"Does [{user1.id}] have access to the [{dashboard_feature.id}] feature? (should be false) -> {user1.has_feature(dashboard_feature.id)}")
136+
print(f"Does [{user2.id}] have access to the [{analytics_feature.id}] feature? (should be false) -> {user2.has_feature(analytics_feature.id)}")
137+
print(f"Does [{user2.id}] have access to the [{dashboard_feature.id}] feature? (should be true) -> {user2.has_feature(dashboard_feature.id)}")
138+
print("\n")
139+
140+
141+
"""
142+
Create authz sessions (for FE use)
143+
"""
144+
# Generate a self-service dashboard url for user2
145+
print("---------- FE & Self-service Authz Tokens ----------")
146+
user2.assign_permission("view-self-service-dashboard")
147+
print("Created self service dashboard url for user [" + user2.id + "]: " + warrant.Authz.create_self_service_url(tenant_id=tenant1.id, user_id=user2.id, redirect_url="http://example.com"))
148+
149+
# Authz sessions
150+
print("Created authorization session token for user [" + user1.id + "]: " + warrant.Authz.create_authorization_session(user_id=user1.id))
151+
print("Created authorization session token for user [" + user2.id + "]: " + warrant.Authz.create_authorization_session(user_id=user2.id))
152+
print("\n")
153+
154+
155+
"""
156+
Create and query your own warrants
157+
"""
158+
print("---------- Create & Query Warrants ----------")
159+
user1_subject = warrant.Subject("user", user1.id)
160+
result = warrant.Authz.check("permission", "view-self-service-dashboard", "member", user1_subject)
161+
print(f"Does [{user1.id}] have the [view-self-service-dashboard] permission? (should be false) -> {result}")
162+
warrant.Warrant.create("permission", "view-self-service-dashboard", "member", user1_subject)
163+
print("Manually assigned [view-self-service-dashboard] permission to [" + user1.id + "]")
164+
result = warrant.Authz.check("permission", "view-self-service-dashboard", "member", user1_subject)
165+
print(f"Does [{user1.id}] have the [view-self-service-dashboard] permission? (should be true) -> {result}")
166+
167+
# Query warrants
168+
warrants = warrant.Warrant.query(select="explicit warrants", for_clause="subject=user:"+user1.id, where="relation=member")
169+
print("Query warrants results:")
170+
for w in warrants:
171+
print(f"[{w.object_type}:{w.object_id} {w.relation} {w.subject.object_type}:{w.subject.object_id}]")
172+
173+
warrant.Warrant.delete("permission", "view-self-service-dashboard", "member", user1_subject)
174+
print("Manually removed [view-self-service-dashboard] permission from [" + user1.id + "]")
175+
result = warrant.Authz.check("permission", "view-self-service-dashboard", "member", user1_subject)
176+
print(f"Does [{user1.id}] have the [view-self-service-dashboard] permission? (should be false) -> {result}")
177+
print("\n")
178+
179+
180+
"""
181+
Cleanup
182+
"""
183+
# Remove associations (not explicitly required if deleting objects, shown for completeness)
184+
print("Cleaning up...")
185+
user1.remove_permission(special_perm.id)
186+
user1.remove_role(admin_role.id)
187+
user2.remove_role(viewer_role.id)
188+
user2.remove_permission("view-self-service-dashboard")
189+
admin_role.remove_permission(create_report_perm.id)
190+
admin_role.remove_permission(delete_report_perm.id)
191+
admin_role.remove_permission(view_report_perm.id)
192+
viewer_role.remove_permission(view_report_perm.id)
193+
tenant1.remove_user(user1.id)
194+
tenant2.remove_user(user2.id)
195+
enterprise_tier.remove_feature(analytics_feature.id)
196+
free_tier.remove_feature(dashboard_feature.id)
197+
198+
warrant.User.delete(user1.id)
199+
warrant.User.delete(user2.id)
200+
warrant.Tenant.delete(tenant1.id)
201+
warrant.Tenant.delete(tenant2.id)
202+
warrant.Role.delete(admin_role.id)
203+
warrant.Role.delete(viewer_role.id)
204+
warrant.Permission.delete(create_report_perm.id)
205+
warrant.Permission.delete(delete_report_perm.id)
206+
warrant.Permission.delete(view_report_perm.id)
207+
warrant.Permission.delete(special_perm.id)
208+
warrant.Feature.delete(analytics_feature.id)
209+
warrant.Feature.delete(dashboard_feature.id)
210+
warrant.PricingTier.delete(enterprise_tier.id)
211+
warrant.PricingTier.delete(free_tier.id)
212+
print("Done")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="warrant-python",
9-
version="0.3.0",
9+
version="1.0.0",
1010
description="Python SDK for Warrant",
1111
long_description=README,
1212
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)