Overview#

scim2-flask serves the System for Cross-domain Identity Management (SCIM) protocol from a Flask application. It exposes the endpoints of RFC 7644 in a Blueprint, and leaves the protocol to scim2-server and the payloads to scim2-models.

scim2-flask does not store the resources itself. Your application provides a ScimStorage that reads and writes them, for example in its database. Your application also authenticates the clients.

Install scim2-flask. It brings Flask, scim2-server and scim2-models along:

$ pip install scim2-flask

This page introduces the parts of a SCIM server built with Flask, in the order you need them. Follow it in order for a first tour. Integrate with your application covers the authentication, the transactions, the URLs and the size of the requests, and the Reference lists the complete API.

Describe the service#

A ScimProvider describes the service: the resources it serves, and the features it supports. The following service serves users, with the enterprise extension, and groups. It supports PATCH, bulk requests, filtering, sorting and ETags:

def create_provider() -> ScimProvider:
    return ScimProvider(
        models=[User, EnterpriseUser, Group],
        resource_types=[
            ResourceType.from_resource(User[EnterpriseUser]),
            ResourceType.from_resource(Group),
        ],
        config=ServiceProviderConfig(
            patch=Patch(supported=True),
            bulk=Bulk(
                supported=True,
                max_operations=MAX_BULK_OPERATIONS,
                max_payload_size=MAX_BULK_PAYLOAD_SIZE,
            ),
            filter=Filter(supported=True, max_results=MAX_RESULTS),
            change_password=ChangePassword(supported=False),
            sort=Sort(supported=True),
            etag=ETag(supported=True),
            authentication_schemes=[],
        ),
    )

Announce filtering, sorting and ETags only when the storage implements them. Describe a SCIM service describes the provider in full.

Serve the endpoints#

The ScimServer extension serves the SCIM endpoints of a provider over a storage. InMemoryStorage keeps the resources in memory, until the process stops:

>>> from flask import Flask
>>> from scim2_server.memory import InMemoryStorage
>>> from examples.minimal_server import create_provider
>>> from scim2_flask import ScimServer
>>> app = Flask(__name__)
>>> scim_server = ScimServer(InMemoryStorage(), create_provider(), app=app)

The endpoints live under /scim/v2. To serve the resources of your own database, write a storage as Write a storage describes, or Serve an existing data model for tables your application already has. Pass it in place of the in-memory one.

Serve a request#

Your application now answers SCIM requests. The examples send them with the test client of Flask, which simulates HTTP requests without running a server. Create a user, and read it back at its location:

>>> client = app.test_client()
>>> response = client.post(
...     "/scim/v2/Users",
...     json={"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"], "userName": "bjensen"},
... )
>>> response.status_code
201
>>> user = response.json
>>> user["meta"]["location"]
'http://localhost/scim/v2/Users/...'
>>> client.get(user["meta"]["location"]).json["userName"]
'bjensen'

Errors come back as SCIM error responses, not as the HTML error pages of Flask:

>>> response = client.get("/scim/v2/Users/unknown")
>>> response.status_code, response.json["schemas"]
(404, ['urn:ietf:params:scim:api:messages:2.0:Error'])

Run the server#

examples/minimal_server.py gathers these steps in an application factory:

def create_app() -> Flask:
    app = Flask(__name__)
    scim_server = ScimServer(InMemoryStorage(), create_provider())
    scim_server.init_app(app)
    return app

Run it with the Flask command, from the directory of the file:

$ flask --app minimal_server run

Any SCIM client can then reach it at http://localhost:5000/scim/v2, such as scim2-cli.

Check a server#

scim2-tester checks that a SCIM server complies with the RFCs. Its check_server() function sends the requests an identity provider would send, and checks each response. The WSGI engine of scim2-client calls the application directly, without a network:

$ pip install scim2-tester
>>> from scim2_client.engines.wsgi import WSGISCIMClient
>>> from scim2_tester import check_server
>>> scim_client = WSGISCIMClient(app, base_url="http://localhost/scim/v2")
>>> scim_client.discover()
>>> results = check_server(scim_client)
>>> [result.title for result in results if result.status.name in ("ERROR", "CRITICAL")]
[]

In the tests of your application, ScimServerContract runs the same checks as a pytest test, with a subtest per result. It needs the testing extra:

$ pip install scim2-tester[testing]

Subclass it, and give it a scim_client fixture:

import pytest
from scim2_client.engines.wsgi import WSGISCIMClient
from scim2_tester.testing import ScimServerContract


class TestScimServer(ScimServerContract):
    @pytest.fixture
    def scim_client(self):
        return WSGISCIMClient(create_app(), base_url="http://localhost/scim/v2")