44__version__ = "0.2.1"
55
66API_ENDPOINT = "https://api.warrant.dev"
7- API_VERSION = "/v1"
87
98class WarrantException (Exception ):
109 def __init__ (self , msg , status_code = - 1 ):
@@ -21,45 +20,134 @@ def __init__(self, object_type, object_id, relation=""):
2120 self .relation = relation
2221
2322class Warrant (object ):
23+ def __init__ (self , object_type , object_id , relation , subject ):
24+ self .objectType = object_type
25+ self .objectId = object_id
26+ self .relation = relation
27+ self .subject = subject
28+
29+ class WarrantCheck (object ):
30+ def __init__ (self , warrants , op ):
31+ self .warrants = warrants
32+ self .op = op
33+
34+ class WarrantClient (object ):
2435 def __init__ (self , api_key ):
2536 self ._apiKey = api_key
2637
2738 def _make_post_request (self , uri , json = {}):
2839 headers = { "Authorization" : "ApiKey " + self ._apiKey }
29- resp = requests .post (url = API_ENDPOINT + API_VERSION + uri , headers = headers , json = json )
40+ resp = requests .post (url = API_ENDPOINT + uri , headers = headers , json = json )
3041 if resp .status_code == 200 :
3142 return resp .json ()
3243 else :
3344 raise WarrantException (msg = resp .text , status_code = resp .status_code )
3445
3546 def _make_get_request (self , uri , params = {}):
3647 headers = { "Authorization" : "ApiKey " + self ._apiKey }
37- resp = requests .get (url = API_ENDPOINT + API_VERSION + uri , headers = headers , params = params )
48+ resp = requests .get (url = API_ENDPOINT + uri , headers = headers , params = params )
3849 if resp .status_code == 200 :
3950 return resp .json ()
4051 else :
4152 raise WarrantException (msg = resp .text , status_code = resp .status_code )
4253
54+ def _make_delete_request (self , uri , params = {}):
55+ headers = { "Authorization" : "ApiKey " + self ._apiKey }
56+ resp = requests .delete (url = API_ENDPOINT + uri , headers = headers , params = params )
57+ if resp .status_code != 200 :
58+ raise WarrantException (msg = resp .text , status_code = resp .status_code )
59+
4360 def create_user (self , user_id = "" ):
4461 if user_id == "" :
4562 payload = {}
4663 else :
4764 payload = { "userId" : user_id }
48- json = self ._make_post_request (uri = "/users" , json = payload )
65+ json = self ._make_post_request (uri = "/v1/ users" , json = payload )
4966 return json ['userId' ]
5067
68+ def delete_user (self , user_id ):
69+ if user_id == "" :
70+ raise WarrantException (msg = "Must include a userId" )
71+ self ._make_delete_request (uri = "/v1/users/" + user_id )
72+
5173 def create_tenant (self , tenant_id = "" ):
5274 if tenant_id == "" :
5375 payload = {}
5476 else :
5577 payload = { "tenantId" : tenant_id }
56- json = self ._make_post_request (uri = "/tenants" , json = payload )
78+ json = self ._make_post_request (uri = "/v1/ tenants" , json = payload )
5779 return json ['tenantId' ]
5880
81+ def delete_tenant (self , tenant_id ):
82+ if tenant_id == "" :
83+ raise WarrantException (msg = "Must include a tenantId" )
84+ self ._make_delete_request (uri = "/v1/tenants/" + tenant_id )
85+
86+ def create_role (self , role_id ):
87+ if role_id == "" :
88+ raise WarrantException (msg = "Must include a roleId" )
89+ payload = { "roleId" : role_id }
90+ json = self ._make_post_request (uri = "/v1/roles" , json = payload )
91+ return json ['roleId' ]
92+
93+ def delete_role (self , role_id ):
94+ if role_id == "" :
95+ raise WarrantException (msg = "Must include a roleId" )
96+ self ._make_delete_request (uri = "/v1/roles/" + role_id )
97+
98+ def create_permission (self , permission_id ):
99+ if permission_id == "" :
100+ raise WarrantException (msg = "Must include a permissionId" )
101+ payload = { "permissionId" : permission_id }
102+ json = self ._make_post_request (uri = "/v1/permissions" , json = payload )
103+ return json ['permissionId' ]
104+
105+ def delete_permission (self , permission_id ):
106+ if permission_id == "" :
107+ raise WarrantException (msg = "Must include a permissionId" )
108+ self ._make_delete_request (uri = "/v1/permissions/" + permission_id )
109+
110+ def assign_role_to_user (self , user_id , role_id ):
111+ if user_id == "" or role_id == "" :
112+ raise WarrantException (msg = "Must include a userId and roleId" )
113+ json = self ._make_post_request (uri = "/v1/users/" + user_id + "/roles/" + role_id )
114+ return json ['roleId' ]
115+
116+ def remove_role_from_user (self , user_id , role_id ):
117+ if user_id == "" or role_id == "" :
118+ raise WarrantException (msg = "Must include a userId and roleId" )
119+ self ._make_delete_request (uri = "/v1/users/" + user_id + "/roles/" + role_id )
120+
121+ def assign_permission_to_user (self , user_id , permission_id ):
122+ if user_id == "" or permission_id == "" :
123+ raise WarrantException (msg = "Must include a userId and permissionId" )
124+ json = self ._make_post_request (uri = "/v1/users/" + user_id + "/permissions/" + permission_id )
125+ return json ['permissionId' ]
126+
127+ def remove_permission_from_user (self , user_id , permission_id ):
128+ if user_id == "" or permission_id == "" :
129+ raise WarrantException (msg = "Must include a userId and permissionId" )
130+ self ._make_delete_request (uri = "/v1/users/" + user_id + "/permissions/" + permission_id )
131+
132+ def assign_permission_to_role (self , role_id , permission_id ):
133+ if role_id == "" or permission_id == "" :
134+ raise WarrantException (msg = "Must include a roleId and permissionId" )
135+ json = self ._make_post_request (uri = "/v1/roles/" + role_id + "/permissions/" + permission_id )
136+ return json ['permissionId' ]
137+
138+ def remove_permission_from_role (self , role_id , permission_id ):
139+ if role_id == "" or permission_id == "" :
140+ raise WarrantException (msg = "Must include a roleId and permissionId" )
141+ self ._make_delete_request (uri = "/v1/roles/" + role_id + "/permissions/" + permission_id )
142+
59143 def create_session (self , user_id ):
60144 if user_id == "" :
61145 raise WarrantException (msg = "Invalid userId provided" )
62- json = self ._make_post_request (uri = "/users/" + user_id + "/sessions" )
146+ payload = {
147+ "type" : "sess" ,
148+ "userId" : user_id
149+ }
150+ json = self ._make_post_request (uri = "/v1/sessions" , json = payload )
63151 return json ['token' ]
64152
65153 def create_warrant (self , object_type , object_id , relation , subject ):
@@ -74,7 +162,7 @@ def create_warrant(self, object_type, object_id, relation, subject):
74162 payload ["subject" ] = subject .__dict__
75163 else :
76164 raise WarrantException (msg = "Invalid type for \' subject\' . Must be of type Subject" )
77- resp = self ._make_post_request (uri = "/warrants" , json = payload )
165+ resp = self ._make_post_request (uri = "/v1/ warrants" , json = payload )
78166 return resp ['id' ]
79167
80168 def list_warrants (self , object_type = "" , object_id = "" , relation = "" , user_id = "" ):
@@ -84,26 +172,20 @@ def list_warrants(self, object_type="", object_id="", relation="", user_id=""):
84172 "relation" : relation ,
85173 "userId" : user_id ,
86174 }
87- resp = self ._make_get_request (uri = "/warrants" , params = filters )
175+ resp = self ._make_get_request (uri = "/v1/ warrants" , params = filters )
88176 return resp
89177
90- def is_authorized (self , object_type , object_id , relation , subject_to_check ):
91- if object_type == "" or object_id == "" or relation == "" :
92- raise WarrantException (msg = "Invalid object_type, object_id and/or relation" )
93- payload = {
94- "objectType" : object_type ,
95- "objectId" : object_id ,
96- "relation" : relation
97- }
98- if isinstance (subject_to_check , Subject ):
99- payload ["subject" ] = subject_to_check .__dict__
100- else :
101- raise WarrantException (msg = "Invalid type for \' subject_to_check\' . Must be of type Subject" )
178+ def is_authorized (self , warrant_check ):
179+ if not isinstance (warrant_check .warrants , list ):
180+ raise WarrantException (msg = "Invalid list of warrants to check" )
181+ payload = json .dumps (warrant_check , default = lambda x : x .__dict__ )
102182 headers = { "Authorization" : "ApiKey " + self ._apiKey }
103- resp = requests .post (url = API_ENDPOINT + API_VERSION + "/authorize" , headers = headers , json = payload )
104- if resp .status_code == 200 :
183+ resp = requests .post (url = API_ENDPOINT + "/v2/authorize" , headers = headers , data = payload )
184+ if resp .status_code != 200 :
185+ raise WarrantException (msg = resp .text , status_code = resp .status_code )
186+ response_payload = resp .json ()
187+ result = response_payload ['code' ]
188+ if result == 200 :
105189 return True
106- elif resp .status_code == 401 :
107- return False
108190 else :
109- raise WarrantException ( msg = resp . text , status_code = resp . status_code )
191+ return False
0 commit comments