|
4 | 4 | __version__ = "0.2.1"
|
5 | 5 |
|
6 | 6 | API_ENDPOINT = "https://api.warrant.dev"
|
| 7 | +SELF_SERVICE_DASHBOARD_BASE_URL = "https://self-serve.warrant.dev" |
7 | 8 |
|
8 | 9 | class WarrantException(Exception):
|
9 | 10 | def __init__(self, msg, status_code=-1):
|
@@ -31,6 +32,11 @@ def __init__(self, warrants, op):
|
31 | 32 | self.warrants = warrants
|
32 | 33 | self.op = op
|
33 | 34 |
|
| 35 | +class PermissionCheck(object): |
| 36 | + def __init__(self, permission_id, user_id): |
| 37 | + self.permission_id = permission_id |
| 38 | + self.user_id = user_id |
| 39 | + |
34 | 40 | class WarrantClient(object):
|
35 | 41 | def __init__(self, api_key):
|
36 | 42 | self._apiKey = api_key
|
@@ -144,16 +150,26 @@ def remove_permission_from_role(self, role_id, permission_id):
|
144 | 150 | raise WarrantException(msg="Must include a roleId and permissionId")
|
145 | 151 | self._make_delete_request(uri="/v1/roles/"+role_id+"/permissions/"+permission_id)
|
146 | 152 |
|
147 |
| - def create_session(self, user_id): |
148 |
| - if user_id == "": |
| 153 | + def create_authorization_session(self, session): |
| 154 | + if session.user_id == "": |
149 | 155 | raise WarrantException(msg="Invalid userId provided")
|
150 |
| - payload = { |
151 |
| - "type": "sess", |
152 |
| - "userId": user_id |
153 |
| - } |
154 |
| - json = self._make_post_request(uri="/v1/sessions", json=payload) |
| 156 | + if session.type != "sess": |
| 157 | + 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) |
155 | 161 | return json['token']
|
156 | 162 |
|
| 163 | + def create_self_service_session(self, session, redirect_url): |
| 164 | + if session.tenant_id == "": |
| 165 | + raise WarrantException(msg="Invalid tenant_id provided") |
| 166 | + if session.user_id == "": |
| 167 | + raise WarrantException(msg="Invalid user_id provided") |
| 168 | + if session.type != "ssdash": |
| 169 | + raise WarrantException(msg="Invalid type provided") |
| 170 | + json = self._make_post_request(uri="/v1/sessions", json=session) |
| 171 | + return f"{SELF_SERVICE_DASHBOARD_BASE_URL}/{json['token']}?redirectUrl={redirect_url}" |
| 172 | + |
157 | 173 | def create_warrant(self, object_type, object_id, relation, subject):
|
158 | 174 | if object_type == "" or object_id == "" or relation == "":
|
159 | 175 | raise WarrantException(msg="Invalid object_type, object_id and/or relation")
|
@@ -193,3 +209,16 @@ def is_authorized(self, warrant_check):
|
193 | 209 | return True
|
194 | 210 | else:
|
195 | 211 | return False
|
| 212 | + |
| 213 | + def has_permission(self, permission_check): |
| 214 | + return self.is_authorized({ |
| 215 | + warrants: [{ |
| 216 | + objectType: "permission", |
| 217 | + objectId: permission_check.permission_id, |
| 218 | + relation: "member", |
| 219 | + subject: { |
| 220 | + objectType: "user", |
| 221 | + objectId: permission_check.user_id |
| 222 | + } |
| 223 | + }] |
| 224 | + }) |
0 commit comments