scim2-client 0.1.4__py3-none-any.whl → 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scim2_client/__init__.py +12 -2
- scim2_client/client.py +282 -101
- scim2_client/errors.py +97 -29
- {scim2_client-0.1.4.dist-info → scim2_client-0.1.5.dist-info}/METADATA +17 -2
- scim2_client-0.1.5.dist-info/RECORD +6 -0
- scim2_client-0.1.4.dist-info/RECORD +0 -6
- {scim2_client-0.1.4.dist-info → scim2_client-0.1.5.dist-info}/WHEEL +0 -0
scim2_client/__init__.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
from .client import SCIMClient
|
|
2
|
+
from .errors import RequestNetworkError
|
|
3
|
+
from .errors import RequestPayloadValidationError
|
|
4
|
+
from .errors import ResponsePayloadValidationError
|
|
2
5
|
from .errors import SCIMClientError
|
|
6
|
+
from .errors import SCIMRequestError
|
|
7
|
+
from .errors import SCIMResponseError
|
|
3
8
|
from .errors import UnexpectedContentFormat
|
|
4
9
|
from .errors import UnexpectedContentType
|
|
5
10
|
from .errors import UnexpectedStatusCode
|
|
@@ -7,7 +12,12 @@ from .errors import UnexpectedStatusCode
|
|
|
7
12
|
__all__ = [
|
|
8
13
|
"SCIMClient",
|
|
9
14
|
"SCIMClientError",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
15
|
+
"SCIMRequestError",
|
|
16
|
+
"SCIMResponseError",
|
|
12
17
|
"UnexpectedContentFormat",
|
|
18
|
+
"UnexpectedContentType",
|
|
19
|
+
"UnexpectedStatusCode",
|
|
20
|
+
"RequestPayloadValidationError",
|
|
21
|
+
"RequestNetworkError",
|
|
22
|
+
"ResponsePayloadValidationError",
|
|
13
23
|
]
|
scim2_client/client.py
CHANGED
|
@@ -8,6 +8,7 @@ from typing import Type
|
|
|
8
8
|
from typing import Union
|
|
9
9
|
|
|
10
10
|
from httpx import Client
|
|
11
|
+
from httpx import RequestError
|
|
11
12
|
from httpx import Response
|
|
12
13
|
from pydantic import ValidationError
|
|
13
14
|
from scim2_models import AnyResource
|
|
@@ -16,9 +17,17 @@ from scim2_models import Error
|
|
|
16
17
|
from scim2_models import ListResponse
|
|
17
18
|
from scim2_models import PatchOp
|
|
18
19
|
from scim2_models import Resource
|
|
20
|
+
from scim2_models import ResourceType
|
|
21
|
+
from scim2_models import Schema
|
|
19
22
|
from scim2_models import SearchRequest
|
|
23
|
+
from scim2_models import ServiceProviderConfig
|
|
20
24
|
|
|
25
|
+
from .errors import RequestNetworkError
|
|
26
|
+
from .errors import RequestPayloadValidationError
|
|
27
|
+
from .errors import ResponsePayloadValidationError
|
|
21
28
|
from .errors import SCIMClientError
|
|
29
|
+
from .errors import SCIMRequestError
|
|
30
|
+
from .errors import SCIMResponseError
|
|
22
31
|
from .errors import UnexpectedContentFormat
|
|
23
32
|
from .errors import UnexpectedContentType
|
|
24
33
|
from .errors import UnexpectedStatusCode
|
|
@@ -30,7 +39,16 @@ BASE_HEADERS = {
|
|
|
30
39
|
|
|
31
40
|
|
|
32
41
|
class SCIMClient:
|
|
33
|
-
"""An object that perform SCIM requests and validate responses.
|
|
42
|
+
"""An object that perform SCIM requests and validate responses.
|
|
43
|
+
|
|
44
|
+
:param client: A :class:`httpx.Client` instance that will be used to send requests.
|
|
45
|
+
:param resource_types: A tuple of :class:`~scim2_models.Resource` types expected to be handled by the SCIMClient.
|
|
46
|
+
If a request payload describe a resource that is not in this list, an exception will be raised.
|
|
47
|
+
|
|
48
|
+
.. note::
|
|
49
|
+
|
|
50
|
+
:class:`~scim2_models.ResourceType`, :class:`~scim2_models.Schema` and :class:`scim2_models.ServiceProviderConfig` are pre-loaded by default.
|
|
51
|
+
"""
|
|
34
52
|
|
|
35
53
|
CREATION_RESPONSE_STATUS_CODES: List[int] = [
|
|
36
54
|
201,
|
|
@@ -99,13 +117,22 @@ class SCIMClient:
|
|
|
99
117
|
|
|
100
118
|
def __init__(self, client: Client, resource_types: Optional[Tuple[Type]] = None):
|
|
101
119
|
self.client = client
|
|
102
|
-
self.resource_types =
|
|
120
|
+
self.resource_types = tuple(
|
|
121
|
+
set(resource_types or []) | {ResourceType, Schema, ServiceProviderConfig}
|
|
122
|
+
)
|
|
103
123
|
|
|
104
124
|
def check_resource_type(self, resource_type):
|
|
105
125
|
if resource_type not in self.resource_types:
|
|
106
|
-
raise
|
|
126
|
+
raise SCIMRequestError(f"Unknown resource type: '{resource_type}'")
|
|
107
127
|
|
|
108
128
|
def resource_endpoint(self, resource_type: Type):
|
|
129
|
+
if resource_type is None:
|
|
130
|
+
return "/"
|
|
131
|
+
|
|
132
|
+
# This one takes no final 's'
|
|
133
|
+
if resource_type is ServiceProviderConfig:
|
|
134
|
+
return "/ServiceProviderConfig"
|
|
135
|
+
|
|
109
136
|
try:
|
|
110
137
|
first_bracket_index = resource_type.__name__.index("[")
|
|
111
138
|
root_name = resource_type.__name__[:first_bracket_index]
|
|
@@ -117,11 +144,12 @@ class SCIMClient:
|
|
|
117
144
|
self,
|
|
118
145
|
response: Response,
|
|
119
146
|
expected_status_codes: List[int],
|
|
120
|
-
|
|
147
|
+
expected_types: Optional[Type] = None,
|
|
148
|
+
check_response_payload: bool = True,
|
|
121
149
|
scim_ctx: Optional[Context] = None,
|
|
122
150
|
):
|
|
123
151
|
if expected_status_codes and response.status_code not in expected_status_codes:
|
|
124
|
-
raise UnexpectedStatusCode(response)
|
|
152
|
+
raise UnexpectedStatusCode(source=response)
|
|
125
153
|
|
|
126
154
|
# Interoperability considerations: The "application/scim+json" media
|
|
127
155
|
# type is intended to identify JSON structure data that conforms to
|
|
@@ -131,7 +159,7 @@ class SCIMClient:
|
|
|
131
159
|
|
|
132
160
|
expected_response_content_types = ("application/scim+json", "application/json")
|
|
133
161
|
if response.headers.get("content-type") not in expected_response_content_types:
|
|
134
|
-
raise UnexpectedContentType(response)
|
|
162
|
+
raise UnexpectedContentType(source=response)
|
|
135
163
|
|
|
136
164
|
# In addition to returning an HTTP response code, implementers MUST return
|
|
137
165
|
# the errors in the body of the response in a JSON format
|
|
@@ -145,21 +173,42 @@ class SCIMClient:
|
|
|
145
173
|
try:
|
|
146
174
|
response_payload = response.json()
|
|
147
175
|
except json.decoder.JSONDecodeError as exc:
|
|
148
|
-
raise UnexpectedContentFormat(response) from exc
|
|
176
|
+
raise UnexpectedContentFormat(source=response) from exc
|
|
177
|
+
|
|
178
|
+
if not check_response_payload:
|
|
179
|
+
return response_payload
|
|
149
180
|
|
|
150
181
|
try:
|
|
151
182
|
return Error.model_validate(response_payload)
|
|
152
183
|
except ValidationError:
|
|
153
184
|
pass
|
|
154
185
|
|
|
155
|
-
if
|
|
186
|
+
if not expected_types:
|
|
187
|
+
return response_payload
|
|
188
|
+
|
|
189
|
+
actual_type = Resource.get_by_payload(
|
|
190
|
+
expected_types, response_payload, with_extensions=False
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
if not actual_type:
|
|
194
|
+
expected = ", ".join([type.__name__ for type in expected_types])
|
|
156
195
|
try:
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
196
|
+
schema = ", ".join(response_payload["schemas"])
|
|
197
|
+
message = f"Expected type {expected} but got unknow resource with schemas: {schema}"
|
|
198
|
+
except KeyError:
|
|
199
|
+
message = (
|
|
200
|
+
f"Expected type {expected} but got undefined object with no schema"
|
|
201
|
+
)
|
|
161
202
|
|
|
162
|
-
|
|
203
|
+
raise SCIMResponseError(message, source=response)
|
|
204
|
+
|
|
205
|
+
try:
|
|
206
|
+
return actual_type.model_validate(response_payload, scim_ctx=scim_ctx)
|
|
207
|
+
except ValidationError as exc:
|
|
208
|
+
scim_exc = ResponsePayloadValidationError(source=response)
|
|
209
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
210
|
+
scim_exc.add_note(str(exc))
|
|
211
|
+
raise scim_exc from exc
|
|
163
212
|
|
|
164
213
|
def create(
|
|
165
214
|
self,
|
|
@@ -184,7 +233,24 @@ class SCIMClient:
|
|
|
184
233
|
|
|
185
234
|
:return:
|
|
186
235
|
- An :class:`~scim2_models.Error` object in case of error.
|
|
187
|
-
- The created object as returned by the server in case of success
|
|
236
|
+
- The created object as returned by the server in case of success and :code:`check_response_payload` is :data:`True`.
|
|
237
|
+
- The created object payload as returned by the server in case of success and :code:`check_response_payload` is :data:`False`.
|
|
238
|
+
|
|
239
|
+
.. code-block:: python
|
|
240
|
+
:caption: Creation of a `User` resource
|
|
241
|
+
|
|
242
|
+
from scim2_models import User
|
|
243
|
+
|
|
244
|
+
request = User(user_name="bjensen@example.com")
|
|
245
|
+
response = scim.create(request)
|
|
246
|
+
# 'response' may be a User or an Error object
|
|
247
|
+
|
|
248
|
+
.. tip::
|
|
249
|
+
|
|
250
|
+
Check the :attr:`~scim2_models.Context.RESOURCE_CREATION_REQUEST`
|
|
251
|
+
and :attr:`~scim2_models.Context.RESOURCE_CREATION_RESPONSE` contexts to understand
|
|
252
|
+
which value will excluded from the request payload, and which values are expected in
|
|
253
|
+
the response payload.
|
|
188
254
|
"""
|
|
189
255
|
|
|
190
256
|
if not check_request_payload:
|
|
@@ -198,30 +264,43 @@ class SCIMClient:
|
|
|
198
264
|
else:
|
|
199
265
|
resource_type = Resource.get_by_payload(self.resource_types, resource)
|
|
200
266
|
if not resource_type:
|
|
201
|
-
raise
|
|
202
|
-
|
|
267
|
+
raise SCIMRequestError(
|
|
268
|
+
"Cannot guess resource type from the payload"
|
|
203
269
|
)
|
|
204
270
|
|
|
205
|
-
|
|
271
|
+
try:
|
|
272
|
+
resource = resource_type.model_validate(resource)
|
|
273
|
+
except ValidationError as exc:
|
|
274
|
+
scim_exc = RequestPayloadValidationError(source=resource)
|
|
275
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
276
|
+
scim_exc.add_note(str(exc))
|
|
277
|
+
raise scim_exc from exc
|
|
206
278
|
|
|
207
279
|
self.check_resource_type(resource_type)
|
|
208
280
|
url = kwargs.pop("url", self.resource_endpoint(resource_type))
|
|
209
281
|
payload = resource.model_dump(scim_ctx=Context.RESOURCE_CREATION_REQUEST)
|
|
210
282
|
|
|
211
|
-
|
|
283
|
+
try:
|
|
284
|
+
response = self.client.post(url, json=payload, **kwargs)
|
|
285
|
+
except RequestError as exc:
|
|
286
|
+
scim_exc = RequestNetworkError(source=payload)
|
|
287
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
288
|
+
scim_exc.add_note(str(exc))
|
|
289
|
+
raise scim_exc from exc
|
|
212
290
|
|
|
213
291
|
return self.check_response(
|
|
214
|
-
response,
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
else None,
|
|
292
|
+
response=response,
|
|
293
|
+
expected_status_codes=(
|
|
294
|
+
self.CREATION_RESPONSE_STATUS_CODES if check_status_code else None
|
|
295
|
+
),
|
|
296
|
+
expected_types=([resource.__class__] if check_request_payload else None),
|
|
297
|
+
check_response_payload=check_response_payload,
|
|
219
298
|
scim_ctx=Context.RESOURCE_CREATION_RESPONSE,
|
|
220
299
|
)
|
|
221
300
|
|
|
222
301
|
def query(
|
|
223
302
|
self,
|
|
224
|
-
resource_type: Type,
|
|
303
|
+
resource_type: Optional[Type] = None,
|
|
225
304
|
id: Optional[str] = None,
|
|
226
305
|
search_request: Optional[Union[SearchRequest, Dict]] = None,
|
|
227
306
|
check_request_payload: bool = True,
|
|
@@ -249,66 +328,49 @@ class SCIMClient:
|
|
|
249
328
|
- A :class:`~scim2_models.Error` object in case of error.
|
|
250
329
|
- A `resource_type` object in case of success if `id` is not :data:`None`
|
|
251
330
|
- A :class:`~scim2_models.ListResponse[resource_type]` object in case of success if `id` is :data:`None`
|
|
252
|
-
"""
|
|
253
331
|
|
|
254
|
-
|
|
255
|
-
if not check_request_payload:
|
|
256
|
-
payload = search_request
|
|
332
|
+
.. note::
|
|
257
333
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
search_request.model_dump(
|
|
261
|
-
exclude_unset=True,
|
|
262
|
-
scim_ctx=Context.RESOURCE_QUERY_REQUEST,
|
|
263
|
-
)
|
|
264
|
-
if search_request
|
|
265
|
-
else None
|
|
266
|
-
)
|
|
334
|
+
Querying a :class:`~scim2_models.ServiceProviderConfig` will return a
|
|
335
|
+
single object, and not a :class:`~scim2_models.ListResponse`.
|
|
267
336
|
|
|
268
|
-
|
|
269
|
-
expected_type = ListResponse[resource_type]
|
|
270
|
-
url = self.resource_endpoint(resource_type)
|
|
337
|
+
:usage:
|
|
271
338
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
url = self.resource_endpoint(resource_type) + f"/{id}"
|
|
339
|
+
.. code-block:: python
|
|
340
|
+
:caption: Query of a `User` resource knowing its id
|
|
275
341
|
|
|
276
|
-
|
|
277
|
-
return self.check_response(
|
|
278
|
-
response,
|
|
279
|
-
self.QUERY_RESPONSE_STATUS_CODES if check_status_code else None,
|
|
280
|
-
expected_type if check_response_payload else None,
|
|
281
|
-
scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
|
|
282
|
-
)
|
|
342
|
+
from scim2_models import User
|
|
283
343
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
search_request: Optional[SearchRequest] = None,
|
|
287
|
-
check_request_payload: bool = True,
|
|
288
|
-
check_response_payload: bool = True,
|
|
289
|
-
check_status_code: bool = True,
|
|
290
|
-
**kwargs,
|
|
291
|
-
) -> Union[AnyResource, ListResponse[AnyResource], Error, Dict]:
|
|
292
|
-
"""Perform a GET request to read all available resources, as defined in
|
|
293
|
-
:rfc:`RFC7644 §3.4.2.1 <7644#section-3.4.2.1>`.
|
|
344
|
+
response = scim.query(User, "my-user-id)
|
|
345
|
+
# 'response' may be a User or an Error object
|
|
294
346
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
:code:`search_request` is expected to be a dict that will be passed as-is in the request.
|
|
298
|
-
:param check_response_payload: Whether to validate that the response payload is valid.
|
|
299
|
-
If set, the raw payload will be returned.
|
|
300
|
-
:param check_status_code: Whether to validate that the response status code is valid.
|
|
301
|
-
:param kwargs: Additional parameters passed to the underlying
|
|
302
|
-
HTTP request library.
|
|
347
|
+
.. code-block:: python
|
|
348
|
+
:caption: Query of all the `User` resources filtering the ones with `userName` starts with `john`
|
|
303
349
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
350
|
+
from scim2_models import User, SearchRequest
|
|
351
|
+
|
|
352
|
+
req = SearchRequest(filter='userName sw "john"')
|
|
353
|
+
response = scim.query(User, search_request=search_request)
|
|
354
|
+
# 'response' may be a ListResponse[User] or an Error object
|
|
355
|
+
|
|
356
|
+
.. code-block:: python
|
|
357
|
+
:caption: Query of all the available resources
|
|
358
|
+
|
|
359
|
+
from scim2_models import User, SearchRequest
|
|
360
|
+
|
|
361
|
+
response = scim.query()
|
|
362
|
+
# 'response' may be a ListResponse[Union[User, Group, ...]] or an Error object
|
|
363
|
+
|
|
364
|
+
.. tip::
|
|
365
|
+
|
|
366
|
+
Check the :attr:`~scim2_models.Context.RESOURCE_QUERY_REQUEST`
|
|
367
|
+
and :attr:`~scim2_models.Context.RESOURCE_QUERY_RESPONSE` contexts to understand
|
|
368
|
+
which value will excluded from the request payload, and which values are expected in
|
|
369
|
+
the response payload.
|
|
307
370
|
"""
|
|
308
371
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
# https://datatracker.ietf.org/doc/html/rfc7644.html#section-3.4.2.1
|
|
372
|
+
if resource_type and check_request_payload:
|
|
373
|
+
self.check_resource_type(resource_type)
|
|
312
374
|
|
|
313
375
|
if not check_request_payload:
|
|
314
376
|
payload = search_request
|
|
@@ -316,20 +378,48 @@ class SCIMClient:
|
|
|
316
378
|
else:
|
|
317
379
|
payload = (
|
|
318
380
|
search_request.model_dump(
|
|
319
|
-
exclude_unset=True,
|
|
381
|
+
exclude_unset=True,
|
|
382
|
+
scim_ctx=Context.RESOURCE_QUERY_REQUEST,
|
|
320
383
|
)
|
|
321
384
|
if search_request
|
|
322
385
|
else None
|
|
323
386
|
)
|
|
324
387
|
|
|
325
|
-
|
|
388
|
+
url = kwargs.pop("url", self.resource_endpoint(resource_type))
|
|
389
|
+
|
|
390
|
+
if resource_type is None:
|
|
391
|
+
expected_types = [
|
|
392
|
+
*self.resource_types,
|
|
393
|
+
ListResponse[Union[self.resource_types]],
|
|
394
|
+
]
|
|
395
|
+
|
|
396
|
+
elif resource_type == ServiceProviderConfig:
|
|
397
|
+
expected_types = [resource_type]
|
|
398
|
+
if id:
|
|
399
|
+
raise SCIMClientError("ServiceProviderConfig cannot have an id")
|
|
400
|
+
|
|
401
|
+
elif id:
|
|
402
|
+
expected_types = [resource_type]
|
|
403
|
+
url = f"{url}/{id}"
|
|
404
|
+
|
|
405
|
+
else:
|
|
406
|
+
expected_types = [ListResponse[resource_type]]
|
|
407
|
+
|
|
408
|
+
try:
|
|
409
|
+
response = self.client.get(url, params=payload, **kwargs)
|
|
410
|
+
except RequestError as exc:
|
|
411
|
+
scim_exc = RequestNetworkError(source=payload)
|
|
412
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
413
|
+
scim_exc.add_note(str(exc))
|
|
414
|
+
raise scim_exc from exc
|
|
326
415
|
|
|
327
416
|
return self.check_response(
|
|
328
|
-
response,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
417
|
+
response=response,
|
|
418
|
+
expected_status_codes=(
|
|
419
|
+
self.QUERY_RESPONSE_STATUS_CODES if check_status_code else None
|
|
420
|
+
),
|
|
421
|
+
expected_types=expected_types,
|
|
422
|
+
check_response_payload=check_response_payload,
|
|
333
423
|
scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
|
|
334
424
|
)
|
|
335
425
|
|
|
@@ -358,6 +448,24 @@ class SCIMClient:
|
|
|
358
448
|
:return:
|
|
359
449
|
- A :class:`~scim2_models.Error` object in case of error.
|
|
360
450
|
- A :class:`~scim2_models.ListResponse[resource_type]` object in case of success.
|
|
451
|
+
|
|
452
|
+
:usage:
|
|
453
|
+
|
|
454
|
+
.. code-block:: python
|
|
455
|
+
:caption: Searching for all the resources filtering the ones with `id` contains with `admin`
|
|
456
|
+
|
|
457
|
+
from scim2_models import User, SearchRequest
|
|
458
|
+
|
|
459
|
+
req = SearchRequest(filter='id co "john"')
|
|
460
|
+
response = scim.search(search_request=search_request)
|
|
461
|
+
# 'response' may be a ListResponse[User] or an Error object
|
|
462
|
+
|
|
463
|
+
.. tip::
|
|
464
|
+
|
|
465
|
+
Check the :attr:`~scim2_models.Context.SEARCH_REQUEST`
|
|
466
|
+
and :attr:`~scim2_models.Context.SEARCH_RESPONSE` contexts to understand
|
|
467
|
+
which value will excluded from the request payload, and which values are expected in
|
|
468
|
+
the response payload.
|
|
361
469
|
"""
|
|
362
470
|
|
|
363
471
|
if not check_request_payload:
|
|
@@ -372,23 +480,41 @@ class SCIMClient:
|
|
|
372
480
|
else None
|
|
373
481
|
)
|
|
374
482
|
|
|
375
|
-
|
|
483
|
+
url = kwargs.pop("url", "/.search")
|
|
484
|
+
|
|
485
|
+
try:
|
|
486
|
+
response = self.client.post(url, json=payload)
|
|
487
|
+
except RequestError as exc:
|
|
488
|
+
scim_exc = RequestNetworkError(source=payload)
|
|
489
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
490
|
+
scim_exc.add_note(str(exc))
|
|
491
|
+
raise scim_exc from exc
|
|
376
492
|
|
|
377
493
|
return self.check_response(
|
|
378
|
-
response,
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
494
|
+
response=response,
|
|
495
|
+
expected_status_codes=(
|
|
496
|
+
self.SEARCH_RESPONSE_STATUS_CODES if check_status_code else None
|
|
497
|
+
),
|
|
498
|
+
expected_types=[ListResponse[Union[self.resource_types]]],
|
|
499
|
+
check_response_payload=check_response_payload,
|
|
383
500
|
scim_ctx=Context.RESOURCE_QUERY_RESPONSE,
|
|
384
501
|
)
|
|
385
502
|
|
|
386
503
|
def delete(
|
|
387
|
-
self,
|
|
504
|
+
self,
|
|
505
|
+
resource_type: Type,
|
|
506
|
+
id: str,
|
|
507
|
+
check_response_payload: bool = True,
|
|
508
|
+
check_status_code: bool = True,
|
|
509
|
+
**kwargs,
|
|
388
510
|
) -> Optional[Union[Error, Dict]]:
|
|
389
511
|
"""Perform a DELETE request to create, as defined in :rfc:`RFC7644 §3.6
|
|
390
512
|
<7644#section-3.6>`.
|
|
391
513
|
|
|
514
|
+
:param resource_type: The type of the resource to delete.
|
|
515
|
+
:param id: The type id the resource to delete.
|
|
516
|
+
:param check_response_payload: Whether to validate that the response payload is valid.
|
|
517
|
+
If set, the raw payload will be returned.
|
|
392
518
|
:param check_status_code: Whether to validate that the response status code is valid.
|
|
393
519
|
:param kwargs: Additional parameters passed to the underlying
|
|
394
520
|
HTTP request library.
|
|
@@ -396,14 +522,36 @@ class SCIMClient:
|
|
|
396
522
|
:return:
|
|
397
523
|
- A :class:`~scim2_models.Error` object in case of error.
|
|
398
524
|
- :data:`None` in case of success.
|
|
525
|
+
|
|
526
|
+
:usage:
|
|
527
|
+
|
|
528
|
+
.. code-block:: python
|
|
529
|
+
:caption: Deleting an `User` which `id` is `foobar`
|
|
530
|
+
|
|
531
|
+
from scim2_models import User, SearchRequest
|
|
532
|
+
|
|
533
|
+
response = scim.delete(User, "foobar")
|
|
534
|
+
# 'response' may be None, or an Error object
|
|
399
535
|
"""
|
|
400
536
|
|
|
401
537
|
self.check_resource_type(resource_type)
|
|
402
|
-
|
|
403
|
-
|
|
538
|
+
delete_url = self.resource_endpoint(resource_type) + f"/{id}"
|
|
539
|
+
url = kwargs.pop("url", delete_url)
|
|
540
|
+
|
|
541
|
+
try:
|
|
542
|
+
response = self.client.delete(url, **kwargs)
|
|
543
|
+
except RequestError as exc:
|
|
544
|
+
scim_exc = RequestNetworkError()
|
|
545
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
546
|
+
scim_exc.add_note(str(exc))
|
|
547
|
+
raise scim_exc from exc
|
|
404
548
|
|
|
405
549
|
return self.check_response(
|
|
406
|
-
response,
|
|
550
|
+
response=response,
|
|
551
|
+
expected_status_codes=(
|
|
552
|
+
self.DELETION_RESPONSE_STATUS_CODES if check_status_code else None
|
|
553
|
+
),
|
|
554
|
+
check_response_payload=check_response_payload,
|
|
407
555
|
)
|
|
408
556
|
|
|
409
557
|
def replace(
|
|
@@ -430,6 +578,25 @@ class SCIMClient:
|
|
|
430
578
|
:return:
|
|
431
579
|
- An :class:`~scim2_models.Error` object in case of error.
|
|
432
580
|
- The updated object as returned by the server in case of success.
|
|
581
|
+
|
|
582
|
+
:usage:
|
|
583
|
+
|
|
584
|
+
.. code-block:: python
|
|
585
|
+
:caption: Replacement of a `User` resource
|
|
586
|
+
|
|
587
|
+
from scim2_models import User
|
|
588
|
+
|
|
589
|
+
user = scim.query(User, "my-used-id")
|
|
590
|
+
user.display_name = "Fancy New Name"
|
|
591
|
+
response = scim.update(user)
|
|
592
|
+
# 'response' may be a User or an Error object
|
|
593
|
+
|
|
594
|
+
.. tip::
|
|
595
|
+
|
|
596
|
+
Check the :attr:`~scim2_models.Context.RESOURCE_REPLACEMENT_REQUEST`
|
|
597
|
+
and :attr:`~scim2_models.Context.RESOURCE_REPLACEMENT_RESPONSE` contexts to understand
|
|
598
|
+
which value will excluded from the request payload, and which values are expected in
|
|
599
|
+
the response payload.
|
|
433
600
|
"""
|
|
434
601
|
|
|
435
602
|
if not check_request_payload:
|
|
@@ -443,30 +610,44 @@ class SCIMClient:
|
|
|
443
610
|
else:
|
|
444
611
|
resource_type = Resource.get_by_payload(self.resource_types, resource)
|
|
445
612
|
if not resource_type:
|
|
446
|
-
raise
|
|
447
|
-
|
|
613
|
+
raise SCIMRequestError(
|
|
614
|
+
"Cannot guess resource type from the payload",
|
|
615
|
+
source=resource,
|
|
448
616
|
)
|
|
449
617
|
|
|
450
|
-
|
|
618
|
+
try:
|
|
619
|
+
resource = resource_type.model_validate(resource)
|
|
620
|
+
except ValidationError as exc:
|
|
621
|
+
scim_exc = RequestPayloadValidationError(source=resource)
|
|
622
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
623
|
+
scim_exc.add_note(str(exc))
|
|
624
|
+
raise scim_exc from exc
|
|
451
625
|
|
|
452
626
|
self.check_resource_type(resource_type)
|
|
453
627
|
|
|
454
628
|
if not resource.id:
|
|
455
|
-
raise
|
|
629
|
+
raise SCIMRequestError("Resource must have an id", source=resource)
|
|
456
630
|
|
|
457
631
|
payload = resource.model_dump(scim_ctx=Context.RESOURCE_REPLACEMENT_REQUEST)
|
|
458
632
|
url = kwargs.pop(
|
|
459
633
|
"url", self.resource_endpoint(resource.__class__) + f"/{resource.id}"
|
|
460
634
|
)
|
|
461
635
|
|
|
462
|
-
|
|
636
|
+
try:
|
|
637
|
+
response = self.client.put(url, json=payload, **kwargs)
|
|
638
|
+
except RequestError as exc:
|
|
639
|
+
scim_exc = RequestNetworkError(source=payload)
|
|
640
|
+
if hasattr(scim_exc, "add_note"): # pragma: no cover
|
|
641
|
+
scim_exc.add_note(str(exc))
|
|
642
|
+
raise scim_exc from exc
|
|
463
643
|
|
|
464
644
|
return self.check_response(
|
|
465
|
-
response,
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
else None,
|
|
645
|
+
response=response,
|
|
646
|
+
expected_status_codes=(
|
|
647
|
+
self.REPLACEMENT_RESPONSE_STATUS_CODES if check_status_code else None
|
|
648
|
+
),
|
|
649
|
+
expected_types=([resource.__class__] if check_request_payload else None),
|
|
650
|
+
check_response_payload=check_response_payload,
|
|
470
651
|
scim_ctx=Context.RESOURCE_REPLACEMENT_RESPONSE,
|
|
471
652
|
)
|
|
472
653
|
|
scim2_client/errors.py
CHANGED
|
@@ -1,43 +1,111 @@
|
|
|
1
|
-
from
|
|
1
|
+
from typing import Any
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class SCIMClientError(Exception):
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
"""Base exception for scim2-client.
|
|
6
|
+
|
|
7
|
+
:param message: The exception reason.
|
|
8
|
+
:param source: The request payload or the response object that have
|
|
9
|
+
caused the exception.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, message: str, source: Any = None, *args, **kwargs):
|
|
13
|
+
self.message = message
|
|
14
|
+
self.source = source
|
|
7
15
|
super().__init__(*args, **kwargs)
|
|
8
16
|
|
|
17
|
+
def __str__(self):
|
|
18
|
+
return self.message or "UNKNOWN"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class SCIMRequestError(SCIMClientError):
|
|
22
|
+
"""Base exception for errors happening during request payload building."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class RequestNetworkError(SCIMRequestError):
|
|
26
|
+
"""Error raised when a network error happened during request.
|
|
27
|
+
|
|
28
|
+
This error is raised when a :class:`httpx.RequestError` has been catched while performing a request.
|
|
29
|
+
The original :class:`~httpx.RequestError` is available with :attr:`~BaseException.__cause__`.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, *args, **kwargs):
|
|
33
|
+
message = kwargs.pop("message", "Network error happened during request")
|
|
34
|
+
super().__init__(message, *args, **kwargs)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RequestPayloadValidationError(SCIMRequestError):
|
|
38
|
+
"""Error raised when an invalid request payload has been passed to
|
|
39
|
+
SCIMClient.
|
|
40
|
+
|
|
41
|
+
This error is raised when a :class:`pydantic.ValidationError` has been catched
|
|
42
|
+
while validating the client request payload.
|
|
43
|
+
The original :class:`~pydantic.ValidationError` is available with :attr:`~BaseException.__cause__`.
|
|
44
|
+
|
|
45
|
+
.. code-block:: python
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
scim.create({"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], "active": "not-a-bool"})
|
|
49
|
+
except RequestPayloadValidationError as exc:
|
|
50
|
+
print("Original validation error cause", exc.__cause__)
|
|
51
|
+
"""
|
|
9
52
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
53
|
+
def __init__(self, *args, **kwargs):
|
|
54
|
+
message = kwargs.pop("message", "Server response payload validation error")
|
|
55
|
+
super().__init__(message, *args, **kwargs)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class SCIMResponseError(SCIMClientError):
|
|
59
|
+
"""Base exception for errors happening during response payload
|
|
60
|
+
validation."""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class UnexpectedStatusCode(SCIMResponseError):
|
|
64
|
+
"""Error raised when a server returned an unexpected status code for a
|
|
65
|
+
given :class:`~scim2_models.Context`."""
|
|
66
|
+
|
|
67
|
+
def __init__(self, *args, **kwargs):
|
|
17
68
|
message = kwargs.pop(
|
|
18
|
-
"message",
|
|
69
|
+
"message",
|
|
70
|
+
f"Unexpected response status code: {kwargs['source'].status_code}",
|
|
19
71
|
)
|
|
20
|
-
super().__init__(
|
|
72
|
+
super().__init__(message, *args, **kwargs)
|
|
73
|
+
|
|
21
74
|
|
|
75
|
+
class UnexpectedContentType(SCIMResponseError):
|
|
76
|
+
"""Error raised when a server returned an unexpected `Content-Type` header
|
|
77
|
+
in a response."""
|
|
22
78
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
self,
|
|
26
|
-
response: Response,
|
|
27
|
-
*args,
|
|
28
|
-
**kwargs,
|
|
29
|
-
):
|
|
30
|
-
content_type = response.headers.get("content-type", "")
|
|
79
|
+
def __init__(self, *args, **kwargs):
|
|
80
|
+
content_type = kwargs["source"].headers.get("content-type", "")
|
|
31
81
|
message = kwargs.pop("message", f"Unexpected content type: {content_type}")
|
|
32
|
-
super().__init__(
|
|
82
|
+
super().__init__(message, *args, **kwargs)
|
|
83
|
+
|
|
33
84
|
|
|
85
|
+
class UnexpectedContentFormat(SCIMResponseError):
|
|
86
|
+
"""Error raised when a server returned a response in a non-JSON format."""
|
|
34
87
|
|
|
35
|
-
|
|
36
|
-
def __init__(
|
|
37
|
-
self,
|
|
38
|
-
response: Response,
|
|
39
|
-
*args,
|
|
40
|
-
**kwargs,
|
|
41
|
-
):
|
|
88
|
+
def __init__(self, *args, **kwargs):
|
|
42
89
|
message = kwargs.pop("message", "Unexpected response content format")
|
|
43
|
-
super().__init__(
|
|
90
|
+
super().__init__(message, *args, **kwargs)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class ResponsePayloadValidationError(SCIMResponseError):
|
|
94
|
+
"""Error raised when the server returned a payload that cannot be
|
|
95
|
+
validated.
|
|
96
|
+
|
|
97
|
+
This error is raised when a :class:`pydantic.ValidationError` has been catched
|
|
98
|
+
while validating the server response payload.
|
|
99
|
+
The original :class:`~pydantic.ValidationError` is available with :attr:`~BaseException.__cause__`.
|
|
100
|
+
|
|
101
|
+
.. code-block:: python
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
scim.query(User, "foobar")
|
|
105
|
+
except ResponsePayloadValidationError as exc:
|
|
106
|
+
print("Original validation error cause", exc.__cause__)
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
def __init__(self, *args, **kwargs):
|
|
110
|
+
message = kwargs.pop("message", "Server response payload validation error")
|
|
111
|
+
super().__init__(message, *args, **kwargs)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: scim2-client
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: Pythonically build SCIM requests and parse SCIM responses
|
|
5
5
|
License: MIT
|
|
6
6
|
Keywords: scim,scim2,provisioning,httpx,api
|
|
@@ -20,7 +20,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.12
|
|
21
21
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
22
|
Requires-Dist: httpx (>=0.27.0,<0.28.0)
|
|
23
|
-
Requires-Dist: scim2-models (>=0.1.
|
|
23
|
+
Requires-Dist: scim2-models (>=0.1.5,<0.2.0)
|
|
24
24
|
Description-Content-Type: text/markdown
|
|
25
25
|
|
|
26
26
|
# scim2-client
|
|
@@ -28,6 +28,16 @@ Description-Content-Type: text/markdown
|
|
|
28
28
|
A SCIM client Python library built upon [scim2-models](https://scim2-models.readthedocs.io) and [httpx](https://github.com/encode/httpx),
|
|
29
29
|
that pythonically build requests and parse responses,
|
|
30
30
|
following the [RFC7643](https://datatracker.ietf.org/doc/html/rfc7643.html) and [RFC7644](https://datatracker.ietf.org/doc/html/rfc7644.html) specifications.
|
|
31
|
+
|
|
32
|
+
It aims to be used in SCIM client applications, or in unit tests for SCIM server applications.
|
|
33
|
+
|
|
34
|
+
## What's SCIM anyway?
|
|
35
|
+
|
|
36
|
+
SCIM stands for System for Cross-domain Identity Management, and it is a provisioning protocol.
|
|
37
|
+
Provisioning is the action of managing a set of resources across different services, usually users and groups.
|
|
38
|
+
SCIM is often used between Identity Providers and applications in completion of standards like OAuth2 and OpenID Connect.
|
|
39
|
+
It allows users and groups creations, modifications and deletions to be synchronized between applications.
|
|
40
|
+
|
|
31
41
|
## Installation
|
|
32
42
|
|
|
33
43
|
```shell
|
|
@@ -71,3 +81,8 @@ assert isinstance(response, Error)
|
|
|
71
81
|
assert response.detail == "One or more of the attribute values are already in use or are reserved."
|
|
72
82
|
```
|
|
73
83
|
|
|
84
|
+
scim2-client belongs in a collection of SCIM tools developed by [Yaal Coop](https://yaal.coop),
|
|
85
|
+
with [scim2-models](https://github.com/yaal-coop/scim2-models),
|
|
86
|
+
[scim2-tester](https://github.com/yaal-coop/scim2-tester) and
|
|
87
|
+
[scim2-cli](https://github.com/yaal-coop/scim2-cli)
|
|
88
|
+
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
scim2_client/__init__.py,sha256=xcCAq0aekpnljrYoOBz2LtFAhmKNaNFurdjy8HbQ9ak,705
|
|
2
|
+
scim2_client/client.py,sha256=QTYDK3yM_89w2uneUQayx0dyCGVNAoRItgzNtCtVe7A,25020
|
|
3
|
+
scim2_client/errors.py,sha256=xBpSCTA0pZHanH7SSDGEvdgztc-aWmSHSovFajnS-lI,3961
|
|
4
|
+
scim2_client-0.1.5.dist-info/METADATA,sha256=PVAwcn-xKgYVyFZuzgWyYR6vZWVQLCmHtrzyfcL-r4A,3495
|
|
5
|
+
scim2_client-0.1.5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
6
|
+
scim2_client-0.1.5.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
scim2_client/__init__.py,sha256=2UNsl6HNtVUv5LVcnmLaCHyT4SqAUUFOIWW2r5XGv6A,338
|
|
2
|
-
scim2_client/client.py,sha256=HzsOyb4xqUC05-HuhV18g5abYK-00AY0iCni4-XBEtA,17880
|
|
3
|
-
scim2_client/errors.py,sha256=uOOAwsD8rDrC8BQbwPid051YyWtexTcS8b7i6QC6-CM,1175
|
|
4
|
-
scim2_client-0.1.4.dist-info/METADATA,sha256=khpZeya8xou5elKL0lN9VUC2sMS6KgYFyL690kH7GfA,2662
|
|
5
|
-
scim2_client-0.1.4.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
6
|
-
scim2_client-0.1.4.dist-info/RECORD,,
|
|
File without changes
|