Repository navigation
Conversation
|
|
||
| func (r *userRepository) Delete(ctx context.Context, id string) error { | ||
| var ids []string | ||
| if err := r.db.WithContext(ctx).RawQuery("UPDATE scim_users SET deleted_at = now() WHERE sso_provider_id = ? AND deleted_at IS NULL AND id = ? RETURNING id", r.tenant(ctx), id).All(&ids); err != nil { |
There was a problem hiding this comment.
🟡 Severity: MEDIUM
The SCIM DELETE path only sets scim_users.deleted_at; it does not disable the linked auth.users account or revoke its sessions. Once a SCIM record is associated through user_id, a deprovisioning request can report success while that user continues authenticating and using existing sessions.
Helpful? Add 👍 / 👎
💡 Fix Suggestion
Suggestion: The Delete method must also revoke the linked auth user's active sessions and optionally disable the auth.users account after soft-deleting the SCIM record. To accomplish this:
- Modify the SQL query to also
RETURNING id, user_idso the linked auth user's UUID is retrieved. - Create a small local struct (e.g.,
deletedScimUser { ID string; UserID *string }) to hold both returned columns. - Wrap the entire operation in a database transaction (
r.db.WithContext(ctx).Transaction(...)) so the SCIM soft-delete and session/account actions are atomic. - If
user_idis not NULL after the soft-delete, parse it as auuid.UUIDand callmodels.Logout(tx, userID)to immediately delete all active sessions for that user, preventing continued use of existing tokens. - Optionally (but recommended for complete deprovisioning), also load the auth user via
models.FindUserByID(tx, userID, false)and calluser.Ban(tx, duration)or setbanned_untilto a far-future date so new logins are also blocked even if sessions are somehow restored. - Add the necessary imports:
github.com/gofrs/uuidandgithub.com/supabase/auth/internal/modelsto the file's import block.
This change is constrained to the Delete method but involves multiple new package imports, transaction handling, and two separate database model operations, making it a multi-step change that should be reviewed holistically rather than as a single-line patch.
| } | ||
|
|
||
| var rows []scimUser | ||
| if err := r.db.WithContext(ctx).RawQuery("UPDATE scim_users SET resource = jsonb_set(?::jsonb, '{active}', coalesce(?::jsonb -> 'active', to_jsonb(active))), updated_at = now() WHERE sso_provider_id = ? AND deleted_at IS NULL AND id = ? RETURNING id, resource, active, created_at, updated_at", string(resource), string(resource), r.tenant(ctx), id).All(&rows); err != nil { |
There was a problem hiding this comment.
🟡 Severity: MEDIUM
An authenticated SCIM client can PUT active:false; this SQL only rewrites scim_users.resource and the generated SCIM active value, without propagating deactivation to the linked auth.users account or its sessions. The user can therefore continue logging in and using existing sessions despite SCIM deprovisioning.
Helpful? Add 👍 / 👎
💡 Fix Suggestion
Suggestion: The Replace function must propagate deactivation to the linked auth.users account and invalidate sessions when active: false is set. The fix requires the following coordinated changes:
-
Add
user_idto thescimUserstruct inmodels.goso the linked auth user ID is available after the SQL update:type scimUser struct { ID string `db:"id"` UserID *string `db:"user_id"` // add this Resource []byte `db:"resource"` Active bool `db:"active"` ... }
-
Extend the SQL RETURNING clause in
Replaceto also returnuser_id:... RETURNING id, user_id, resource, active, created_at, updated_at
-
Wrap the whole
Replacebody in a transaction and, after a successful update, check if the result hasActive == falseand a non-nullUserID. If so:- Use
models.FindUserByID(tx, uuid.FromStringOrNil(*rows[0].UserID), ...)to fetch the linkedauth.usersrecord. - Call
authUser.Ban(tx, 87600*time.Hour)(or a sentinel "forever" duration) to setbanned_untilon the auth user, preventing future logins. - Call
models.Logout(tx, authUser.ID)to delete all active sessions for that user, invalidating any existing tokens.
- Use
-
Handle the re-activation case symmetrically: if
Active == true, lift the ban by callingauthUser.Ban(tx, 0)(zero duration clears the ban per the existingBanimplementation).
This ensures that SCIM deprovisioning (active: false) is immediately enforced at the auth layer, both preventing new logins and terminating existing sessions.
| } | ||
|
|
||
| var rows []scimUser | ||
| if err := r.db.WithContext(ctx).RawQuery("INSERT INTO scim_users (sso_provider_id, resource) VALUES (?, ?) RETURNING id, resource, active, created_at, updated_at", r.tenant(ctx), resource).All(&rows); err != nil { |
There was a problem hiding this comment.
🟡 Severity: MEDIUM
userName and other fields from an authenticated SCIM POST are persisted without a user_id association. This creates only a shadow scim_users row, so the Auth identity is not bound to SCIM lifecycle operations; subsequent deprovisioning can leave the corresponding account and sessions usable.
Helpful? Add 👍 / 👎
💡 Fix Suggestion
Suggestion: The Create function inserts into scim_users with only (sso_provider_id, resource), never populating the user_id foreign key column defined in the migration. This severs the SCIM lifecycle from the actual auth.users identity, meaning SCIM deprovisioning (DELETE or active: false) only updates the isolated scim_users shadow row and leaves the underlying account and sessions intact.
To fix this properly, several coordinated changes are required:
-
During SCIM user creation (
Create): After inserting thescim_usersrow, look up or create the correspondingauth.usersrecord (matched by email /userName), then updatescim_users.user_idto bind the two rows. The INSERT (or a follow-up UPDATE) must includeuser_id. -
Update the
scimUsermodel (models.go): Add aUserID *string(nullable)db:"user_id"field and propagate it throughGet,List,Create,Replace, andDeletequeries. -
During SCIM user deletion (
Delete): After soft-deleting thescim_usersrow, use the storeduser_idto ban or disable the linkedauth.usersrecord and revoke all of its active sessions. -
During
Replacewhenactive: false: Similarly useuser_idto disable theauth.usersaccount and invalidate sessions, mirroring the deprovisioning path.
These changes span models.go, user_repository.go (multiple methods), and likely the auth.users management layer, making this a multi-file architectural fix rather than a single-line correction.
Add a generic Repository[T] with a map-backed in-memory implementation,
and serve GET /Users and GET /Users/{id} scoped to the SSO provider from
the x-scim-provider-id header. The repository is injected via
WithSCIMUserRepository, so a Postgres adapter can replace it once the
schema (AUTH-1460) is settled.
…til we build the Groups API
## What kind of change does this PR introduce? Feature. Add `scim_users` and `scim_tokens` tables. ## What is the current behavior? These tables do not exist. ## What is the new behavior? These tables are added to support the SCIM API. ## Additional context Extracted from #2731
What kind of change does this PR introduce?
Feature. Add SCIM endpoints for core User schema.
What is the current behavior?
These endpoints do not exist yet.
What is the new behavior?
Adds:
/scim/v2/Users/scim/v2/Users/{id}/scim/v2/Users/scim/v2/Users/{id}/scim/v2/Users/{id}/scim/v2/ResourceTypes/{id}/scim/v2/Schemas/{id}Additional context
/Usersendpoints authenticate with a bearer SCIM token. Tokens are stored hashed in the newscim_tokenstable, are revocable (revoked_at) and expirable (expires_at), and resolve to ansso_provider_idthat scopes every query to a single tenant./ResourceTypes/{id}and/Schemas/{id}routes are discovery metadata and are unauthenticated, consistent with the existing/ServiceProviderConfig,/ResourceTypes, and/Schemasendpoints.requireScimServerEnabledgate.scim_usersandscim_tokenstables. A user's SCIM payload is stored as a JSONBresourcecolumn and deletes are soft usingdeleted_at.Listrejects anyfilterparameter.