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:
InvalidValueExceptionfor an attribute no mapping stores, andMutabilityExceptionfor 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 ascoped_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
$refrelative to the SCIM root, from the endpoints of its resource types. Strings are compared with thecomparison_keyof 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.
The storage warns with a
UniqueConstraintWarningabout each unique attribute that no unique constraint of the models covers: the database checks the unique values, so two resources could hold the same value.- 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
Noneto let its column give one.Override this method to choose the identifiers, as
InMemoryStorage.generate_idallows. 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. The database checks the unique values when the storage writes the resource, after this method. So this method can run for a resource that the database then refuses with a 409.
An identifier that another resource of the same type holds raises a 409. Per RFC 7643 Section 3.1, an identifier is unique across all the resource types, but the storage does not check it: choose identifiers that cannot clash, such as UUIDs.
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,
007in an integer column becomes7. The storage raises aValueErrorwhen the column cannot hold the identifier, or when it cannot write it, such as anidmapped 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:
resource_type (ResourceType)
resource (Resource)
- Return type:
str | None
- class scim2_sqlalchemy.AsyncSqlAlchemyStorage(mappings, session, provider=None, *, version_key=None)[source]#
The asynchronous variant of
SqlAlchemyStorage.It needs the
asyncioextra, 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 anasync_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_keyof 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
Noneto 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: overridecreate()to undo it.- Parameters:
resource_type (ResourceType)
resource (Resource)
- Return type:
str | None