[OID4VCI] Enforce DID uniqueness and restrict DID updates to admins#50112
Open
Awambeng wants to merge 3 commits into
Open
[OID4VCI] Enforce DID uniqueness and restrict DID updates to admins#50112Awambeng wants to merge 3 commits into
Awambeng wants to merge 3 commits into
Conversation
1 task
Contributor
There was a problem hiding this comment.
Pull request overview
This PR strengthens management of the did user-profile attribute for OID4VCI by adding server-side uniqueness validation and tightening edit permissions, while keeping the attribute readable where appropriate.
Changes:
- Added a new
DuplicateDidValidatorand registered it for automatic discovery. - Updated default user-profile metadata for the DID attribute (pattern validation + uniqueness + write restrictions).
- Updated OID4VC tests/config to reflect the new DID behavior and added a duplicate-DID rejection test.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/base/src/test/java/org/keycloak/tests/oid4vc/OID4VCUserDidAttributeTest.java | Adds an integration test asserting duplicate DID updates are rejected with 409 CONFLICT. |
| tests/base/src/test/java/org/keycloak/tests/oid4vc/OID4VCIssuerEndpointTest.java | Adjusts test user-profile configuration for DID permissions/validation. |
| services/src/main/resources/META-INF/services/org.keycloak.validate.ValidatorFactory | Registers DuplicateDidValidator for validator discovery. |
| services/src/main/java/org/keycloak/userprofile/validator/DuplicateDidValidator.java | Introduces DID uniqueness validator using attribute-based user search. |
| services/src/main/java/org/keycloak/userprofile/DeclarativeUserProfileProviderFactory.java | Updates built-in DID attribute metadata (pattern validation, uniqueness validator, and write restrictions). |
- Add DuplicateDidValidator to prevent duplicate DID values across users, restrict DID editing to admin-only in the account console, and fix pattern validation to use ignore.empty.value for optional DID attribute. Closes keycloak#46524 Signed-off-by: Awambeng Rodrick <awambengrodrick@gmail.com>
Signed-off-by: Awambeng Rodrick <awambengrodrick@gmail.com>
Signed-off-by: Awambeng Rodrick <awambengrodrick@gmail.com>
Comment on lines
+47
to
+65
| KeycloakSession session = context.getSession(); | ||
| RealmModel realm = session.getContext().getRealm(); | ||
| UserModel user = UserProfileAttributeValidationContext.from(context).getAttributeContext().getUser(); | ||
|
|
||
| // Skip validation if the DID value hasn't changed for an existing user | ||
| if (user != null && Objects.equals(user.getFirstAttribute(UserModel.DID), value)) { | ||
| return context; | ||
| } | ||
|
|
||
| // Search for existing users with the same DID attribute value | ||
| session.users().searchForUserByUserAttributeStream(realm, UserModel.DID, value) | ||
| .filter(existing -> user == null || !Objects.equals(existing.getId(), user.getId())) | ||
| .findFirst() | ||
| .ifPresent(existing -> { | ||
| context.addError(new ValidationError(ID, inputHint, Messages.DID_EXISTS) | ||
| .setStatusCode(Response.Status.CONFLICT)); | ||
| }); | ||
|
|
||
| return context; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR hardens the DID attribute management on user profiles to ensure DID uniqueness across users and prevent unauthorized modifications.
Key Changes
DuplicateDidValidatorto enforce DID uniqueness across all users within a realm.ignore.empty.value=true, allowing optional DID values while still enforcing valid DID format when provided.DuplicateDidValidatorinMETA-INF/services/org.keycloak.validate.ValidatorFactoryfor automatic validator discovery.Closes #46524