System for Cross-domain Identity Management 2.0 (RFC 7643, RFC 7644)
README.md

scim #

A SCIM 2.0 service provider and client in OCaml (RFC 7643,
RFC 7644).

An organisation's identity provider, such as Microsoft Entra ID or Okta,
creates, updates and removes its users and groups in the applications it
subscribes to. It does so with SCIM, the System for Cross-domain Identity
Management. The application is the service provider. It accepts JSON
resources over HTTP, with filters to query them, PATCH to modify them,
ETags to guard them against concurrent changes and bulk requests to batch
them.

The scim package is the service provider and the client without network I/O. The
scim-eio package serves and sends them over HTTP with Eio. A request that
RFC 7644 refuses gets a SCIM error response. The requests that identity
providers are known to send against the RFC are accepted only when the
server's configuration lists them.

Installation #

Both packages need OCaml 5.1 or later. They are not in the public
opam-repository; add the overlay repository, then install them:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install scim scim-eio

Usage #

Scim.Server.Make builds a service provider over a store. Scim.Memory is
a store in memory, whose tenants are strings. The id function names each
new resource:

module S = Scim.Server.Make (Scim.Memory)

let server ?quirks () =
  let n = ref 0 in
  let id () = incr n; Fmt.str "id-%d" !n in
  S.v
    (Scim.Server.config ?quirks ~id ~base:"https://example.com/v2" ())
    (Scim.Memory.v ())

let call s request = S.incoming s "tenant-a" ~now:Ptime.epoch request

A POST creates a user and a filter finds it:

# let s = server ();;
val s : S.t = <abstr>
# let user = {|{"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],"userName":"bjensen"}|};;
val user : string =
  "{\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"],\"userName\":\"bjensen\"}"
# let { Scim.Response.status; location; _ } = call s (Scim.Request.v ~body:user `POST "/Users");;
val status : int = 201
val location : string option = Some "https://example.com/v2/Users/id-1"
# let found = call s (Scim.Request.v ~query:[ ("filter", {|userName sw "bj"|}) ] `GET "/Users") in
  Option.bind found.body (Json.Value.Find.int [ "totalResults" ]);;
- : int option = Some 1

Microsoft Entra ID sends "op": "Replace" in a PATCH, where RFC 7644
section 3.5.2 has "replace". The server refuses it unless its
configuration lists Scim.Quirk.Operation_case:

# let patch = {|{"schemas":["urn:ietf:params:scim:api:messages:2.0:PatchOp"],"Operations":[{"op":"Replace","path":"active","value":false}]}|};;
val patch : string =
  "{\"schemas\":[\"urn:ietf:params:scim:api:messages:2.0:PatchOp\"],\"Operations\":[{\"op\":\"Replace\",\"path\":\"active\",\"value\":false}]}"
# (call s (Scim.Request.v ~body:patch `PATCH "/Users/id-1")).status;;
- : int = 400
# let s = server ~quirks:[ Scim.Quirk.Operation_case ] ();;
val s : S.t = <abstr>
# (call s (Scim.Request.v ~body:user `POST "/Users")).status;;
- : int = 201
# (call s (Scim.Request.v ~body:patch `PATCH "/Users/id-1")).status;;
- : int = 200

Scim_eio.Server turns such a server into the routes of a respond-eio
HTTP server, and Scim_eio.Client sends the requests of Scim.Client with
requests-eio.

API #

lib/scim.mli is the entry point of scim:

  • Scim.Server is the service provider over a store. Scim.Store is the
    interface of a store and Scim.Memory a store in memory.
  • Scim.Client builds the requests an identity provider sends and reads
    the answers.
  • Scim.Attribute, Scim.Schema and Scim.Resource_type hold the core
    schema of RFC 7643 (User, Group and the Enterprise User extension) and
    the schemas a vendor adds.
  • Scim.Attr_path, Scim.Filter and Scim.Path read attribute names,
    filters (RFC 7644 section 3.4.2.2) and PATCH paths (section 3.5.2).
  • Scim.Resource checks what a client sends in a POST or a PUT, and
    selects what a response returns (attributes, excludedAttributes).
  • Scim.Patch applies PATCH operations.
  • Scim.Version computes ETags and evaluates If-Match and
    If-None-Match.
  • Scim.Query reads queries and cuts their results into pages.
  • Scim.Bulk reads bulk requests and runs their operations, with bulkId
    references and failOnErrors.
  • Scim.Quirk names the requests against the RFC that a server may accept.
  • Scim.Reconcile reports how the resources an identity provider expects
    differ from those a service provider holds.

lib/eio/scim_eio.mli is the entry point of
scim-eio.

Design #

A server is a function of a store and a request #

Each SCIM request stands alone. A request depends on an earlier one only
through the resources that one changed. The service provider therefore
keeps no session: its state is the store, and S.incoming takes a request
to a response. The library takes the time as an argument. Password writes
obtain fresh salt from the operating system's cryptographic random source;
other operations perform no I/O.
The store is a module the caller supplies (Scim.Store.S), and with
Scim.Memory a whole service provider runs in a test with no network.

Each request names its tenant. Every operation on the store is scoped to
that tenant (RFC 7644 section 6), so the identifiers, the uniqueness keys
and the queries of one tenant never reach another. Over HTTP, the caller
resolves the tenant from the request's authorisation.

Resources stay JSON #

