Repository navigation
Support the password attribute in the SCIM User resource - #55681
costa-chown wants to merge 2 commits into
Conversation
The core User schema gets the password attribute of RFC 7643, Section 4.1.1: writeOnly, returned never, on POST, PUT and PATCH. It is set as the password credential once the user is persisted, as the reset password endpoint of the Admin REST API does, so the password policy of the realm applies. A policy violation or a blank password is rejected with scimType invalidValue. Write-only values are read as sent on POST and PUT, not trimmed. The Service Provider Configuration reports changePassword as supported. Closes keycloak#55680 Signed-off-by: Konstantin Kauz <konstantin.kauz@opendefense.cloud>
There was a problem hiding this comment.
🟡 Changes recommended
SCIM PUT/PATCH lets callers with manage permission bypass an explicit reset-password denial, while password removal reports success without deleting the credential.
2 open issues
What changed in this PR
Adds password provisioning to Keycloak’s SCIM User resource, addressing #55680 so clients can set or rotate passwords without a separate Admin REST call.
Changes:
- Supports write-only passwords through POST, PUT, and PATCH, and advertises password-change support.
- Preserves password whitespace, applies realm password policies, and returns localized
invalidValueerrors. - Adds regression tests, administration guidance, and release notes.
| File | Description |
|---|---|
scim/tests/base/src/test/java/org/keycloak/tests/scim/tck/UserTest.java |
Tests password writes, policy rejection, response omission, and whitespace preservation. |
scim/tests/base/src/test/java/org/keycloak/tests/scim/tck/ServiceProviderConfigTest.java |
Expects password-change support. |
scim/tests/base/src/test/java/org/keycloak/tests/scim/tck/SchemaTest.java |
Verifies password schema metadata. |
scim/services/src/main/java/org/keycloak/scim/services/Error.java |
Maps invalid values to localized SCIM errors. |
scim/model/src/main/java/org/keycloak/scim/model/user/UserResourceTypeProvider.java |
Writes staged passwords after profile persistence. |
scim/model/src/main/java/org/keycloak/scim/model/user/UserModelAttributeRecorder.java |
Stages passwords separately from user attributes. |
scim/model/src/main/java/org/keycloak/scim/model/user/UserCoreModelSchema.java |
Defines the write-only password mapping and rejects blank values. |
scim/model/src/main/java/org/keycloak/scim/model/schema/SchemaResourceTypeProvider.java |
Publishes each attribute’s mutability. |
scim/model/src/main/java/org/keycloak/scim/model/config/ServiceProviderConfigResourceTypeProvider.java |
Advertises password-change support. |
scim/core/src/main/java/org/keycloak/scim/resource/user/User.java |
Adds the password field and accessors. |
scim/core/src/main/java/org/keycloak/scim/resource/spi/ScimInvalidValueException.java |
Introduces an exception for invalid attribute values. |
scim/core/src/main/java/org/keycloak/scim/resource/schema/attribute/Attribute.java |
Adds write-only mutability support. |
scim/core/src/main/java/org/keycloak/scim/resource/schema/AbstractModelSchema.java |
Preserves raw write-only values during population. |
docs/documentation/server_admin/topics/scim/managing-users.adoc |
Documents password operations, policy handling, and limitations. |
docs/documentation/server_admin/topics/scim/accessing-service-config.adoc |
Updates documented password-change capabilities. |
docs/documentation/release_notes/topics/27_0_0.adoc |
Announces SCIM password support. |
Review effort: Balanced
Setting a password through SCIM now needs the reset-password permission on the user, as the Admin REST API does; managing the user is not enough. A PATCH remove of the password is rejected with scimType invalidValue instead of being ignored, also after an add in the same request. A PATCH replace still works: its remove is followed by the new password. Closes keycloak#55680 Signed-off-by: Konstantin Kauz <konstantin.kauz@opendefense.cloud>
There was a problem hiding this comment.
I've tried to review this one thoroughly, checking RFC conformance and permission parity with the Admin REST API. Left some detailed comments inline
- Permission parity is correct in the direction that matters. canResetPassword falls back to canManage when no reset-password permission is defined, so the new DefaultPermissions branch is exactly what requireResetPassword already does, for plain RBAC, FGAP v1 and FGAP v2 alike. SCIM PUT/PATCH requires MANAGE(user) and isManageable and canResetPassword(user), which is strictly more than the Admin API's requireResetPassword(user) alone. I could not see a case where SCIM is more permissive than the Admin API. Two divergences go the other way, noted inline: the UPDATE_PASSWORD required action is never cleared, and create-with-password is stricter than POST /admin/realms/{realm}/users with credentials.
Blocking from my side: the ReadOnlyException 500, the UPDATE_PASSWORD parity gap, and caseExact. The rest are judgment calls I would like your view on rather than changes I am asking for.
Test gaps: create under FGAP v2 with a reset-password permission defined, a user federated from a read-only provider, UPDATE_PASSWORD handling on PUT/PATCH, and rollback of the sibling attribute on a denied PUT.
| try { | ||
| // as the admin API does when it resets a password; the password policy of the realm applies | ||
| user.credentialManager().updateCredential(UserCredentialModel.password(password, false)); | ||
| } catch (ModelException me) { |
There was a problem hiding this comment.
ReadOnlyException extends RuntimeException, not ModelException, so this catch misses it and the request ends as a 500 with a stack trace in the server log.
getModel only requires StorageId.isLocalStorage, which is true for users imported from a federated provider, and LDAPStorageProvider.updateCredential throws ReadOnlyException when editMode is READ_ONLY. The Admin API catches it and returns 400 with "Can't reset password as account is read only".
Separately, ModelIllegalStateException also extends ModelException, so it currently surfaces as invalidValue with the raw message as detail. UserResource.resetPassword logs it and returns 500. Worth narrowing the catch for both.
| UserModel user = recorder.getDelegate(); | ||
|
|
||
| // as the admin API: managing a user does not imply resetting its password | ||
| if (!session.getContext().getPermissions().hasPermission(user, AdminPermissionsSchema.USERS_RESOURCE_TYPE, AdminPermissionsSchema.RESET_PASSWORD)) { |
There was a problem hiding this comment.
Every other check in these providers goes through BaseResourceTypeProvider.hasPermission(model, type, scope), which additionally requires isManageable(model), that is, that the target is not an admin user. This call reaches Permissions directly and skips that guard.
I do not think it is exploitable today: PUT/PATCH already ran the guarded MANAGE check, POST creates a non-admin user, and checkGroupMembershipPermission rejects isAdminGroup, so a user cannot become an admin within the same request. But hasPermission(user, getRealmResourceType(), RESET_PASSWORD) is a one-line change that keeps the guard uniform and uses the resource type the class already defines.
| throw handleValidationException(ve); | ||
| } | ||
|
|
||
| updatePassword(recorder); |
There was a problem hiding this comment.
Authorization is enforced after the write: on POST the user row already exists from profile.create(false), and on PUT/PATCH profile.update(true) has already run. The 403 then depends entirely on setRollbackOnly in Error.toResponse.
That holds for database state, and the policy-violation tests prove it, but it makes the authorization outcome contingent on transaction handling rather than on a check. Checking the permission when the password is staged, in the model setter, would decide it before any mutation. At minimum, testPasswordDeniedWithoutResetPasswordFGAP should assert that the title sent in the same denied PUT was rolled back; right now it only asserts that no credential exists.
| attributes.addAll(Attribute.<UserModel, User>simple("password") | ||
| .writeOnly() | ||
| .returned(Attribute.RETURNED_NEVER) | ||
| .caseExact() |
There was a problem hiding this comment.
RFC 7643 Section 8.7.1 publishes "caseExact" : false for password. This sets it to true, and SchemaTest asserts true.
It has no functional effect here, since caseExact only drives filter case-folding in ScimJPAPredicateProvider and schema publication, but /Schemas is exactly what clients diff against the spec. I'd suggest dropping .caseExact() and updating the assertion.
|
|
||
| UserModel user = recorder.getDelegate(); | ||
|
|
||
| // as the admin API: managing a user does not imply resetting its password |
There was a problem hiding this comment.
This is true for the reset password endpoint, but not for create. UsersResource.createUser requires only auth.users().requireManage() and never evaluates reset-password, even when the body carries credentials, so on the POST path this check is stricter than the Admin API rather than equivalent to it.
Under FGAP v2, once any reset-password permission exists the scope is evaluated, so the fallback to MANAGE in FineGrainedAdminPermissionEvaluator is skipped, and a brand-new user matches no resource-scoped permission. A client that can create users with credentials through the Admin API would get a 403 from SCIM. It fails closed, so it is not a security problem, but it is a parity gap, no test covers create under FGAP, and the comment reads as if the two paths match.
| .writeOnly() | ||
| .returned(Attribute.RETURNED_NEVER) | ||
| .caseExact() | ||
| .modelAttributeResolver(Attribute::getName) |
There was a problem hiding this comment.
Also, I think this has a side effect worth deciding on: this makes password resolvable as a filter path. ScimJPAPredicateProvider finds no password field on UserEntity and falls back to an EXISTS over UserAttributeEntity with name = 'password', so filter=password eq "x" always returns an empty page, and filter=password pr matches any user who happens to carry an unrelated user attribute named password.
No credential is exposed, so this is not a leak, but an empty page is a silently wrong answer to a filter RFC 7643 Section 4.1.1 explicitly contemplates ("set, replace, or compare (i.e., filter for equality)"). Rejecting filters on write-only attributes with invalidFilter would be clearer than answering them incorrectly.
| throw new IllegalStateException("password can only be set while the user is persisted"); | ||
| } | ||
| if (value.isBlank()) { | ||
| throw new ScimInvalidValueException("password must not be blank"); |
There was a problem hiding this comment.
This message, and "password cannot be removed", are raw English strings, while every other SCIM error message is a message bundle key.
| user.setPassword(KeycloakModelUtils.generateId()); | ||
| user.setTitle("Denied"); | ||
| assertAccessDenied(() -> noAccessClient.users().update(user)); | ||
| assertTrue(realm.admin().users().get(managedUser.getId()).credentials().isEmpty()); |
There was a problem hiding this comment.
This asserts the credential was not created, but not that the rest of the denied PUT was rolled back. Since the permission check runs after profile.update(true), the title set on line 617 is the interesting assertion here: it is what proves the 403 left nothing behind.
| * Records a PATCH remove of the password and drops a password staged before it. A PATCH replace is a remove | ||
| * followed by an add, which stages the new password again. | ||
| */ | ||
| public void removePassword() { |
There was a problem hiding this comment.
Whether a password remove is rejected depends on where it falls in the operation list, since all operations share one recorder. testRemovePasswordRejected covers add-then-remove, which is rejected. The reverse order, {"op":"remove","path":"password"} followed by {"op":"add","path":"password","value":"..."}, clears the flag again and succeeds, so the explicit remove is silently dropped instead of rejected.
That falls out of replace being remove plus add and I am not asking to change it, but the rule is really "a remove is rejected only when it is the last password operation in the request", which is worth stating in the javadoc here and pinning with a test.
|
|
||
| Only the password credential is affected. Other credentials of the user, such as OTP or WebAuthn, are not managed through SCIM. | ||
|
|
||
| NOTE: A password set through SCIM is always permanent. SCIM has no equivalent of a temporary password, so the |
There was a problem hiding this comment.
If the UPDATE_PASSWORD parity gap is not fixed, this note should also say that an existing Update Password required action is not removed either, unlike the Admin REST API reset password endpoint, so a user with one pending still has to change the password that was just provisioned.


Closes #55680
Adds the
passwordattribute of the core User schema (RFC 7643, Section 4.1.1) to the SCIM User resource, following the shape @sguilhen outlined in #52957.passwordiswriteOnlyandreturned: never, and works onPOST,PUTandPATCH. The schema endpoint now reports the mutability of each attribute (writeOnlywas not representable before, onlyimmutableandreadWrite).credentialManager().updateCredential(UserCredentialModel.password(password, false)), as the reset password endpoint of the Admin REST API does: an unconditional overwrite, the current password is not required. Only the password credential is touched.400andscimType: invalidValue(newScimInvalidValueException). The message is the localized policy message, as forinvalidSyntax. The transaction is rolled back, so a rejectedPOSTcreates no user and a rejectedPUTchanges nothing.PUTwithoutpasswordkeeps the current password.POSTandPUT,populateModelreads attribute values throughJsonUtils.getJsonValue, which trims text and turns blank text intonull. A password must be taken as sent, so write-only attributes are read untrimmed. Otherwise" secret "would silently become"secret"on create but not onPATCH, and a blank password would be ignored instead of rejected.changePassword.supportedistruein the Service Provider Configuration.UPDATE_PASSWORDrequired action is never added. Release note in 27.0.0.Tests
UserTest: create with password (not returned on create orGET, valid credential, onepasswordcredential and nopassworduser attribute); full replace without password keeps it and with password changes it;PATCHreplace; policy violation (length(12)) on create, replace and patch returnsinvalidValueand leaves the password unchanged; blank password returnsinvalidValue; surrounding whitespace is kept on create and onPATCH.SchemaTest: the core User schema has 15 attributes;passwordiswriteOnly,returned: never,caseExact.ServiceProviderConfigTest:changePasswordis supported.