Mapping core#
The base of the storages that map SCIM resources to the records of an ORM. It holds the SCIM rules, and a backend gives the primitives of its ORM. scim2-django and scim2-sqlalchemy build on it.
Warning
This API follows the needs of these backends, and may change in any minor version.
The base of the storages that map SCIM resources to the records of an ORM.
A backend declares how each attribute is stored, and gives the primitives of its ORM. The core holds the SCIM rules: the checks of the mappings, the translation of the filters and of the sorts, the reading of the records and the checks of the writes. scim2-django and scim2-sqlalchemy build on it.
Warning
This API follows the needs of these backends, and may change in any minor version.
Backend hooks#
A backend subclasses ResourceMapping and overrides these methods:
_is_target(value)tells whether a value is a target of the ORM, such as a field;_check_declared(path, binding, value, holder)refuses a value the backend cannot map, by raisingValueError;_build_column(path, binding, value, owner)and_build_collection(path, binding, declaration)build the descriptors, and yield aMappingProblemfor a target that does not fit;_check_record()and_fields_needing_value()give the problems and the fields of the record;_links_to(collection, target)tells whether a Link holds the records of a target mapping;_read(column, record),_related(collection, record),_counter(record)and_holds_id(record, value)read the records.
The backend calls _resolve() once its models are ready, and gets the problems of the mapping.
A storage then calls _targets(mappings) on every mapping, then _bind(targets), so that a
refused mapping binds nothing.
Declaration#
The values a mapping declares for each SCIM attribute.
The targets are opaque to the core: a field name for Django, a column or a relationship for SQLAlchemy, a field for Peewee. Each backend subclasses these classes, to name the parameters after its ORM and to document them.
- class scim2_server.mapping.declaration.Attribute(target: Any, *, writable: bool = True)[source]#
A value holding a SCIM attribute, whose writing can be turned off.
- class scim2_server.mapping.declaration.Many(relation: Any, attributes: Mapping[str, Any] | Any)[source]#
The entries of a multi-valued attribute, stored in related records.
attributes maps each sub-attribute name to its target in the entry records, or is a single target for a multi-valued simple attribute.
- class scim2_server.mapping.declaration.Entry(attributes: Mapping[str, Any], *, type: str | None = None, primary: bool | None = None)[source]#
A multi-valued complex attribute holding one entry at most, stored in the resource record.
constants holds the sub-attributes the entry always has, such as its type.
Resolution#
- class scim2_server.mapping.ResourceMapping(model: type[Resource], record: Any, attributes: Mapping[str, Any], *, version: Any = None)[source]#
How the resources of a SCIM model are stored in the records of a backend.
The paths are checked when the mapping is created, which raises
ValueErrorfor a path out of the model. The backend resolves the targets once its models are ready, and gets the problems of the mapping asMappingProblemobjects.- schemas() list[Schema][source]#
Return the schemas of the resources, restricted to what the mapping stores.
- unique_attributes() dict[str, AttributeBinding][source]#
Return the attributes whose values no two resources share, by URN.
A storage puts a unique constraint on the fields holding them. They may be held by the record, by an Entry, by the entries of a collection, or be the value of a Link. A field the storage only reads counts too, as clients read it. The id is left out: it is the primary key.
- class scim2_server.mapping.MappingProblem(level: str, code: str, message: str)[source]#
A problem of a mapping, which the backend reports in its own way.
level is “error” or “warning”. code tells the rule apart, so that a backend can give it its own identifier.
What a resolved mapping knows of each attribute.
The backend builds the descriptors, and fills the facts only the ORM knows. The core derives the rest from the SCIM attribute.
- class scim2_server.mapping.descriptors.Collection(binding: AttributeBinding, relation: Any, columns: dict[str | None, ~scim2_server.mapping.descriptors.Column]=<factory>, resource_type: str | None = None, target: ResourceMapping | None = None, link_type: str | None = None, type_column: Column | None = None, nullable: bool = True, stores: str | None = None)[source]#
A multi-valued or complex attribute, stored in related records.
The entries of an owned collection belong to the resource. The others are links to the resources of another mapping, the target. The backend fills relation, and nullable for a single link: the record can hold no link.
- class scim2_server.mapping.descriptors.Column(binding: AttributeBinding, key: str, writable: bool, textual: bool = False, keyed: bool = False, nullable: bool = True, needs_value: bool = False, empty_string: bool = False, related: bool = False, enumeration: list[Any] | None = None, stores: str | None = None, fills_none: bool = False, in_entry: bool = False, constant: Any = <object object>)[source]#
A SCIM attribute stored in a value of a record.
The backend fills key, writable and the facts of the storage. The core reads keyed, nullable, needs_value, related, enumeration, stores and fills_none. textual and empty_string are left to the conditions of the backend:
textual: filters compare the value as text, as for an integer id;
keyed: a search by this value goes through its index;
nullable: the record can hold no value;
needs_value: a new record without value fails in the database;
empty_string: the record holds an empty string for no value;
related: the value is held by a related record, which may not exist;
enumeration: the values the storage accepts, if it restricts them;
stores: the name of the record field written, for the checks;
fills_none: the backend writes a value of its own for no value, as a function converting the values may.
A constant column always holds the same value, when its holder exists.
- class scim2_server.mapping.descriptors.EntryColumns(binding: AttributeBinding, columns: dict[str, Column], constants: dict[str, Column])[source]#
The columns of an Entry, held by the resource record.
The entry exists when one of its columns has a value.
- class scim2_server.mapping.descriptors.Storage(*values)[source]#
How a mapping stores an attribute.
A derived attribute is read from another resource, such as the display of a member. A ref is the $ref of a link, built from its value. A link type is the type a Link gives its entries, and a constant a sub-attribute an Entry gives its entry: clients may send them, and the storage checks them.
Filters and sorts#
SCIM filters and sorts, turned into the conditions of a backend.
The core holds the SCIM rules: the scope of a value selection, ne on a
collection, missing values, substrings, strings the policy cannot prepare,
constants. The backend gives the primitives, through Predicates.
- class scim2_server.mapping.filters.Predicates(*args, **kwargs)[source]#
The conditions a backend builds, on the records or on the entries of a collection.
The core only calls compare and compare_text with a value that is not None, and compare_text with a string the policy can prepare. compare never gets a substring operator.
Each condition is true or false, never unknown as a SQL NULL is, so that its negation holds when it does not. compare_text is false for a stored string the policy cannot prepare.
- is_empty(column: Column) C[source]#
Return the condition that a string column holds an empty string.
- compare_text(column: Column, op: CompareOperator, value: str) C[source]#
Compare a string column with the comparison key of the policy.
- compare_key(column: Column, value: Any) C | None[source]#
Return the condition that a keyed column equals a value, or None when no record can hold it.
- exists(collection: Collection, build: Callable[[Predicates[C]], C] | None = None) C[source]#
Return the condition that an entry exists, matching what build returns from the conditions on the entries.
- scim2_server.mapping.filters.holds(binding: AttributeBinding, op: CompareOperator, actual: Any, expected: Any, policy: ScimPolicy) bool[source]#
Tell whether a value matches a comparison, as ScimFilter.match tells it.
- scim2_server.mapping.filters.entry_present(conditions: Predicates[C], entry: EntryColumns) C[source]#
Return the condition that the record holds the entry: one of its columns has a value.
- scim2_server.mapping.filters.present(mapping: ResourceMapping, conditions: Predicates[C], column: Column) C[source]#
Per RFC 7644 §3.4.2.2, an empty string is no value.
- scim2_server.mapping.filters.compare(mapping: ResourceMapping, conditions: Predicates[C], policy: ScimPolicy, column: Column, op: CompareOperator, value: Any) C[source]#
Compare a column with a value.
A constant is compared in Python, as it is the same for every record holding it. A missing value is equal to no value but a missing one.
- scim2_server.mapping.filters.where(mapping: ResourceMapping, search_request: SearchRequest[Any], conditions: Predicates[C], policy: ScimPolicy) C[source]#
Return the condition keeping the records matching the filter of a search.
- scim2_server.mapping.filters.sort_target(mapping: ResourceMapping, search_request: SearchRequest[Any]) tuple[Collection | None, Column] | None[source]#
Return the column the records sort on, with its collection if any.
Return None when the search has no sortBy, or when the model of the mapping does not declare its attribute. A value hidden from clients, or held by another resource, cannot sort. A constant of an Entry sorts as its value when the entry exists, and as no value otherwise.
Reading and writing#
Resources built from records, and the checks and plans of their writes.
- scim2_server.mapping.conversion.comparable(value: Any) Any[source]#
Return a value in a form two resources compare in. A list of missing values is no value.
- scim2_server.mapping.conversion.entries(collection: Collection, resource: Resource) list[Any][source]#
Return the entries a resource holds for a collection, as a list even for a single one.
- scim2_server.mapping.conversion.version_hash(resource: Resource, key: bytes) str[source]#
Return the weak ETag of the hash of a resource.
The hash covers the values hidden from clients, so it is keyed: a version tells nothing about these values.
- scim2_server.mapping.conversion.to_scim(mapping: ResourceMapping, record: Any, resource_type: str, model: type[Resource], endpoints: Mapping[str, str], collections: Iterable[Collection], version_key: bytes | None) Resource[source]#
Build the resource a record stores, reading the given collections only.
- scim2_server.mapping.conversion.check_storable(mapping: ResourceMapping, resource: Resource, stored: Resource | None) None[source]#
Refuse a change of a resource that the mapping cannot store.
Each attribute is compared with the stored resource, None for a new one. A value equal to the stored one changes nothing. The read-only attributes of the model are skipped, as RFC 7644 §3.5.1 has them ignored.
- scim2_server.mapping.conversion.keeps(mapping: ResourceMapping, column: Column, value: Any, record: Any, creating: bool) bool[source]#
Tell whether a value leaves a record as it is.
When creating, a missing value keeps the default, unless the backend writes a value for none. When updating, a value equal to the one read is kept, so that a password hash is not hashed again.
- scim2_server.mapping.conversion.write_plan(mapping: ResourceMapping, resource: Resource, record: Any, creating: bool) list[tuple[Column, Any]][source]#
Return the columns of the resource record to write, with their SCIM values.
The backend converts each value into what its record stores. The dates, the collections and the related records are written apart.
- scim2_server.mapping.conversion.dates(mapping: ResourceMapping, now: Any, creating: bool) list[tuple[Column, Any]][source]#
Return the date columns set to now: the modification date, and the creation date when creating.
- scim2_server.mapping.conversion.link_entries(mapping: ResourceMapping, resource: Resource) list[tuple[list[Collection], list[Any]]][source]#
Return the entries of each written attribute linking to other resources, with the collections of its Links.
An entry without value, such as one with a $ref only, is refused rather than lost.
- scim2_server.mapping.conversion.same_type(collection: Collection, value: str, policy: ScimPolicy) bool[source]#
Tell whether a type is the one a Link gives, compared as filters compare it.
- scim2_server.mapping.conversion.assign_links(found: list[tuple[Collection, list[Any]]], link_values: list[Any], policy: ScimPolicy) dict[Collection, list[Any]][source]#
Give each entry the record it links to, in the first Link holding that record.
found holds the records each Link holds among the values of the entries. An entry with a type links to a Link giving the same type.