Storages#

The storages reading and writing the resources with a SQLAlchemy Session.

class scim2_sqlalchemy.SqlAlchemyStorage(mappings, session, provider=None, *, version_key=None)[source]#

Store SCIM resources in the records of SQLAlchemy models.

The storage never commits: committing the request is left to the application. Each write is flushed, so that the database fills the identifiers and checks its constraints.

A change the mappings cannot store raises an error rather than being lost: InvalidValueException for an attribute no mapping stores, and MutabilityException for an attribute the mapping only reads. A value equal to the stored one is accepted, so a resource read and sent back can always be written.

Parameters:
  • mappings (Mapping[str, ResourceMapping]) – The mapping of each resource type, by resource type name.

  • session (Callable[[], Session]) – Return the current Session, such as a scoped_session.

  • provider (ScimProvider | None) – The provider of the server. The storage then returns instances of its models, rather than of the models of the mappings, and gives each link a $ref relative to the SCIM root, from the endpoints of its resource types. Strings are compared with the comparison_key of its policy. Each of its resource types needs a mapping.

  • version_key (bytes | None) – The key of the hash giving the version of the resources whose mapping has no version. Keep it secret, as the hash covers the values clients never read, such as a password hash. The hash covers the links too, so renaming a member changes the version of its groups. A write on such a resource locks its row until the end of the transaction.

Raises:

ValueError – When a resource type has no mapping, or when a mapping has no version and no key is given.

operation()[source]#

Run the operation in a savepoint.

The savepoint comes from begin_nested(). A failed operation of a bulk request is then rolled back alone (RFC 7644 §3.7).

Return type:

Generator[None]

generate_id(resource_type, resource)[source]#

Return the identifier of a new resource, or None to let its column give one.

Override this method to choose the identifiers, as InMemoryStorage.generate_id allows. The following storage derives the identifier of a user from its userName at creation, so that two servers give the same user the same identifier:

>>> import uuid
>>> USERS = uuid.UUID("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
>>> class Storage(SqlAlchemyStorage):
...     def generate_id(self, resource_type, resource):
...         if resource_type.name != "User":
...             return None
...         return str(uuid.uuid5(USERS, resource.user_name))

The storage calls it once for each new resource, after checking the resource, and does not call it for a resource it refuses. Per RFC 7643 Section 3.1, an identifier is unique across all the resource types: an identifier that another resource holds raises a 409.

The storage stores the identifier in the type of its column, and the created resource has the identifier as the column gives it back. For example, 007 in an integer column becomes 7. The storage raises a ValueError when the column cannot hold the identifier, or when it cannot write it, such as an id mapped to a property without setter.

When the database numbers the records, as an autoincrement column does, give an identifier to all the resources of a type, or to none. The database does not skip the numbers given by this method, so a resource that it numbers later can get one of them, and a 409.

Parameters:
Return type:

str | None

class scim2_sqlalchemy.AsyncSqlAlchemyStorage(mappings, session, provider=None, *, version_key=None)[source]#

The asynchronous variant of SqlAlchemyStorage.

It needs the asyncio extra, which installs the asynchronous support of SQLAlchemy: pip install "scim2-sqlalchemy[asyncio]".

Parameters:
  • mappings (Mapping[str, ResourceMapping]) – The mapping of each resource type, by resource type name.

  • session (Callable[[], AsyncSession]) – Return the current AsyncSession, such as an async_scoped_session.

  • provider (ScimProvider | None) – The provider of the server. The storage then returns instances of its models, rather than of the models of the mappings. Strings are compared with the comparison_key of its policy. Each of its resource types needs a mapping.

  • version_key (bytes | None) – The key of the hash giving the version of the resources whose mapping has no version. Keep it secret, as the hash covers the values clients never read, such as a password hash. The hash covers the links too, so renaming a member changes the version of its groups. A write on such a resource locks its row until the end of the transaction.

Raises:

ValueError – When a resource type has no mapping, or when a mapping has no version and no key is given.

operation()[source]#

Run the operation in a savepoint. See SqlAlchemyStorage.operation().

Return type:

AsyncGenerator[None]

async generate_id(resource_type, resource)[source]#

Return the identifier of a new resource, or None to let its column give one.

See SqlAlchemyStorage.generate_id(). This method can await, for instance to create the account in another service and take its identifier. A write that fails afterwards, such as with a 409 from the database, does not undo what the method did: override create() to handle it.

Parameters:
Return type:

str | None