@@ -16,14 +16,14 @@ def __init__(self, msg, status_code=-1):
16
16
17
17
class Subject (object ):
18
18
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
21
21
self .relation = relation
22
22
23
23
class Warrant (object ):
24
24
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
27
27
self .relation = relation
28
28
self .subject = subject
29
29
@@ -37,28 +37,39 @@ def __init__(self, permission_id, user_id):
37
37
self .permission_id = permission_id
38
38
self .user_id = user_id
39
39
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
+
40
51
class WarrantClient (object ):
41
52
def __init__ (self , api_key ):
42
- self ._apiKey = api_key
53
+ self ._api_key = api_key
43
54
44
55
def _make_post_request (self , uri , json = {}):
45
- headers = { "Authorization" : "ApiKey " + self ._apiKey }
56
+ headers = { "Authorization" : "ApiKey " + self ._api_key }
46
57
resp = requests .post (url = API_ENDPOINT + uri , headers = headers , json = json )
47
58
if resp .status_code == 200 :
48
59
return resp .json ()
49
60
else :
50
61
raise WarrantException (msg = resp .text , status_code = resp .status_code )
51
62
52
63
def _make_get_request (self , uri , params = {}):
53
- headers = { "Authorization" : "ApiKey " + self ._apiKey }
64
+ headers = { "Authorization" : "ApiKey " + self ._api_key }
54
65
resp = requests .get (url = API_ENDPOINT + uri , headers = headers , params = params )
55
66
if resp .status_code == 200 :
56
67
return resp .json ()
57
68
else :
58
69
raise WarrantException (msg = resp .text , status_code = resp .status_code )
59
70
60
71
def _make_delete_request (self , uri , params = {}):
61
- headers = { "Authorization" : "ApiKey " + self ._apiKey }
72
+ headers = { "Authorization" : "ApiKey " + self ._api_key }
62
73
resp = requests .delete (url = API_ENDPOINT + uri , headers = headers , params = params )
63
74
if resp .status_code != 200 :
64
75
raise WarrantException (msg = resp .text , status_code = resp .status_code )
@@ -75,7 +86,7 @@ def create_user(self, user_id="", email=""):
75
86
76
87
def delete_user (self , user_id ):
77
88
if user_id == "" :
78
- raise WarrantException (msg = "Must include a userId " )
89
+ raise WarrantException (msg = "Must include a user_id " )
79
90
self ._make_delete_request (uri = "/v1/users/" + user_id )
80
91
81
92
def create_tenant (self , tenant_id = "" , name = "" ):
@@ -130,60 +141,66 @@ def remove_role_from_user(self, user_id, role_id):
130
141
131
142
def assign_permission_to_user (self , user_id , permission_id ):
132
143
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 " )
134
145
json = self ._make_post_request (uri = "/v1/users/" + user_id + "/permissions/" + permission_id )
135
146
return json ['permissionId' ]
136
147
137
148
def remove_permission_from_user (self , user_id , permission_id ):
138
149
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 " )
140
151
self ._make_delete_request (uri = "/v1/users/" + user_id + "/permissions/" + permission_id )
141
152
142
153
def assign_permission_to_role (self , role_id , permission_id ):
143
154
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 " )
145
156
json = self ._make_post_request (uri = "/v1/roles/" + role_id + "/permissions/" + permission_id )
146
157
return json ['permissionId' ]
147
158
148
159
def remove_permission_from_role (self , role_id , permission_id ):
149
160
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 " )
151
162
self ._make_delete_request (uri = "/v1/roles/" + role_id + "/permissions/" + permission_id )
152
163
153
164
def create_authorization_session (self , session ):
154
165
if session .user_id == "" :
155
- raise WarrantException (msg = "Invalid userId provided " )
166
+ raise WarrantException (msg = "Must include a user_id " )
156
167
if session .type != "sess" :
157
168
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 )
161
171
return json ['token' ]
162
172
163
173
def create_self_service_session (self , session , redirect_url ):
164
174
if session .tenant_id == "" :
165
- raise WarrantException (msg = "Invalid tenant_id provided " )
175
+ raise WarrantException (msg = "Must include a tenant_id " )
166
176
if session .user_id == "" :
167
- raise WarrantException (msg = "Invalid user_id provided " )
177
+ raise WarrantException (msg = "Must include a user_id " )
168
178
if session .type != "ssdash" :
169
179
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 )
171
184
return f"{ SELF_SERVICE_DASHBOARD_BASE_URL } /{ json ['token' ]} ?redirectUrl={ redirect_url } "
172
185
173
186
def create_warrant (self , object_type , object_id , relation , subject ):
174
187
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" )
176
189
payload = {
177
190
"objectType" : object_type ,
178
191
"objectId" : object_id ,
179
192
"relation" : relation
180
193
}
181
194
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
+ }
183
200
else :
184
201
raise WarrantException (msg = "Invalid type for \' subject\' . Must be of type Subject" )
185
202
resp = self ._make_post_request (uri = "/v1/warrants" , json = payload )
186
- return resp [ 'id' ]
203
+ return resp
187
204
188
205
def list_warrants (self , object_type = "" , object_id = "" , relation = "" , user_id = "" ):
189
206
filters = {
@@ -197,10 +214,22 @@ def list_warrants(self, object_type="", object_id="", relation="", user_id=""):
197
214
198
215
def is_authorized (self , warrant_check ):
199
216
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 )
204
233
if resp .status_code != 200 :
205
234
raise WarrantException (msg = resp .text , status_code = resp .status_code )
206
235
response_payload = resp .json ()
@@ -213,12 +242,12 @@ def is_authorized(self, warrant_check):
213
242
def has_permission (self , permission_check ):
214
243
return self .is_authorized ({
215
244
warrants : [{
216
- objectType : "permission" ,
217
- objectId : permission_check .permission_id ,
245
+ object_type : "permission" ,
246
+ object_id : permission_check .permission_id ,
218
247
relation : "member" ,
219
248
subject : {
220
- objectType : "user" ,
221
- objectId : permission_check .user_id
249
+ object_type : "user" ,
250
+ object_id : permission_check .user_id
222
251
}
223
252
}]
224
253
})
0 commit comments