Mapping#
The classes describing how SCIM resources are stored in SQLAlchemy models, and the function generating them from the resource types.
- class scim2_sqlalchemy.ResourceMapping(model, record, attributes, *, version=None)[source]#
How the resources of a SCIM model are stored in the records of a SQLAlchemy model.
The paths and the columns are checked when the mapping is created, so a mistake raises at import time rather than at the first request.
- Parameters:
model (type[Resource]) – The SCIM model of the resources, such as
User[EnterpriseUser].attributes (Mapping[str, Any]) – The attributes, by SCIM path. A value is a column, a
hybrid_property, anAttribute, aMany, anEntry, aLink, or a list ofLink.idis required.meta.createdandmeta.lastModifiedare filled by the storage: without them, the resources have no such date.version (Any) – The
version_id_colof the mapper ofrecord, which givesmeta.version(Configuring a Version Counter). It only changes when the record changes, not when a linked record does: adding a member to a group leaves the version of the user as it is. Without it,meta.versionis a hash of the whole resource, which the storage computes with itsversion_key. The storage then reads every collection of a resource, even those a response leaves out, such as the members of a group.
- Raises:
ValueError – When a path is not valid for the model, when a value does not fit the attribute of its path, or when
idis mapped to a column that can holdNULL. Also when clients could neither read nor write an attribute, when an attribute clients read, or an immutable one, is mapped to aHashedcolumn, or when removing aLinkwould writeNULLin a foreign key that refuses it.
The mapping warns about a column that needs a value but that the storage never writes.
- schemas()[source]#
Return the schemas of the resources, restricted to what the mapping stores.
They hold the mapped attributes only. An attribute the mapping reads but never writes is
readOnly. An attribute whose column cannot beNULLand has no default isrequired, unless clients never read it back. A column with a default that cannot beNULLleaves its attribute optional, but its value cannot be removed. Publish them, rather than the schemas ofmodel, so that clients know what the server stores: build the provider of the server withScimProvider.from_discovery, and pass it to the storage.
- class scim2_sqlalchemy.Attribute(expression, *, writable=True)[source]#
A column holding a SCIM attribute, whose writing can be turned off.
To store a value in another form, map the attribute to a
hybrid_propertythat converts it, in Python and in SQL.- Parameters:
expression (Any) – The column, or the
hybrid_property, holding the value.writable (bool) – Whether the storage writes the attribute. When
False, the attribute is read-only.
- class scim2_sqlalchemy.Hashed(hash_value, length=None)[source]#
A column type storing the hash of a string, such as a password.
The column stores what the hash function returns for the value written. Map it to an attribute that the schema makes
writeOnly, such aspassword, or never returns. The storage reads the hash back, and clients never see it. A replacement without the value gives the hash back to the storage, which keeps it as it is.
- class scim2_sqlalchemy.Many(relationship, attributes)[source]#
The entries of a multi-valued attribute, stored in a child table.
The entries belong to the resource: writing the resource replaces them. The relationship needs
cascade="all, delete-orphan"(delete-orphan). Itsorder_bygives the order of the entries. Without it, the order is the one the database returns. A sort uses theprimaryentry, or else the first one.Since writing replaces the entries, they cannot hold values that clients never read back, such as write-only values. Their columns are also either all written or none.
The entries of a multi-valued simple attribute, such as a list of strings, are the values of one column of the child table. The storage keeps their order and their duplicates.
- class scim2_sqlalchemy.Entry(attributes, *, type=None, primary=None)[source]#
A multi-valued complex attribute holding one entry at most, stored in columns of the resource record.
Such as the email address of a user, in its
emailcolumn. The resource holds an entry when one of the columns has a value, so the columns acceptNULL: a write removing the entry empties them. A write with several entries is refused.- Parameters:
attributes (Mapping[str, Any]) – The columns of the resource record holding the sub-attributes, by sub-attribute name, such as
{"value": UserRecord.email}. A value may be anAttribute.type (str | None) – The
typesub-attribute of the entry, such as"work". A client may send it, and a write with another type is refused. Without it, the storage does not storetype.primary (bool | None) – The
primarysub-attribute of the entry. A client may send any value, as the lone entry is the primary one, and reads this one back. Without it, the storage does not storeprimary.
- class scim2_sqlalchemy.Link(relationship, resource_type, *, type=None)[source]#
A complex attribute linking to other resources.
A multi-valued attribute, such as the
membersof a group, maps to a relationship holding several records. A single-valued one, such as themanagerof an enterprise user, maps to a relationship holding one record.valueis theidof the linked resource. Its display name, thedisplayordisplayNamesub-attribute, is thedisplayNameof the linked resource when its mapping has one, or else itsuserName. Without either, the links have no display name, althoughResourceMapping.schemas()publishes it. Writing the resource changes the links, and never creates nor deletes the linked resources.A Link links to the resources of one resource type. An attribute linking to several resource types, such as the
membersof a group holding users and groups, maps to a list of Links, one per resource type. The entries are read in the order of the list.- Parameters:
relationship (Any) – The
relationship()from the resource record to the records of the linked resources. The foreign key of a many-to-one relationship acceptsNULL, unless the schema makes the attribute required, and the extension holding it too. The foreign key of a one-to-many relationship always acceptsNULL.resource_type (str) – The name of the resource type of the linked resources, as the storage knows it.
type (str | None) – The
typesub-attribute of the entries, such as"User"for the members of a group, or"direct"for the groups of a user. A client may send it, and a write with another value is refused. Without it, the storage does not storetype.
- scim2_sqlalchemy.generate_mappings(provider, metadata, *, prefix='', store=_store_all, inverses=None)[source]#
Generate the tables and the mappings that store the resources of a provider.
Each resource type gets a table named after its endpoint, such as
users. A simple attribute gets a column, a complex attribute holding a single value gets a column for each sub-attribute, and an extension gets columns prefixed by the name of its schema. A multi-valued attribute gets a child table, mapped withMany. A complex attribute whose$reflinks to served resource types gets aLinkfor each of them.A read-only attribute is not stored, except an inverse: it reads the links of the attribute it reverses, such as
groupsof a user, which reads themembersof the groups. A multi-valued sub-attribute is not stored either, nor a write-only attribute other than a single string. A column holdsNOT NULLfor a required attribute of the core schema, andUNIQUEfor an attribute unique on the server. The table and column names longer than 63 characters are shortened.storedecides how each attribute is kept. It receives theAttributeBindingof the attribute, and returns:True, to store the attribute with the column type matching it;False, not to store it: the published schemas leave it out;a column type, such as
Hashed, to store it in a column of this type. Only an attribute with a column of its own takes one: a simple attribute, a sub-attribute, or the values of a multi-valued simple attribute.
A write-only string, such as a password, is never stored in clear text:
storegives it a column type that hashes it, or does not store it.Pass the mappings to the storage, and create the tables with
MetaData.create_allor a migration tool.- Parameters:
provider (ScimProvider) – The provider of the server, which gives the resource types and their models.
prefix (str) – A prefix of the names of the tables.
store (Callable[[AttributeBinding], bool | TypeEngine]) – The function deciding how each attribute is kept. By default, every attribute is stored with the column type matching it.
inverses (Mapping[str, str] | None) – The read-only attributes reading the links of another attribute, by URN, in addition to
groupsof a user reversingmembersof a group.
- Returns:
The mappings, by resource type name.
- Raises:
ValueError – When
storekeeps a write-only string in clear text, when it gives a column type to an attribute without a column of its own, or when two attributes need the same column name.- Return type: