|
| 1 | +# ENT-2 — SCIM 2.0 provisioning |
| 2 | + |
| 3 | +**Status:** Draft · **Owner:** TBD · **Tier:** 2 · **Depends on:** ORG-1, ENT-1 |
| 4 | + |
| 5 | +## Summary |
| 6 | +Expose a SCIM 2.0 ([RFC 7642 / 7643 / 7644](https://datatracker.ietf.org/doc/html/rfc7644)) endpoint per organization so customer IdPs (Okta, Entra, JumpCloud, OneLogin) can auto-provision and de-provision users and groups. The IdP authenticates with a per-org bearer token; supported resources are **Users** and **Groups** (groups → org roles). |
| 7 | + |
| 8 | +## Why now |
| 9 | +- Pairs with ENT-1. Enterprise customers want both: SAML for sign-in and SCIM so removing someone from the IdP automatically revokes their access in our products. |
| 10 | +- Without it, customer admins must manually deprovision in our UI — a deal-breaker for any compliance-conscious buyer. |
| 11 | + |
| 12 | +## Goals |
| 13 | +1. Per-org SCIM endpoint with bearer-token auth. |
| 14 | +2. CRUD on Users. |
| 15 | +3. CRUD on Groups, mapping `Group.displayName` → an org role (configurable mapping reused from ENT-1). |
| 16 | +4. PATCH operations (RFC 7644 §3.5.2) for active flag, name attrs, email, group memberships. |
| 17 | +5. Filtering for `userName eq` and `displayName eq` (the two filters Okta/Entra actually use). |
| 18 | +6. Pagination (`startIndex`, `count`). |
| 19 | +7. Schema discovery endpoints. |
| 20 | +8. Deprovisioning: `active=false` PATCH terminates all sessions and refresh tokens for that user in that org. |
| 21 | +9. Audit logged. |
| 22 | + |
| 23 | +## Non-goals |
| 24 | +- SCIM filter language fully (just the few operators IdPs use). |
| 25 | +- Bulk operations endpoint (RFC 7644 §3.7) — defer. |
| 26 | +- ETags / If-Match concurrency control — defer. |
| 27 | +- Schema extensions (we'll only expose `User`, `Group`, `EnterpriseUser`). |
| 28 | + |
| 29 | +## Library |
| 30 | +- Don't pull in a heavy SCIM library — none in Python ecosystem are great. Implement directly with FastAPI + a small `scim/schemas.py` module. ~600 LOC total. |
| 31 | +- Reference: Okta and Entra agents both stick close to the spec; test against both. |
| 32 | + |
| 33 | +## Data model |
| 34 | + |
| 35 | +``` |
| 36 | +fief_organization_scim_tokens |
| 37 | + id uuid pk |
| 38 | + organization_id uuid fk organizations |
| 39 | + token_hash text -- SHA-256 |
| 40 | + label text -- e.g. "Okta production" |
| 41 | + last_used_at timestamp null |
| 42 | + created_at timestamp |
| 43 | + revoked_at timestamp null |
| 44 | + index (organization_id, revoked_at) |
| 45 | +
|
| 46 | +fief_organization_scim_external_ids |
| 47 | + id uuid pk |
| 48 | + organization_id uuid fk organizations |
| 49 | + resource_type enum('user','group') |
| 50 | + resource_id uuid -- user_id or role_id |
| 51 | + external_id text -- IdP-assigned ID |
| 52 | + unique (organization_id, resource_type, resource_id) |
| 53 | + unique (organization_id, resource_type, external_id) |
| 54 | +``` |
| 55 | + |
| 56 | +We map SCIM Groups to existing org roles. A new Group call creates a role within the org's tenant if one doesn't already exist (configurable behavior). |
| 57 | + |
| 58 | +## Endpoints (all under `/scim/v2/orgs/{org_slug}/...`) |
| 59 | + |
| 60 | +| Method | Path | Notes | |
| 61 | +|--------|-------------------------------------|------------------------------------------------| |
| 62 | +| GET | `/ServiceProviderConfig` | Capabilities | |
| 63 | +| GET | `/Schemas` | Schema list | |
| 64 | +| GET | `/ResourceTypes` | | |
| 65 | +| GET | `/Users` | Filter, paginate | |
| 66 | +| POST | `/Users` | Create (or 409 if exists) | |
| 67 | +| GET | `/Users/{id}` | | |
| 68 | +| PUT | `/Users/{id}` | Replace | |
| 69 | +| PATCH | `/Users/{id}` | RFC 7644 patch ops | |
| 70 | +| DELETE | `/Users/{id}` | Soft-deactivate (sets `active=false`) | |
| 71 | +| GET | `/Groups` | | |
| 72 | +| POST | `/Groups` | | |
| 73 | +| GET | `/Groups/{id}` | | |
| 74 | +| PUT | `/Groups/{id}` | | |
| 75 | +| PATCH | `/Groups/{id}` | Common: add/remove members | |
| 76 | +| DELETE | `/Groups/{id}` | | |
| 77 | + |
| 78 | +## Schemas (subset) |
| 79 | + |
| 80 | +### User |
| 81 | +- `userName` → email (lowercase, unique within tenant) |
| 82 | +- `name.givenName`, `name.familyName` |
| 83 | +- `emails[primary=true].value` → email (must equal userName) |
| 84 | +- `active` → soft-deactivate flag |
| 85 | +- `externalId` → IdP-assigned |
| 86 | +- `groups` (read-only listing of role memberships within the org) |
| 87 | + |
| 88 | +### Group |
| 89 | +- `displayName` → role name within the org's role table |
| 90 | +- `members[].value` → user IDs |
| 91 | +- `externalId` |
| 92 | + |
| 93 | +## Auth |
| 94 | +- Bearer token in `Authorization: Bearer <token>`. Token is opaque (random 40 bytes), stored as SHA-256 hash, scoped to the org in the URL. |
| 95 | +- Token created in admin UI, copyable once on creation, never again. |
| 96 | + |
| 97 | +## Behaviour details |
| 98 | + |
| 99 | +### Create user (`POST /Users`) |
| 100 | +- If a user with `userName` exists in the tenant: attach to org if not already, return 200/201. |
| 101 | +- Else create new user with `active=true`, email-verified (asserted by IdP), no password. |
| 102 | +- Add membership with the org's `default_role_id`. |
| 103 | +- Honor any `groups` array by also adding/removing role memberships. |
| 104 | + |
| 105 | +### Deactivate (`PATCH active=false`) |
| 106 | +- Remove user's membership from this org. |
| 107 | +- Revoke all refresh tokens issued to this user that carry this `org_id` claim. |
| 108 | +- Invalidate active session tokens with this `org_id`. |
| 109 | +- Do NOT delete the user account; they may still belong to other orgs. |
| 110 | + |
| 111 | +### Group membership PATCH |
| 112 | +- Standard ops `add`, `remove`, `replace` on `members`. |
| 113 | +- Each membership change writes an audit log entry. |
| 114 | + |
| 115 | +### Filtering |
| 116 | +- Support: `userName eq "x"`, `displayName eq "x"`, `externalId eq "x"`. Reject other filters with 400 + `scimType=invalidFilter`. |
| 117 | + |
| 118 | +## Error format |
| 119 | +All errors per RFC 7644 §3.12: JSON body with `schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"]`, `status`, `detail`, optional `scimType`. |
| 120 | + |
| 121 | +## Edge cases |
| 122 | +- IdP sends a User with `userName=Alice@FIRM.com` but our store has `alice@firm.com` — normalize to lowercase on both sides; document this clearly. |
| 123 | +- Group with same `displayName` as an existing role — match by name; if mismatch on `externalId`, treat as conflict. |
| 124 | +- User exists in two orgs: deactivation in one doesn't touch the other. |
| 125 | +- IdP-driven creation race with self-serve invitation acceptance — last-write-wins on attributes; `externalId` once set is sticky. |
| 126 | + |
| 127 | +## Telemetry |
| 128 | +- `scim.request.{resource}.{op}.{status}`, `scim.user.deprovisioned`, `scim.group.member_changed`. |
| 129 | +- Surface in admin UI: "Last sync from Okta: 14 min ago, 3 changes." |
| 130 | + |
| 131 | +## Risks |
| 132 | +- Spec compliance bugs that block customer onboarding. Mitigation: run [Okta SCIM compatibility test suite](https://developer.okta.com/docs/guides/scim-provisioning-integration-prepare/main/) and Entra "Validate SCIM" tool before each release. |
| 133 | +- Token theft → org-wide write access. Mitigation: tokens are per-org-scoped; UI shows last-used IP and timestamp; revoke is immediate. |
| 134 | + |
| 135 | +## Open questions |
| 136 | +- Auto-create roles for unknown groups, or 400? Default: auto-create with no permissions (admin must grant). Configurable. |
| 137 | +- Should SCIM be enabled-by-default once the org has SAML configured? Recommend explicit opt-in. |
| 138 | + |
| 139 | +## Sequencing |
| 140 | +~2 sprints after ENT-1. Sprint 1: ServiceProviderConfig + User CRUD + filtering + tests against Okta. Sprint 2: Group CRUD + PATCH + Entra compatibility + UI for token management. |
0 commit comments