How to Implement SCIM 2.0 User Provisioning
Build SCIM endpoints so an identity provider can create, update and deactivate users automatically, with correct filtering and ETags.
On this page
Introduction
In an enterprise, users are created and removed in the identity provider, not in each application. Without automation, onboarding and offboarding become manual, error-prone tasks, and a departed employee keeps access for weeks. SCIM 2.0 (System for Cross-domain Identity Management) is the standard that lets an identity provider push users and groups into your application over a simple REST API with a JSON schema. Implementing SCIM correctly means your app is wired into the directory, so deactivation in the directory deactivates the account in your app.
The SCIM Model
SCIM defines a User resource with a fixed core schema (userName, name, emails, active, and so on) plus an extension mechanism for provider-specific attributes. A Group resource carries a list of member references. The identity provider (the SCIM client) sends create, update, patch and delete requests; your application is the SCIM service provider.
The essential behaviour is not the schema but the sync semantics: when the provider creates a user, your app creates one; when it sets active to false, your app must immediately block sign-in; when it deletes, your app must revoke access. Provisioning is only useful if deprovisioning is reliable, so the active flag and delete handling are the parts that matter most.
{
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
"userName": "[email protected]",
"name": { "givenName": "Ada", "familyName": "Lovelace" },
"emails": [{ "value": "[email protected]", "primary": true }],
"active": true,
"externalId": "00u1a2b3c4d5e6f7g8"
}
Endpoints and HTTP Semantics
SCIM uses resource-oriented URLs: POST /Users to create, GET /Users/{id} to read, PUT /Users/{id} to replace, PATCH /Users/{id} for partial updates, DELETE /Users/{id} to remove. Responses use the SCIM JSON format with a schemas array, a unique id, and a meta object carrying resourceType, created and lastModified.
Use standard HTTP status codes and the SCIM error format: 201 on create with a Location header, 200 on read and update, 204 on delete, 400 on malformed input, 404 on missing resources, 409 on conflicts, and 501 for unsupported operations rather than silently succeeding. The provider relies on these codes to know whether its change applied.
// Minimal create-user handler shape
export async function createUser(body: ScimUser, db: Db): Promise<{ status: number; user?: ScimUser }> {
if (!body.userName) return { status: 400 };
const existing = await db.findUserByExternalId(body.externalId ?? '');
if (existing) return { status: 409 }; // conflict: already provisioned
const user = await db.createUser({
userName: body.userName,
externalId: body.externalId,
active: body.active ?? true,
});
return { status: 201, user };
}
Filtering and Pagination
The provider finds users by filtering, for example GET /Users?filter=userName eq “[email protected]”. You must parse the SCIM filter syntax for at least the equality and and/or operators, because that is how the provider checks whether a user already exists before creating one. Returning an empty list when a user exists causes the provider to create a duplicate on every sync.
Pagination uses startIndex and count, and the response includes totalResults, itemsPerPage and the items. Implement a sane maximum page size and honour the provider’s count, so a large directory sync stays bounded. Stable ordering matters so paginated reads do not skip or duplicate users as the directory changes.
GET /Users?filter=userName eq "[email protected]"&startIndex=1&count=100
Response:
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"totalResults": 1,
"startIndex": 1,
"itemsPerPage": 1,
"Resources": [ { "id": "2819c223...", "userName": "[email protected]", "active": true } ]
}
PATCH and ETag Concurrency
SCIM PATCH uses the SCIM patch operation format (op: add/replace/remove, path, value), not JSON Patch or JSON Merge Patch. You must implement at least replace on active and on userName, because toggling active is how the provider deactivates a user. Respond 200 with the updated resource, or 204 when there is no body.
To avoid lost updates under concurrent syncs, support ETags: return an ETag on reads and require If-Match on updates, replying 412 on a mismatch. Without optimistic concurrency, two overlapping sync jobs can overwrite each other and leave the app inconsistent with the directory.
PATCH /Users/2819c223-7f76-453a-919d-413861904646
Content-Type: application/scim+json
If-Match: W/"e180ee84f0671b1"
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
"Operations": [
{ "op": "replace", "path": "active", "value": false }
]
}
Deactivation and Deprovisioning
Setting active to false is the normal deprovisioning signal, and it must take effect immediately: the user cannot sign in, existing sessions are invalidated, and tokens issued to them stop working. Treat deactivation as a security event, not a soft flag, because a departed employee with a live session is exactly the risk SCIM exists to close.
Full deletion may come as DELETE, but many providers only deactivate. Handle both: delete removes the user or marks them deleted, and deactivate blocks access while retaining the record for audit. Never leave a deactivated user able to authenticate through a second path such as a personal token; enumerate every sign-in method when implementing deactivation.
async function deactivate(externalId: string) {
const user = await db.findUserByExternalId(externalId);
if (!user) return;
await db.updateUser(user.id, { active: false });
await sessionStore.revokeAllForUser(user.id); // kill live sessions
await apiTokenStore.revokeAllForUser(user.id); // kill personal tokens
await auditLog.write({ action: 'scim.deactivate', userId: user.id });
}
Authentication and Reliability
The SCIM endpoint is a privileged admin API, so authenticate it with a bearer token issued to the provider, rotate that token, and never expose the endpoint without TLS. Authorise it to a single tenant’s scope so a token from one customer’s directory cannot modify another’s users.
Make operations idempotent where the provider retries. A repeated create should return a conflict or the existing user, not a duplicate. A repeated deactivate should be a no-op success. Log every SCIM mutation with the external id, so an unexpected change can be traced back to the directory event that caused it.
# SCIM endpoint hardening checklist
[ ] bearer token, scoped to one tenant, rotated periodically
[ ] TLS only; reject plaintext
[ ] idempotent create/update; 409 on duplicates, not silent dupes
[ ] deactivate revokes sessions AND tokens AND SSO access
[ ] audit every mutation with externalId + timestamp
[ ] support ETag/If-Match to prevent lost updates
Common Pitfalls and Conclusion
Do not ignore filtering; duplicate users follow. Do not return a generic 200 for unsupported operations; the provider needs an accurate status. Do not treat deactivation as a soft delete that leaves sessions alive. Do not skip ETags under concurrent sync. And do not forget that SCIM is reciprocal in importance: provisioning is convenient, deprovisioning is the security feature.
SCIM turns your application into a first-class member of the corporate directory. Implement the core User and Group resources, correct HTTP semantics and error codes, filter support for existence checks, PATCH for active toggling, ETags for concurrency, and deactivation that revokes every access path immediately. The value is not automated onboarding; it is automated, reliable offboarding.
Sources: RFC 7643: SCIM Core Schema, RFC 7644: SCIM Protocol, Okta: SCIM provisioning.
FAQ
- What is the most important SCIM operation to get right?
- Deactivation. Setting active to false must immediately block sign-in and revoke existing sessions and tokens. Reliable deprovisioning is the security benefit that justifies SCIM; provisioning is the convenience.
- Why does the provider create duplicate users?
- Usually because filtering is not implemented, so the provider cannot find an existing user and creates another. Implement at least userName equality filtering so the existence check works before each create.
Report a problem with this articleOpens a pre-filled issue on GitHub.