Skip to content

Commit e87cae8

Browse files
authored
Update/standardize casing (snake_case vs camelCase) across all methods (#7)
1 parent 2db2a0e commit e87cae8

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

examples/example.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ def make_warrant_requests(api_key):
99
provided_user_id = "custom_user_100"
1010
user2 = client.create_user(provided_user_id)
1111
print("Created user with provided id: " + user2)
12-
print("Created authorization session token for user " + user1 + ": " + client.create_authorization_session({type:"sess", user_id:user1}))
13-
print("Created authorization session token for user " + user2 + ": " + client.create_authorization_session({type:"sess", user_id:user2}))
1412
tenant1 = client.create_tenant("custom_tenant_210")
1513
print("Created tenant with provided id: " + tenant1)
16-
print("Created self service session for user " + user2 + ": " + client.create_self_service_session({type:"ssdash", user_id:user2, tenant_id: tenant1}, "http://example.com"))
1714
admin_role = client.create_role("admin1")
1815
print("Created role: " + admin_role)
1916
permission1 = client.create_permission("create_report")
@@ -23,6 +20,10 @@ def make_warrant_requests(api_key):
2320
print("Assigned role " + client.assign_role_to_user(user1, admin_role) + " to user " + user1)
2421
print("Assigned permission " + client.assign_permission_to_user(user1, permission1) + " to user " + user1)
2522
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"))
2627

2728
# Create and test warrants
2829
user1_subject = Subject("user", user1)

warrant/__init__.py

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ def __init__(self, msg, status_code=-1):
1616

1717
class Subject(object):
1818
def __init__(self, object_type, object_id, relation=""):
19-
self.objectType = object_type
20-
self.objectId = object_id
19+
self.object_type = object_type
20+
self.object_id = object_id
2121
self.relation = relation
2222

2323
class Warrant(object):
2424
def __init__(self, object_type, object_id, relation, subject):
25-
self.objectType = object_type
26-
self.objectId = object_id
25+
self.object_type = object_type
26+
self.object_id = object_id
2727
self.relation = relation
2828
self.subject = subject
2929

@@ -37,28 +37,39 @@ def __init__(self, permission_id, user_id):
3737
self.permission_id = permission_id
3838
self.user_id = user_id
3939

40+
class AuthorizationSession(object):
41+
def __init__(self, user_id):
42+
self.type = "sess"
43+
self.user_id = user_id
44+
45+
class SelfServiceSession(object):
46+
def __init__(self, user_id, tenant_id):
47+
self.type = "ssdash"
48+
self.user_id = user_id
49+
self.tenant_id = tenant_id
50+
4051
class WarrantClient(object):
4152
def __init__(self, api_key):
42-
self._apiKey = api_key
53+
self._api_key = api_key
4354

4455
def _make_post_request(self, uri, json={}):
45-
headers = { "Authorization": "ApiKey " + self._apiKey }
56+
headers = { "Authorization": "ApiKey " + self._api_key }
4657
resp = requests.post(url = API_ENDPOINT+uri, headers = headers, json = json)
4758
if resp.status_code == 200:
4859
return resp.json()
4960
else:
5061
raise WarrantException(msg=resp.text, status_code=resp.status_code)
5162

5263
def _make_get_request(self, uri, params={}):
53-
headers = { "Authorization": "ApiKey " + self._apiKey }
64+
headers = { "Authorization": "ApiKey " + self._api_key }
5465
resp = requests.get(url = API_ENDPOINT+uri, headers = headers, params = params)
5566
if resp.status_code == 200:
5667
return resp.json()
5768
else:
5869
raise WarrantException(msg=resp.text, status_code=resp.status_code)
5970

6071
def _make_delete_request(self, uri, params={}):
61-
headers = { "Authorization": "ApiKey " + self._apiKey }
72+
headers = { "Authorization": "ApiKey " + self._api_key }
6273
resp = requests.delete(url = API_ENDPOINT+uri, headers = headers, params = params)
6374
if resp.status_code != 200:
6475
raise WarrantException(msg=resp.text, status_code=resp.status_code)
@@ -75,7 +86,7 @@ def create_user(self, user_id="", email=""):
7586

7687
def delete_user(self, user_id):
7788
if user_id == "":
78-
raise WarrantException(msg="Must include a userId")
89+
raise WarrantException(msg="Must include a user_id")
7990
self._make_delete_request(uri="/v1/users/"+user_id)
8091

8192
def create_tenant(self, tenant_id="", name=""):
@@ -130,60 +141,66 @@ def remove_role_from_user(self, user_id, role_id):
130141

131142
def assign_permission_to_user(self, user_id, permission_id):
132143
if user_id == "" or permission_id == "":
133-
raise WarrantException(msg="Must include a userId and permissionId")
144+
raise WarrantException(msg="Must include a user_id and permission_id")
134145
json = self._make_post_request(uri="/v1/users/" + user_id + "/permissions/" + permission_id)
135146
return json['permissionId']
136147

137148
def remove_permission_from_user(self, user_id, permission_id):
138149
if user_id == "" or permission_id == "":
139-
raise WarrantException(msg="Must include a userId and permissionId")
150+
raise WarrantException(msg="Must include a user_id and permission_id")
140151
self._make_delete_request(uri="/v1/users/"+user_id+"/permissions/"+permission_id)
141152

142153
def assign_permission_to_role(self, role_id, permission_id):
143154
if role_id == "" or permission_id == "":
144-
raise WarrantException(msg="Must include a roleId and permissionId")
155+
raise WarrantException(msg="Must include a role_id and permission_id")
145156
json = self._make_post_request(uri="/v1/roles/" + role_id + "/permissions/" + permission_id)
146157
return json['permissionId']
147158

148159
def remove_permission_from_role(self, role_id, permission_id):
149160
if role_id == "" or permission_id == "":
150-
raise WarrantException(msg="Must include a roleId and permissionId")
161+
raise WarrantException(msg="Must include a role_id and permission_id")
151162
self._make_delete_request(uri="/v1/roles/"+role_id+"/permissions/"+permission_id)
152163

153164
def create_authorization_session(self, session):
154165
if session.user_id == "":
155-
raise WarrantException(msg="Invalid userId provided")
166+
raise WarrantException(msg="Must include a user_id")
156167
if session.type != "sess":
157168
raise WarrantException(msg="Invalid type provided")
158-
if redirect_url == "":
159-
raise WarrantException(msg="Must include a redirect_url")
160-
json = self._make_post_request(uri="/v1/sessions", json=session)
169+
payload = { "type": session.type, "userId": session.user_id }
170+
json = self._make_post_request(uri="/v1/sessions", json=payload)
161171
return json['token']
162172

163173
def create_self_service_session(self, session, redirect_url):
164174
if session.tenant_id == "":
165-
raise WarrantException(msg="Invalid tenant_id provided")
175+
raise WarrantException(msg="Must include a tenant_id")
166176
if session.user_id == "":
167-
raise WarrantException(msg="Invalid user_id provided")
177+
raise WarrantException(msg="Must include a user_id")
168178
if session.type != "ssdash":
169179
raise WarrantException(msg="Invalid type provided")
170-
json = self._make_post_request(uri="/v1/sessions", json=session)
180+
if redirect_url == "":
181+
raise WarrantException(msg="Must include a redirect_url")
182+
payload = { "type": session.type, "userId": session.user_id, "tenantId": session.tenant_id }
183+
json = self._make_post_request(uri="/v1/sessions", json=payload)
171184
return f"{SELF_SERVICE_DASHBOARD_BASE_URL}/{json['token']}?redirectUrl={redirect_url}"
172185

173186
def create_warrant(self, object_type, object_id, relation, subject):
174187
if object_type == "" or object_id == "" or relation == "":
175-
raise WarrantException(msg="Invalid object_type, object_id and/or relation")
188+
raise WarrantException(msg="Must provide object_type, object_id, and relation")
176189
payload = {
177190
"objectType": object_type,
178191
"objectId": object_id,
179192
"relation": relation
180193
}
181194
if isinstance(subject, Subject):
182-
payload["subject"] = subject.__dict__
195+
payload["subject"] = {
196+
"objectType": subject.object_type,
197+
"objectId": subject.object_id,
198+
"relation": subject.relation
199+
}
183200
else:
184201
raise WarrantException(msg="Invalid type for \'subject\'. Must be of type Subject")
185202
resp = self._make_post_request(uri="/v1/warrants", json=payload)
186-
return resp['id']
203+
return resp
187204

188205
def list_warrants(self, object_type="", object_id="", relation="", user_id=""):
189206
filters = {
@@ -197,10 +214,22 @@ def list_warrants(self, object_type="", object_id="", relation="", user_id=""):
197214

198215
def is_authorized(self, warrant_check):
199216
if not isinstance(warrant_check.warrants, list):
200-
raise WarrantException(msg="Invalid list of warrants to check")
201-
payload = json.dumps(warrant_check, default = lambda x: x.__dict__)
202-
headers = { "Authorization": "ApiKey " + self._apiKey }
203-
resp = requests.post(url = API_ENDPOINT+"/v2/authorize", headers = headers, data=payload)
217+
raise WarrantException(msg="Must provide a list of warrants")
218+
payload = {
219+
"op": warrant_check.op,
220+
"warrants": list(map(lambda wnt: {
221+
"objectType": wnt.object_type,
222+
"objectId": wnt.object_id,
223+
"relation": wnt.relation,
224+
"subject": {
225+
"objectType": wnt.subject.object_type,
226+
"objectId": wnt.subject.object_id,
227+
"relation": wnt.subject.relation
228+
}
229+
}, warrant_check.warrants))
230+
}
231+
headers = { "Authorization": "ApiKey " + self._api_key }
232+
resp = requests.post(url=API_ENDPOINT+"/v2/authorize", headers=headers, json=payload)
204233
if resp.status_code != 200:
205234
raise WarrantException(msg=resp.text, status_code=resp.status_code)
206235
response_payload = resp.json()
@@ -213,12 +242,12 @@ def is_authorized(self, warrant_check):
213242
def has_permission(self, permission_check):
214243
return self.is_authorized({
215244
warrants: [{
216-
objectType: "permission",
217-
objectId: permission_check.permission_id,
245+
object_type: "permission",
246+
object_id: permission_check.permission_id,
218247
relation: "member",
219248
subject: {
220-
objectType: "user",
221-
objectId: permission_check.user_id
249+
object_type: "user",
250+
object_id: permission_check.user_id
222251
}
223252
}]
224253
})

0 commit comments

Comments
 (0)