A resource is a Json.t of nox-json. Its schema is data: a vendor adds
attributes and extensions at run time, and one resource carries attributes
of several schemas. A stored resource spells each attribute name as its
schema does. Lookups are then exact, although names are case-insensitive in
requests (RFC 7643 section 2.1).

A password write validates the RFC 8264 FreeformClass and applies the RFC 8265
OpaqueString profile before hashing: non-ASCII spaces become ASCII spaces,
Unicode is normalized to NFC, and case and width are preserved. POST, PUT and
PATCH reject malformed or disallowed strings. Password equality filters apply
the same preparation before verification; the server returns no password value.

A change is computed, then stored #

A POST or a PUT goes through Scim.Resource and a PATCH through
Scim.Patch.apply, which has no effect on the store. A PATCH is atomic
(RFC 7644 section 3.5.2) because the server stores its result or nothing.

The store writes a resource only if its version is the one the server
read, so a concurrent update is never lost. In the same write it keeps
unique the attributes whose uniqueness is server or global.

The version of a resource is the hash of its canonical JSON (RFC 8785)
without meta. It changes exactly when the resource does, and a PATCH that
changes nothing keeps the resource's lastModified (section 3.5.2.1).

Requests against the RFC #

Scim.Quirk.t lists the deviations a server may accept. The list in a
server's configuration is empty by default, and an accepted deviation is
read as the request the RFC defines.

Quirk What the identity provider sends
Operation_case "op": "Replace" in a PATCH
Boolean_string "active": "False", a boolean as a string
Remove_by_value a remove of group members that lists them in "value"
Operator_spacing value eq"x", with no space after the operator
Path_names a PATCH value with no path, keyed by "name.givenName"
Undeclared_schema a "schemas" URI that the resource type does not declare

Contradictions in the RFCs #

  • RFC 7644 section 3.14 sends weak ETags in If-Match. RFC 9110
    section 13.1.1 compares If-Match strongly, so no weak tag would ever
    match. scim compares both If-Match and If-None-Match weakly.
  • RFC 7643 section 8.7.1 leaves primary out of a User's addresses and
    display out of a Group's members. Section 8.2 and the PATCH examples
    of RFC 7644 send both, and section 2.4 makes both default sub-attributes
    of a multi-valued attribute. Scim.Schema accepts them.
  • RFC 7644 section 3.5.2.2 removes a member with value eq"...", which
    the grammar of its figure 1 refuses. Scim.Filter refuses it unless
    Operator_spacing is configured.

Bounds on the work a client can cause #

bench/cost.ml applies each input at two sizes, ten times apart, and
counts the words it allocates. dune exec bench/cost.exe runs it.

  • A filter is read in one pass, in time linear in its length: 37k and 368k
    words for 100 and 1000 comparisons. Scim.Filter.default_limits allows
    4096 bytes and 32 levels of brackets.
  • A query evaluates its filter once per resource of the tenant's type: 47k
    and 443k words over 100 and 1000 users. A page returns at most
    max_results resources, 1000 by default.
  • A password equality or inequality filter verifies salted hashes at most
    max_password_checks times per request (2 by default). Before evaluation,
    the server counts the resources and password comparisons across a root or
    typed query; it returns SCIM tooMany if the upper bound exceeds the budget.
    The server admits one password query at a time by default, across tenants
    and HTTP sessions, so simultaneous queries cannot multiply that KDF budget.
    At the default settings the measured accepted request uses about 30 million
    allocated words (about 241 MiB) and under two seconds on the measurement
    host; a 20-user request is refused before verification.
  • A PATCH request holds at most 1000 operations by default
    (Scim.Patch.default_limits). Applying them is linear in their number:
    0.6M and 5.8M words for 100 and 1000 add operations.
  • A bulk request of more than 1 MiB is refused before it is parsed, and so
    is one of more than 1000 operations (Scim.Bulk.default_limits). Each
    operation runs at most once and a bulkId is looked up in a map, so a
    run is linear in its operations. It allocates 1.0M and 11.5M words for
    100 and 1000 POSTs through the server and Scim.Memory.
  • Over HTTP, the caller bounds the body of a request with the
    max_body_size of Respond_eio.run.

Interoperability #

The tests replay, with no network, what other implementations do with the
same requests and what identity providers send.

  • test/interop/filter-parser holds the filters of RFC 7644 figure 2 and
    the PATCH paths of figure 8, as
    scim2/filter-parser (Go) reads them. The tests compare
    them with Scim.Filter and Scim.Path.
  • test/interop/elimity holds the PATCH requests of RFC 7644 section 3.5.2
    and of the identity providers' documentation, as
    elimity-com/scim (Go) reads them. The tests compare them with
    Scim.Patch. That library validates a PATCH and leaves applying it to
    its caller.
  • test/idp replays the requests Microsoft Entra ID and Okta document
    through Scim.Server, with and without the quirks.

Each test lists the requests where the implementations differ, and the RFC
text that decides. REGEN=1 dune build @traces records the Go traces again.

  • UnboundID's SCIM 2 SDK and Apache Syncope are the
    Java implementations.
  • elimity-com/scim (Go, MIT) is a service-provider library, with
    scim2/filter-parser for filters.
  • scim2-server (Python, Apache-2.0) is a server built on
    the scim2-models types.
  • jsonptr implements JSON Pointer (RFC 6901). SCIM's PATCH paths are a
    different notation, which selects values by filter and not by index.

scim uses nox-json for JSON and nox-http to compare entity tags.
scim-eio uses respond-eio for the HTTP server and requests-eio for
the client.

References #

Licence #

ISC. See LICENSE.md.