Repository navigation
Support the password attribute in the SCIM User resource #55681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.scim.resource.spi; | ||
|
|
||
| /** | ||
| * Exception thrown when a value is not acceptable for an attribute, for example a password that does not meet the | ||
| * password policy of the realm. It maps to the SCIM error type {@code invalidValue} (RFC 7644, Section 3.12). | ||
| * | ||
| * <p>The message may be a key of the message bundle, formatted with the given parameters. | ||
| */ | ||
| public class ScimInvalidValueException extends RuntimeException { | ||
|
|
||
| private final Object[] parameters; | ||
|
|
||
| public ScimInvalidValueException(String message, Object... parameters) { | ||
| super(message); | ||
| this.parameters = parameters; | ||
| } | ||
|
|
||
| public Object[] getParameters() { | ||
| return parameters; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import org.keycloak.scim.resource.Scim; | ||
| import org.keycloak.scim.resource.schema.attribute.Attribute; | ||
| import org.keycloak.scim.resource.spi.MembershipChange; | ||
| import org.keycloak.scim.resource.spi.ScimInvalidValueException; | ||
| import org.keycloak.scim.resource.user.Email; | ||
| import org.keycloak.scim.resource.user.GroupMembership; | ||
| import org.keycloak.scim.resource.user.Name; | ||
|
|
@@ -153,6 +154,35 @@ protected Map<String, Attribute<UserModel, User>> getAttributeMappers() { | |
| } | ||
| ) | ||
| .build()); | ||
| // RFC 7643, Section 4.1.1: write-only and never returned. The value is staged and set as a | ||
| // credential when the user is persisted, so it passes the password policy of the realm. | ||
| attributes.addAll(Attribute.<UserModel, User>simple("password") | ||
| .writeOnly() | ||
| .returned(Attribute.RETURNED_NEVER) | ||
| .caseExact() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RFC 7643 Section 8.7.1 publishes "caseExact" : false for password. This sets it to true, and It has no functional effect here, since |
||
| .modelAttributeResolver(Attribute::getName) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I think this has a side effect worth deciding on: this makes password resolvable as a filter path. 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 |
||
| .withModelSetter((TriConsumer<UserModel, String, String>) (model, name, value) -> { | ||
| if (value == null) { | ||
| return; | ||
|
costa-chown marked this conversation as resolved.
Resolved
|
||
| } | ||
| if (!(model instanceof UserModelAttributeRecorder recorder)) { | ||
| throw new IllegalStateException("password can only be set while the user is persisted"); | ||
| } | ||
| if (value.isBlank()) { | ||
| throw new ScimInvalidValueException("password must not be blank"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This message, and "password cannot be removed", are raw English strings, while every other SCIM error message is a message bundle key. |
||
| } | ||
| recorder.setPassword(value); | ||
| }, (BiConsumer<User, Object>) (user, value) -> { | ||
| // never returned | ||
| }) | ||
| // PATCH remove, also as the first half of a PATCH replace; rejected when the user is persisted | ||
| // unless a new password follows | ||
| .withModelRemover((model, name, values) -> { | ||
| if (model instanceof UserModelAttributeRecorder recorder) { | ||
| recorder.removePassword(); | ||
| } | ||
| }) | ||
| .build()); | ||
| attributes.addAll(Attribute.<UserModel, User>simple("meta.created") | ||
| .timestamp() | ||
| .immutable() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |
| public class UserModelAttributeRecorder extends UserModelDelegate { | ||
|
|
||
| private final Map<String, List<String>> attributes; | ||
| private String password; | ||
| private boolean passwordRemoved; | ||
|
|
||
| public UserModelAttributeRecorder(UserModel delegate, Map<String, List<String>> attributes) { | ||
| super(delegate); | ||
|
|
@@ -33,6 +35,34 @@ public void removeAttribute(String name) { | |
| attributes.put(name, List.of()); | ||
| } | ||
|
|
||
| /** | ||
| * Stages a new password. It is set as a credential once the user profile is persisted. | ||
| */ | ||
| public void setPassword(String password) { | ||
| this.password = password; | ||
| this.passwordRemoved = false; | ||
| } | ||
|
|
||
| /** | ||
| * 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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whether a password remove is rejected depends on where it falls in the operation list, since all operations share one recorder. 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. |
||
| this.password = null; | ||
| this.passwordRemoved = true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} if the password was removed and no new one was staged after that. | ||
| */ | ||
| public boolean isPasswordRemoved() { | ||
| return passwordRemoved; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| @Override | ||
| public void setUsername(String username) { | ||
| setSingleAttribute(UserModel.USERNAME, username); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the
UPDATE_PASSWORDparity 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.