SCIM provisioning
For: an app developer whose customers want their directory to add and remove their staff in the app.
What it does: core serves SCIM 2.0 (RFC 7643 and RFC 7644) for Users and Groups under <basePath>/scim/v2. The directory of an organization calls it with a SCIM token of that organization. A SCIM group can give a role, a team, or both.
Before you start
Section titled “Before you start”- A verified domain of the organization, by the steps of Enterprise SSO. A SCIM create needs an address in a verified domain.
- An SSO connection of the organization, for the role map. The Enterprise SSO steps add one.
- Teams, when a group must give a team.
- Add
scimto the config, besideorganizationsandsso. - Set
scim.groupstoroles,teams, orboth. - Set
scim.defaultRole, the role of a person that no mapped group names.
import { type Auth, createAuth, type DnsTxtPort, dohCloudflare, dohGoogle, type MailPort, type StorageAdapter,} from "@authtempo/core";
/** * A SCIM server for the directory of each organization. A SCIM create needs * an address in a verified domain of the organization, so `scim` comes with * `sso` and its resolvers. A SCIM group gives a role through the role map. */export function createScimAuth( deps: { storage: StorageAdapter; mail: MailPort; secret: string }, dns?: DnsTxtPort[],) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, organizations: {}, sso: { oidc: true, dns: dns ?? [dohCloudflare(fetch), dohGoogle(fetch)] }, scim: { // A group gives a role through the role map. "teams" gives a team per group, "both" both. groups: "roles", // The role of a person that the directory adds and that no mapped group names. defaultRole: "member", }, });}
/** The app's admin page as the actor of each SCIM call. Each audit row names it. */const ADMIN = { kind: "app", name: "admin-page" } as const;
/** * The token for the directory, and the role map. The clear token appears * once: paste it into the directory with the base URL * `https://api.example.com/auth/scim/v2`. */export async function connectDirectory(auth: Auth, organizationId: string) { const { sso, scim } = auth.api; if (!sso || !scim) throw new Error("sso or scim is off"); // The role map lives on the first enabled connection of the organization. const connection = await sso.connections.create({ actor: ADMIN, organizationId, kind: "oidc", label: "Acme staff", issuer: "https://login.acme.example", clientId: "authtempo", clientSecret: "the client secret of the provider", }); if (!connection.ok) throw new Error(connection.code); // A member of the SCIM group "Admins" gets the role admin. SCIM never gives owner. const map = await scim.setRoleMap({ actor: ADMIN, organizationId, map: { Admins: "admin" } }); if (!map.ok) throw new Error(map.code); const token = await scim.tokens.create({ actor: ADMIN, organizationId, name: "Entra ID" }); if (!token.ok) throw new Error(token.code); return token.value.token;}- Create a token:
client.scim.tokens.create({ organizationId, name })on the owner’s page. - Show the clear token once. Core keeps only its SHA-256.
- In the directory, enter the base URL
<baseURL><basePath>/scim/v2and the token. - Set the role map:
scimRolesof the connection, by group name. - Start the provisioning in the directory.
import type { Auth, DnsTxtPort } from "@authtempo/core";import { describe, expect, it } from "vitest";import { API, call, harness, signIn } from "../support/harness.ts";import { connectDirectory, createScimAuth } from "./scim.ts";
const USER = "urn:ietf:params:scim:schemas:core:2.0:User";const GROUP = "urn:ietf:params:scim:schemas:core:2.0:Group";const PATCH = "urn:ietf:params:scim:api:messages:2.0:PatchOp";
/** One request of the directory, with its token. */function scim(auth: Auth, token: string, method: string, path: string, body?: unknown) { return auth.handle( new Request(`${API}/auth/scim/v2${path}`, { method, headers: { authorization: `Bearer ${token}`, ...(body !== undefined ? { "content-type": "application/scim+json" } : {}), }, ...(body !== undefined ? { body: JSON.stringify(body) } : {}), }), );}
/** An organization of an owner from another domain, with acme.example verified through two test resolvers. */async function setup() { const h = harness(); const zone = new Map<string, string[]>(); const dns: DnsTxtPort[] = ["a", "b"].map((name) => ({ name, txt: async (n: string) => zone.get(n) ?? [], })); const auth = createScimAuth(h, dns); const owner = await signIn(auth, h, "owner@owner.example"); const made = await call( auth, "POST", "/auth/organizations/create", { slug: "acme", name: "Acme" }, { cookie: owner }, ); const { organization } = (await made.json()) as { organization: { id: string } }; const app = { kind: "app", name: "admin-page" } as const; const added = await auth.api.sso?.domains.add({ actor: app, organizationId: organization.id, domain: "acme.example", }); if (!added?.ok || added.value.record === null) throw new Error("no domain"); zone.set(added.value.record.name, [added.value.record.value]); const verified = await auth.api.sso?.domains.verify({ actor: app, id: added.value.id }); if (!verified?.ok) throw new Error("not verified"); return { h, auth, organizationId: organization.id };}
describe("SCIM provisioning", () => { it("creates a person, gives a role by group, refuses another domain, and ends access at a deactivate", async () => { const { h, auth, organizationId } = await setup(); const token = await connectDirectory(auth, organizationId); expect(token).toMatch(/^sk_[A-Za-z0-9_-]{43}$/);
// Okta sends a placeholder password in each create. Core ignores it. const created = await scim(auth, token, "POST", "/Users", { schemas: [USER], userName: "ada@acme.example", name: { givenName: "Ada", familyName: "Lovelace" }, emails: [{ value: "ada@acme.example", primary: true }], active: true, password: "placeholder", }); expect(created.status).toBe(201); expect(created.headers.get("content-type")).toContain("application/scim+json"); const ada = (await created.json()) as { id: string; password?: unknown }; expect(ada.password).toBeUndefined(); const members = () => h.storage.findMany("member", []); const adaMember = async () => (await members()).find((m) => m.role !== "owner"); expect((await adaMember())?.role).toBe("member");
// The group "Admins" is in the role map, so its member gets the role admin. const group = await scim(auth, token, "POST", "/Groups", { schemas: [GROUP], displayName: "Admins", members: [{ value: ada.id }], }); expect(group.status).toBe(201); expect((await adaMember())?.role).toBe("admin"); // With enforce off, Ada can still sign in by her own methods, here a magic link. const adaSession = await signIn(auth, h, "ada@acme.example"); expect( (await call(auth, "GET", "/auth/session", undefined, { cookie: adaSession })).status, ).toBe(200);
// An address outside the verified domains creates nothing. const outside = await scim(auth, token, "POST", "/Users", { schemas: [USER], userName: "bob@other.example", emails: [{ value: "bob@other.example", primary: true }], }); expect(outside.status).toBe(400); expect(((await outside.json()) as { scimType: string }).scimType).toBe("invalidValue");
// A deactivate removes the member row and ends the person's sessions at once. const off = await scim(auth, token, "PATCH", `/Users/${ada.id}`, { schemas: [PATCH], Operations: [{ op: "replace", value: { active: false } }], }); expect(off.status).toBe(200); expect(await adaMember()).toBeUndefined(); expect( (await call(auth, "GET", "/auth/session", undefined, { cookie: adaSession })).status, ).toBe(401); // The account stays: only the person deletes an account. const users = await h.storage.findMany("user", [ { field: "email", op: "eq", value: "ada@acme.example" }, ]); expect(users).toHaveLength(1); });});How it behaves
Section titled “How it behaves”The token
Section titled “The token”- A token holds
sk_and 43 characters. It takes the scopesscim.users.read,scim.users.write,scim.groups.read, andscim.groups.write. With no list it takes all four. expiresInDaysis 1 to 730. With no value, the token never expires.- An organization holds at most 2 live tokens. To rotate, create the new token, move the directory to it, then revoke the old one. A third token answers
token_limit. - The organization of each call is the organization of the token, never a value of the path or the body.
- A wrong token answers 401. Each failure counts on
scim-auth:ip:<ip>, 60 per 15 minutes. A valid token counts onscim:token:<id>, 1200 per 10 minutes.
The address rule
Section titled “The address rule”A create, and a change of the address, needs the address in a verified domain of the token’s organization. Else core answers 400 invalidValue, and creates nothing. This rule stops a token from taking an account outside the domains of its organization. A user who exists at the address gets a member row, and is not copied.
The role map
Section titled “The role map”The map is scimRoles of the connection: { "<group name>": "<role>" }. Set it with client.sso.connections.update or auth.api.scim.setRoleMap. The map of an organization is the map of its enabled connection that it created first.
- A member gets the highest-ranked role of its mapped groups, else
scim.defaultRole. - SCIM never gives or takes
owneror the platform role. A person whom the app made an owner stays an owner. - A change of the map changes no role at once. A member gets the role of the new map at the next change of its groups, or at a rename of a mapped group.
With scim.groups set to teams or both, each group has one team. A group create creates the team, and a member change is a team member change. A SCIM team write fires no team hook. An app that watches the team hooks must also watch hooks.scim.afterChange, which reports each write after its commit.
A deactivate and a delete
Section titled “A deactivate and a delete”active: falseremoves the member row. When a verified domain of the organization claims the address, the person’s access ends at once. Every session, OAuth refresh token, and API token of the person ends. Each organization token that the person made in the organization goes too.- The sessions that do not fit the call budget wait for the sweep. Each door refuses them at once all the same.
- The record of a deactivate also refuses a session or a refresh token that begins in the 5 seconds after it. That bound covers a server whose clock runs ahead.
active: truegives back the role of the groups, and not the teams. The teams come back at the next member change of each group.- A PUT with no
activereads asactive: true, as RFC 7644 says, so it reactivates the person. - A delete runs the deactivate, then deletes the SCIM record. The account stays: only the person deletes an account.
- With
enforceoff, a deprovisioned person can still sign in by their own methods. A deactivated person who signs in through the connection getsaccount_disabled.
A group write can race a deactivate. Core then runs the deactivate again, up to 5 rounds. On D1 Free, a few races in one request can still answer 409, and the person keeps access until the directory sends it again. For example, a delete answers 409 after 3 lost rounds at the member read, or after 2 at the member delete. The routes page lists each such case.
A create can race another request on the same user. On D1, a magic link or another proof can verify an unverified user before the create claims it. On D1 and the SQLite adapter, an account delete of the user can stop the create. Core then answers 409 with no scimType, and keeps no row of the create. The detail is “Another request changed this resource at the same time. Send the request again.” The directory sends the same request again. On D1, that request links the verified user. A userName in use still answers 409 uniqueness, and a resend does not pass.
A person can move the account to an address of their own. When no verified domain of the organization claims the current address, a change of the address and a reactivate answer 400 mutability. So the directory cannot take the account back into the domain. A deactivate and a delete still run.
What core refuses
Section titled “What core refuses”| Request | Answer |
|---|---|
POST /Bulk, GET /Me, and each /.search |
501 |
A password in a PATCH |
400 invalidValue. The password change is off |
| A filter outside the grammar of the routes page | 400 invalidFilter |
sortBy or sortOrder |
400 invalidValue |
A PATCH path with an index, or an op move or copy |
400 invalidPath |
| A path to an attribute that the schema does not know | 400 invalidPath |
A schemas URN that core does not serve, such as a custom extension |
400 invalidSyntax |
A remove of userName or of a group displayName, or a replace of it with null |
400 mutability |
| A body over 1 MiB | 413 |
| A body that does not arrive within 10 s | 408 |
A body nested deeper than 16 levels, or with more than 65536 items of [, {, and , outside its strings |
400 invalidSyntax |
| A request that names more than 1000 members of a group, or takes away more than 1000 | 400 invalidValue |
To fix a custom extension URN, remove the custom mapping in the directory, or ask for the extension.
What core accepts from a directory
Section titled “What core accepts from a directory”- An attribute that the schema does not know: core ignores it, stores nothing of it, and answers the User as stored.
- A
passwordin a POST or a PUT: core ignores it, because Okta sends a placeholder in each create. - An
opwith a capital letter, such asReplace, and the strings"True"and"False"for a boolean, as Entra ID sends them. - A PATCH with no
path, as Entra ID and Okta send it. - The Group URN
http://schemas.microsoft.com/2006/11/ResourceManagement/ADSCIM/2.0/Groupof Entra ID. Core reads nothing of it. - The manager of Entra ID as a list of one object.
- The email match of Entra ID: the filter
emails[type eq "work"].value eq "<address>". - A remove of a person who is not a member: 204, and nothing changes. A directory sends a removal again after a time-out.
- One request names at most 1000 members of a group, inside the D1 Free budget of 50 queries. The count holds the members of a POST or a PUT body, or the values of a PATCH. It takes away at most 1000 members too. So a PUT of 1000 members that replaces 1000 other members passes. The cap is the same on D1, SQLite, Postgres, and MySQL. Okta sends up to 1000 members in one group PATCH, so its batch fits.
- After a write of a value with
primary: true, each other value of that attribute getsprimary: false.
The known gaps
Section titled “The known gaps”- A PUT with more than 1000 members answers 400, and so does a PUT that takes away more than 1000 members. An Okta app of the App Integration Wizard sends a PUT with every member of a group. Push the group again in smaller steps.
- A write of 1000 members whose role changes make three or more pairs of an old and a new role can answer 400. D1 needs room in its budget to reverse a write that fails late, and every store keeps the same rule. The undo of the refusal restores the group row and the team row, so nothing changes. An Okta push can meet it. Take 999 adds and 1 removal to the mapped group
Admins. 300 of the added persons hold the role of the mapped groupWeb. Okta marks the push failed. Push the group in smaller steps then: 500 members with 3 pairs fit, and 333 members with 4 pairs. - A list with no filter reads every User of the organization for
totalResults. On Postgres with 10 000 Users, it took 29.7 ms at the median and 40.6 ms at the most (10 runs, load 14, measured 2026-10-24).
If it fails
Section titled “If it fails”token_limit: the organization holds 2 live tokens. Revoke one first.unauthorized: the call to a token route has no session.invalid_body: the body or the query does not match the route.second_factor_required: the owner has no second factor.session_not_fresh: prove it again, then call once more.forbidden_rank: the caller is not an owner of the organization.operator_required: a platform operator failed the operator guard.impersonation_session: an impersonation session cannot create or revoke a token.account_disabled: the directory deactivated the person.rate_limited: see the SCIM keys of Limits.
A SCIM route answers the SCIM error message of RFC 7644. A value in use has the scimType uniqueness, and a race has no scimType.