scim2-server 0.8.8__tar.gz → 0.9.0__tar.gz
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_server-0.8.8 → scim2_server-0.9.0}/PKG-INFO +2 -2
- {scim2_server-0.8.8 → scim2_server-0.9.0}/pyproject.toml +2 -2
- {scim2_server-0.8.8 → scim2_server-0.9.0}/pyproject.toml.orig +2 -2
- scim2_server-0.9.0/scim2_server/mapping/__init__.py +38 -0
- scim2_server-0.9.0/scim2_server/mapping/conversion.py +434 -0
- scim2_server-0.9.0/scim2_server/mapping/declaration.py +100 -0
- scim2_server-0.9.0/scim2_server/mapping/descriptors.py +193 -0
- scim2_server-0.9.0/scim2_server/mapping/filters.py +526 -0
- scim2_server-0.9.0/scim2_server/mapping/mapping.py +873 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/storage.py +14 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/testing.py +66 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/LICENSE +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/README.md +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/__init__.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/applications/__init__.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/applications/asgi.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/applications/base.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/applications/wsgi.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/bulk.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/conditions.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/cursor.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/errors.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/handler.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/keyset.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/memory.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/py.typed +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/requests.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/resources/default-resource-types.json +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/resources/default-schemas.json +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/resources/default-service-provider-config.json +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/responses.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/routing.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/service.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/testserver/__init__.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/testserver/application.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/testserver/cli.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/testserver/oauth.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/testserver/precis.py +0 -0
- {scim2_server-0.8.8 → scim2_server-0.9.0}/scim2_server/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: scim2-server
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9.0
|
|
4
4
|
Summary: SCIM server core for any storage and web framework
|
|
5
5
|
Keywords: scim,scim2,provisioning,rfc7643,rfc7644
|
|
6
6
|
Author: Yaal Coop, Christian Friedrich Coors
|
|
@@ -218,7 +218,7 @@ Classifier: License :: OSI Approved :: Apache Software License
|
|
|
218
218
|
Classifier: Environment :: Web Environment
|
|
219
219
|
Classifier: Programming Language :: Python
|
|
220
220
|
Classifier: Operating System :: OS Independent
|
|
221
|
-
Requires-Dist: scim2-models>=0.12.
|
|
221
|
+
Requires-Dist: scim2-models>=0.12.7
|
|
222
222
|
Requires-Dist: cryptography>=50.0.1 ; extra == 'cursor'
|
|
223
223
|
Requires-Dist: authlib>=1.10.0b1 ; extra == 'oauth'
|
|
224
224
|
Requires-Dist: precis-i18n>=1.1.2 ; extra == 'precis'
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scim2-server"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.9.0"
|
|
8
8
|
description = "SCIM server core for any storage and web framework"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
keywords = [
|
|
@@ -29,7 +29,7 @@ classifiers = [
|
|
|
29
29
|
"Operating System :: OS Independent",
|
|
30
30
|
]
|
|
31
31
|
requires-python = ">= 3.11"
|
|
32
|
-
dependencies = ["scim2-models>=0.12.
|
|
32
|
+
dependencies = ["scim2-models>=0.12.7"]
|
|
33
33
|
|
|
34
34
|
[[project.authors]]
|
|
35
35
|
name = "Yaal Coop"
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scim2-server"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.9.0"
|
|
8
8
|
description = "SCIM server core for any storage and web framework"
|
|
9
9
|
authors = [
|
|
10
10
|
{name="Yaal Coop", email="contact@yaal.coop"},
|
|
@@ -30,7 +30,7 @@ classifiers = [
|
|
|
30
30
|
|
|
31
31
|
requires-python = ">= 3.11"
|
|
32
32
|
dependencies = [
|
|
33
|
-
"scim2-models>=0.12.
|
|
33
|
+
"scim2-models>=0.12.7",
|
|
34
34
|
]
|
|
35
35
|
|
|
36
36
|
[project.optional-dependencies]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""The base of the storages that map SCIM resources to the records of an ORM.
|
|
2
|
+
|
|
3
|
+
A backend declares how each attribute is stored, and gives the primitives of
|
|
4
|
+
its ORM. The core holds the SCIM rules: the checks of the mappings, the
|
|
5
|
+
translation of the filters and of the sorts, the reading of the records and
|
|
6
|
+
the checks of the writes. scim2-django and scim2-sqlalchemy build on it.
|
|
7
|
+
|
|
8
|
+
.. warning::
|
|
9
|
+
|
|
10
|
+
This API follows the needs of these backends, and may change in any minor
|
|
11
|
+
version.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from .declaration import Attribute
|
|
15
|
+
from .declaration import Entry
|
|
16
|
+
from .declaration import Link
|
|
17
|
+
from .declaration import Many
|
|
18
|
+
from .descriptors import Collection
|
|
19
|
+
from .descriptors import Column
|
|
20
|
+
from .descriptors import EntryColumns
|
|
21
|
+
from .descriptors import Storage
|
|
22
|
+
from .filters import Predicates
|
|
23
|
+
from .mapping import MappingProblem
|
|
24
|
+
from .mapping import ResourceMapping
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"Attribute",
|
|
28
|
+
"Collection",
|
|
29
|
+
"Column",
|
|
30
|
+
"Entry",
|
|
31
|
+
"EntryColumns",
|
|
32
|
+
"Link",
|
|
33
|
+
"Many",
|
|
34
|
+
"MappingProblem",
|
|
35
|
+
"Predicates",
|
|
36
|
+
"ResourceMapping",
|
|
37
|
+
"Storage",
|
|
38
|
+
]
|
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
"""Resources built from records, and the checks and plans of their writes."""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import hmac
|
|
5
|
+
import json
|
|
6
|
+
from collections.abc import Iterable
|
|
7
|
+
from collections.abc import Mapping
|
|
8
|
+
from inspect import isclass
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from pydantic import BaseModel
|
|
12
|
+
from scim2_models import AttributeBinding
|
|
13
|
+
from scim2_models import InvalidValueException
|
|
14
|
+
from scim2_models import Meta
|
|
15
|
+
from scim2_models import Mutability
|
|
16
|
+
from scim2_models import MutabilityException
|
|
17
|
+
from scim2_models import Path
|
|
18
|
+
from scim2_models import Resource
|
|
19
|
+
from scim2_models import ScimPolicy
|
|
20
|
+
|
|
21
|
+
from .descriptors import Collection
|
|
22
|
+
from .descriptors import Column
|
|
23
|
+
from .descriptors import Storage
|
|
24
|
+
from .filters import as_stored
|
|
25
|
+
from .mapping import ResourceMapping
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def comparable(value: Any) -> Any:
|
|
29
|
+
"""Return a value in a form two resources compare in. A list of missing values is no value."""
|
|
30
|
+
if isinstance(value, list):
|
|
31
|
+
values = [as_stored(item) for item in value]
|
|
32
|
+
return None if all(item is None for item in values) else values
|
|
33
|
+
|
|
34
|
+
return as_stored(value)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def values(binding: AttributeBinding, resource: Resource[Any]) -> list[Any]:
|
|
38
|
+
return list(Path(binding.urn).get(resource, strict=False) or [])
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def entries(collection: Collection, resource: Resource[Any]) -> list[Any]:
|
|
42
|
+
"""Return the entries a resource holds for a collection, as a list even for a single one."""
|
|
43
|
+
if collection.single:
|
|
44
|
+
value = Path(collection.binding.urn).get(resource, strict=False)
|
|
45
|
+
return [] if value is None else [value]
|
|
46
|
+
|
|
47
|
+
return values(collection.binding, resource)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def version_hash(resource: Resource[Any], key: bytes) -> str:
|
|
51
|
+
"""Return the weak ETag of the hash of a resource.
|
|
52
|
+
|
|
53
|
+
The hash covers the values hidden from clients, so it is keyed: a version
|
|
54
|
+
tells nothing about these values.
|
|
55
|
+
"""
|
|
56
|
+
data = json.dumps(resource.model_dump(mode="json"), sort_keys=True)
|
|
57
|
+
digest = hmac.new(key, data.encode(), hashlib.sha256)
|
|
58
|
+
return f'W/"{digest.hexdigest()[:32]}"'
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def to_scim(
|
|
62
|
+
mapping: ResourceMapping,
|
|
63
|
+
record: Any,
|
|
64
|
+
resource_type: str,
|
|
65
|
+
model: type[Resource[Any]],
|
|
66
|
+
endpoints: Mapping[str, str],
|
|
67
|
+
collections: Iterable[Collection],
|
|
68
|
+
version_key: bytes | None,
|
|
69
|
+
) -> Resource[Any]:
|
|
70
|
+
"""Build the resource a record stores, reading the given collections only."""
|
|
71
|
+
resource = model()
|
|
72
|
+
special = (mapping._id, mapping._created, mapping._last_modified)
|
|
73
|
+
for column in mapping._columns.values():
|
|
74
|
+
if column in special or column.in_entry:
|
|
75
|
+
continue
|
|
76
|
+
|
|
77
|
+
value = mapping._read(column, record)
|
|
78
|
+
if value is not None:
|
|
79
|
+
Path[model](column.binding.urn).set(resource, value) # type: ignore[valid-type]
|
|
80
|
+
|
|
81
|
+
for entry in mapping._entries.values():
|
|
82
|
+
found = {
|
|
83
|
+
column.binding.urn.rsplit(".", 1)[-1]: mapping._read(column, record)
|
|
84
|
+
for column in entry.columns.values()
|
|
85
|
+
}
|
|
86
|
+
if any(value is not None for value in found.values()):
|
|
87
|
+
found |= {name: column.constant for name, column in entry.constants.items()}
|
|
88
|
+
Path[model](entry.binding.urn).set(resource, [found]) # type: ignore[valid-type]
|
|
89
|
+
|
|
90
|
+
read: dict[str, tuple[bool, list[Any]]] = {}
|
|
91
|
+
for collection in collections:
|
|
92
|
+
endpoint = None
|
|
93
|
+
if Path[model](f"{collection.binding.urn}.$ref").resolve() is not None: # type: ignore[valid-type]
|
|
94
|
+
endpoint = endpoints.get(collection.resource_type) # type: ignore[arg-type]
|
|
95
|
+
|
|
96
|
+
_, found_entries = read.setdefault(
|
|
97
|
+
collection.binding.urn, (collection.single, [])
|
|
98
|
+
)
|
|
99
|
+
for related in mapping._related(collection, record):
|
|
100
|
+
entry = _read_entry(mapping, collection, related, endpoint)
|
|
101
|
+
if entry is not None:
|
|
102
|
+
found_entries.append(entry)
|
|
103
|
+
|
|
104
|
+
for urn, (single, found_entries) in read.items():
|
|
105
|
+
if found_entries:
|
|
106
|
+
Path[model](urn).set( # type: ignore[valid-type]
|
|
107
|
+
resource, found_entries[0] if single else found_entries
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
resource.id = str(mapping._read(mapping._id, record))
|
|
111
|
+
resource.meta = Meta(
|
|
112
|
+
resource_type=resource_type,
|
|
113
|
+
created=_read_date(mapping, mapping._created, record),
|
|
114
|
+
last_modified=_read_date(mapping, mapping._last_modified, record),
|
|
115
|
+
)
|
|
116
|
+
resource = model.model_validate(resource.model_dump())
|
|
117
|
+
if mapping.version is not None:
|
|
118
|
+
version = f'W/"{mapping._counter(record)}"'
|
|
119
|
+
else:
|
|
120
|
+
if version_key is None:
|
|
121
|
+
raise ValueError(f"{mapping!r} has no counter, and needs a version key")
|
|
122
|
+
|
|
123
|
+
version = version_hash(resource, version_key)
|
|
124
|
+
|
|
125
|
+
resource.meta.version = version # type: ignore[union-attr]
|
|
126
|
+
return resource
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _read_date(mapping: ResourceMapping, column: Column | None, record: Any) -> Any:
|
|
130
|
+
return None if column is None else mapping._read(column, record)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _read_entry(
|
|
134
|
+
mapping: ResourceMapping, collection: Collection, entry: Any, endpoint: str | None
|
|
135
|
+
) -> Any:
|
|
136
|
+
"""Return the values of an entry, or the value of an entry of a simple collection.
|
|
137
|
+
|
|
138
|
+
The entries of a Link are records of its target, which reads them.
|
|
139
|
+
"""
|
|
140
|
+
reader = mapping if collection.owned else collection.target
|
|
141
|
+
assert reader is not None
|
|
142
|
+
if collection.simple:
|
|
143
|
+
return reader._read(collection.columns[None], entry)
|
|
144
|
+
|
|
145
|
+
found = {
|
|
146
|
+
column.binding.urn.rsplit(".", 1)[-1]: reader._read(column, entry)
|
|
147
|
+
for column in collection.columns.values()
|
|
148
|
+
}
|
|
149
|
+
if collection.owned:
|
|
150
|
+
return found
|
|
151
|
+
|
|
152
|
+
found["value"] = str(found["value"])
|
|
153
|
+
if endpoint is not None:
|
|
154
|
+
found["$ref"] = f"{endpoint}/{found['value']}"
|
|
155
|
+
|
|
156
|
+
if collection.link_type is not None:
|
|
157
|
+
found["type"] = collection.link_type
|
|
158
|
+
|
|
159
|
+
return found
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def check_storable(
|
|
163
|
+
mapping: ResourceMapping,
|
|
164
|
+
resource: Resource[Any],
|
|
165
|
+
stored: Resource[Any] | None,
|
|
166
|
+
) -> None:
|
|
167
|
+
"""Refuse a change of a resource that the mapping cannot store.
|
|
168
|
+
|
|
169
|
+
Each attribute is compared with the stored resource, None for a new one.
|
|
170
|
+
A value equal to the stored one changes nothing. The read-only attributes
|
|
171
|
+
of the model are skipped, as RFC 7644 §3.5.1 has them ignored.
|
|
172
|
+
"""
|
|
173
|
+
_check_entry_values(mapping, resource)
|
|
174
|
+
_check_single_entries(mapping, resource)
|
|
175
|
+
model = type(resource)
|
|
176
|
+
for path in Path[model].iter_paths(): # type: ignore[valid-type]
|
|
177
|
+
binding = path.resolve()
|
|
178
|
+
if binding is None:
|
|
179
|
+
continue
|
|
180
|
+
|
|
181
|
+
target = binding.target_type
|
|
182
|
+
if isclass(target) and issubclass(target, BaseModel):
|
|
183
|
+
continue
|
|
184
|
+
|
|
185
|
+
if Mutability.read_only in (
|
|
186
|
+
binding.model.get_field_annotation(binding.field_name, Mutability),
|
|
187
|
+
binding.get_annotation(Mutability),
|
|
188
|
+
):
|
|
189
|
+
continue
|
|
190
|
+
|
|
191
|
+
storage = mapping._storage(binding.urn)
|
|
192
|
+
value = comparable(path.get(resource, strict=False))
|
|
193
|
+
current = None
|
|
194
|
+
if stored is not None:
|
|
195
|
+
current = comparable(Path(binding.urn).get(stored, strict=False))
|
|
196
|
+
|
|
197
|
+
if storage == Storage.written:
|
|
198
|
+
_check_written(mapping, binding, value, current, stored is None)
|
|
199
|
+
continue
|
|
200
|
+
|
|
201
|
+
if storage in (
|
|
202
|
+
Storage.derived,
|
|
203
|
+
Storage.ref,
|
|
204
|
+
Storage.link_type,
|
|
205
|
+
Storage.constant,
|
|
206
|
+
) or value in (None, current):
|
|
207
|
+
continue
|
|
208
|
+
|
|
209
|
+
if storage == Storage.read_only:
|
|
210
|
+
raise MutabilityException(
|
|
211
|
+
attribute=binding.urn,
|
|
212
|
+
mutability="readOnly",
|
|
213
|
+
detail=f"'{binding.urn}' is read-only on this server",
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
raise InvalidValueException(
|
|
217
|
+
detail=f"'{binding.urn}' is not stored by this server"
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _check_written(
|
|
222
|
+
mapping: ResourceMapping,
|
|
223
|
+
binding: AttributeBinding,
|
|
224
|
+
value: Any,
|
|
225
|
+
current: Any,
|
|
226
|
+
creating: bool,
|
|
227
|
+
) -> None:
|
|
228
|
+
_check_enumerated(mapping, binding, value)
|
|
229
|
+
if value is not None:
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
if creating and mapping._required(binding.urn):
|
|
233
|
+
raise InvalidValueException(
|
|
234
|
+
detail=f"'{binding.urn}' is required on this server"
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
if current is not None and not mapping._nullable(binding.urn):
|
|
238
|
+
raise MutabilityException(
|
|
239
|
+
attribute=binding.urn,
|
|
240
|
+
detail=f"'{binding.urn}' cannot be removed on this server",
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _check_entry_values(mapping: ResourceMapping, resource: Resource[Any]) -> None:
|
|
245
|
+
for collection in mapping._all_collections():
|
|
246
|
+
if not collection.owned or not collection.writable or collection.simple:
|
|
247
|
+
continue
|
|
248
|
+
|
|
249
|
+
for entry in entries(collection, resource):
|
|
250
|
+
for column in collection.columns.values():
|
|
251
|
+
value = getattr(entry, column.binding.target_field_name, None)
|
|
252
|
+
if value is None and column.required:
|
|
253
|
+
raise InvalidValueException(
|
|
254
|
+
detail=f"'{column.binding.urn}' is required on this server"
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _check_single_entries(mapping: ResourceMapping, resource: Resource[Any]) -> None:
|
|
259
|
+
"""Refuse several entries for an Entry, or an entry with another type than its constant."""
|
|
260
|
+
for entry in mapping._entries.values():
|
|
261
|
+
found = values(entry.binding, resource)
|
|
262
|
+
if len(found) > 1:
|
|
263
|
+
raise InvalidValueException(
|
|
264
|
+
detail=f"'{entry.binding.urn}' holds one entry at most on this server"
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
for item in found:
|
|
268
|
+
for name, column in entry.constants.items():
|
|
269
|
+
if name == "primary":
|
|
270
|
+
continue
|
|
271
|
+
|
|
272
|
+
value = as_stored(getattr(item, column.binding.target_field_name, None))
|
|
273
|
+
if value is not None and value.casefold() != column.constant.casefold():
|
|
274
|
+
raise InvalidValueException(
|
|
275
|
+
detail=f"'{column.binding.urn}' can only be "
|
|
276
|
+
f"{column.constant!r} on this server"
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _check_enumerated(
|
|
281
|
+
mapping: ResourceMapping, binding: AttributeBinding, value: Any
|
|
282
|
+
) -> None:
|
|
283
|
+
"""Refuse a value that a field with choices does not store.
|
|
284
|
+
|
|
285
|
+
The resource may be of another class than the model of the mapping, such
|
|
286
|
+
as the model of the provider, so the attribute is looked up by its URN.
|
|
287
|
+
"""
|
|
288
|
+
own = Path[mapping.model](binding.urn).resolve() # type: ignore[name-defined]
|
|
289
|
+
assert own is not None
|
|
290
|
+
_, column = mapping._lookup(own)[0]
|
|
291
|
+
if column is None or column.enumeration is None:
|
|
292
|
+
return
|
|
293
|
+
|
|
294
|
+
for item in value if isinstance(value, list) else [value]:
|
|
295
|
+
if item is not None and mapping._as_stored(item) not in column.enumeration:
|
|
296
|
+
raise InvalidValueException(
|
|
297
|
+
detail=f"'{binding.urn}' cannot hold {item!r} on this server"
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def keeps(
|
|
302
|
+
mapping: ResourceMapping, column: Column, value: Any, record: Any, creating: bool
|
|
303
|
+
) -> bool:
|
|
304
|
+
"""Tell whether a value leaves a record as it is.
|
|
305
|
+
|
|
306
|
+
When creating, a missing value keeps the default, unless the backend
|
|
307
|
+
writes a value for none. When updating, a value equal to the one read is
|
|
308
|
+
kept, so that a password hash is not hashed again.
|
|
309
|
+
"""
|
|
310
|
+
if creating:
|
|
311
|
+
return value is None and not column.fills_none
|
|
312
|
+
|
|
313
|
+
return bool(value == mapping._read(column, record))
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def write_plan(
|
|
317
|
+
mapping: ResourceMapping,
|
|
318
|
+
resource: Resource[Any],
|
|
319
|
+
record: Any,
|
|
320
|
+
creating: bool,
|
|
321
|
+
) -> list[tuple[Column, Any]]:
|
|
322
|
+
"""Return the columns of the resource record to write, with their SCIM values.
|
|
323
|
+
|
|
324
|
+
The backend converts each value into what its record stores. The dates,
|
|
325
|
+
the collections and the related records are written apart.
|
|
326
|
+
"""
|
|
327
|
+
plan: list[tuple[Column, Any]] = []
|
|
328
|
+
for column in mapping._columns.values():
|
|
329
|
+
if not column.writable or column.related or column.in_entry:
|
|
330
|
+
continue
|
|
331
|
+
|
|
332
|
+
value = Path(column.binding.urn).get(resource, strict=False)
|
|
333
|
+
if not keeps(mapping, column, value, record, creating):
|
|
334
|
+
plan.append((column, value))
|
|
335
|
+
|
|
336
|
+
for entry in mapping._entries.values():
|
|
337
|
+
found = values(entry.binding, resource)
|
|
338
|
+
if not entry.writable or (creating and not found):
|
|
339
|
+
continue
|
|
340
|
+
|
|
341
|
+
item = found[0] if found else None
|
|
342
|
+
for column in entry.columns.values():
|
|
343
|
+
value = getattr(item, column.binding.target_field_name, None)
|
|
344
|
+
if not keeps(mapping, column, value, record, creating):
|
|
345
|
+
plan.append((column, value))
|
|
346
|
+
|
|
347
|
+
return plan
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def dates(
|
|
351
|
+
mapping: ResourceMapping, now: Any, creating: bool
|
|
352
|
+
) -> list[tuple[Column, Any]]:
|
|
353
|
+
"""Return the date columns set to now: the modification date, and the creation date when creating."""
|
|
354
|
+
columns = [mapping._last_modified, mapping._created if creating else None]
|
|
355
|
+
return [(column, now) for column in columns if column is not None]
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def link_entries(
|
|
359
|
+
mapping: ResourceMapping, resource: Resource[Any]
|
|
360
|
+
) -> list[tuple[list[Collection], list[Any]]]:
|
|
361
|
+
"""Return the entries of each written attribute linking to other resources, with the collections of its Links.
|
|
362
|
+
|
|
363
|
+
An entry without value, such as one with a $ref only, is refused rather than lost.
|
|
364
|
+
"""
|
|
365
|
+
links = []
|
|
366
|
+
for collections in mapping._collections.values():
|
|
367
|
+
collection = collections[0]
|
|
368
|
+
if collection.owned or not collection.writable:
|
|
369
|
+
continue
|
|
370
|
+
|
|
371
|
+
found = entries(collection, resource)
|
|
372
|
+
if any(entry.value is None for entry in found):
|
|
373
|
+
raise InvalidValueException(
|
|
374
|
+
detail=f"'{collection.binding.urn}' holds an entry without value"
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
links.append((collections, found))
|
|
378
|
+
|
|
379
|
+
return links
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def same_type(collection: Collection, value: str, policy: ScimPolicy) -> bool:
|
|
383
|
+
"""Tell whether a type is the one a Link gives, compared as filters compare it."""
|
|
384
|
+
binding = collection.type_column.binding # type: ignore[union-attr]
|
|
385
|
+
try:
|
|
386
|
+
return bool(
|
|
387
|
+
binding.comparable(value, policy)
|
|
388
|
+
== binding.comparable(collection.link_type, policy)
|
|
389
|
+
)
|
|
390
|
+
except ValueError:
|
|
391
|
+
return False
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def assign_links(
|
|
395
|
+
found: list[tuple[Collection, list[Any]]],
|
|
396
|
+
link_values: list[Any],
|
|
397
|
+
policy: ScimPolicy,
|
|
398
|
+
) -> dict[Collection, list[Any]]:
|
|
399
|
+
"""Give each entry the record it links to, in the first Link holding that record.
|
|
400
|
+
|
|
401
|
+
found holds the records each Link holds among the values of the entries.
|
|
402
|
+
An entry with a type links to a Link giving the same type.
|
|
403
|
+
"""
|
|
404
|
+
links: dict[Collection, list[Any]] = {collection: [] for collection, _ in found}
|
|
405
|
+
urn = found[0][0].binding.urn
|
|
406
|
+
for entry in link_values:
|
|
407
|
+
matches = [
|
|
408
|
+
(collection, record)
|
|
409
|
+
for collection, records in found
|
|
410
|
+
for record in records
|
|
411
|
+
if collection.target._holds_id(record, entry.value) # type: ignore[union-attr]
|
|
412
|
+
]
|
|
413
|
+
if not matches:
|
|
414
|
+
raise InvalidValueException(detail=f"'{urn}' links to an unknown resource")
|
|
415
|
+
|
|
416
|
+
entry_type = getattr(entry, "type", None)
|
|
417
|
+
if entry_type is not None:
|
|
418
|
+
matches = [
|
|
419
|
+
(collection, record)
|
|
420
|
+
for collection, record in matches
|
|
421
|
+
if same_type(collection, entry_type, policy)
|
|
422
|
+
]
|
|
423
|
+
|
|
424
|
+
if not matches:
|
|
425
|
+
raise InvalidValueException(
|
|
426
|
+
detail=f"'{urn}' links to {entry.value!r}, which is not "
|
|
427
|
+
f"of type {entry_type!r}"
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
collection, record = matches[0]
|
|
431
|
+
if record not in links[collection]:
|
|
432
|
+
links[collection].append(record)
|
|
433
|
+
|
|
434
|
+
return links
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""The values a mapping declares for each SCIM attribute.
|
|
2
|
+
|
|
3
|
+
The targets are opaque to the core: a field name for Django, a column or a
|
|
4
|
+
relationship for SQLAlchemy, a field for Peewee. Each backend subclasses these
|
|
5
|
+
classes, to name the parameters after its ORM and to document them.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from scim2_models import AttributeBinding
|
|
12
|
+
from scim2_models import Mutability
|
|
13
|
+
from scim2_models import Required
|
|
14
|
+
from scim2_models import Returned
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Attribute:
|
|
18
|
+
"""A value holding a SCIM attribute, whose writing can be turned off."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, target: Any, *, writable: bool = True) -> None:
|
|
21
|
+
self.target = target
|
|
22
|
+
self.writable = writable
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Many:
|
|
26
|
+
"""The entries of a multi-valued attribute, stored in related records.
|
|
27
|
+
|
|
28
|
+
attributes maps each sub-attribute name to its target in the entry
|
|
29
|
+
records, or is a single target for a multi-valued simple attribute.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, relation: Any, attributes: Mapping[str, Any] | Any) -> None:
|
|
33
|
+
self.relation = relation
|
|
34
|
+
self.attributes = attributes
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Entry:
|
|
38
|
+
"""A multi-valued complex attribute holding one entry at most, stored in the resource record.
|
|
39
|
+
|
|
40
|
+
constants holds the sub-attributes the entry always has, such as its type.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
attributes: Mapping[str, Any],
|
|
46
|
+
*,
|
|
47
|
+
type: str | None = None,
|
|
48
|
+
primary: bool | None = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
self.attributes = attributes
|
|
51
|
+
self.constants = {
|
|
52
|
+
name: value
|
|
53
|
+
for name, value in (("type", type), ("primary", primary))
|
|
54
|
+
if value is not None
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Link:
|
|
59
|
+
"""A complex attribute linking to the resources of one resource type."""
|
|
60
|
+
|
|
61
|
+
def __init__(
|
|
62
|
+
self, relation: Any, resource_type: str, *, type: str | None = None
|
|
63
|
+
) -> None:
|
|
64
|
+
self.relation = relation
|
|
65
|
+
self.resource_type = resource_type
|
|
66
|
+
self.type = type
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def key(binding: AttributeBinding) -> tuple[Any, ...]:
|
|
70
|
+
"""Return what tells an attribute apart, whatever the case of its path."""
|
|
71
|
+
return (binding.model, binding.field_name, binding.sub_field_name)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def overlap(key: tuple[Any, ...], other: tuple[Any, ...]) -> bool:
|
|
75
|
+
"""Tell whether two attributes are the same, or one holds the other."""
|
|
76
|
+
return key == other or (key[:2] == other[:2] and None in (key[2], other[2]))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def mutabilities(binding: AttributeBinding) -> set[Mutability | None]:
|
|
80
|
+
"""Return the mutabilities of an attribute, on its own or through its parent."""
|
|
81
|
+
return {
|
|
82
|
+
binding.model.get_field_annotation(binding.field_name, Mutability),
|
|
83
|
+
binding.get_annotation(Mutability),
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def never_returned(binding: AttributeBinding) -> bool:
|
|
88
|
+
"""Tell whether an attribute is never returned, on its own or through its parent."""
|
|
89
|
+
return Returned.never in (
|
|
90
|
+
binding.model.get_field_annotation(binding.field_name, Returned),
|
|
91
|
+
binding.get_annotation(Returned),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def required(binding: AttributeBinding) -> bool:
|
|
96
|
+
"""Tell whether an attribute is required where it is defined."""
|
|
97
|
+
return Required.true in (
|
|
98
|
+
binding.model.get_field_annotation(binding.field_name, Required),
|
|
99
|
+
binding.get_annotation(Required),
|
|
100
|
+
)
|