8000 Add client type support to Admin API v2 by arnabnandy7 · Pull Request #50895 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Add client type support to Admin API v2 - #50895

Closed
arnabnandy7 wants to merge 15 commits into
keycloak:mainfrom
arnabnandy7:feature/v2-client-types
Closed

arnabnandy7 wants to merge 15 commits into
keycloak:mainfrom
arnabnandy7:feature/v2-client-types

Conversation

@arnabnandy7
Copy link
Copy Markdown
Contributor

Closes #50894

This adds client-type support when creating and updating clients through Admin API v2.

The v2 client creation flow now:

  • exposes the client type in the v2 representation and generated OpenAPI schema
  • augments newly created clients using the configured client type
  • preserves omitted values controlled by the client type
  • distinguishes omitted creation fields from explicit null or empty updates
  • delegates explicit updates to client-type validation
  • handles typed service-account clients consistently
  • exposes the requested type to client policy contexts

Regression coverage includes creating a minimal service-account client and clearing mutable typed-client properties through a merge patch.

The protocol-default changes remain in #50801 and are intentionally excluded from this PR.

Apply configured client types during v2 client creation while preserving omitted type-controlled values and explicit update semantics.

Closes keycloak#50894

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI balanced review requested due to automatic review settings July 14, 2026 18:08
@arnabnandy7
arnabnandy7 requested review from a team as code owners July 14, 2026 18:08
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds client-type support to Admin API v2 client creation and updates.

Changes:

  • Exposes client type through representations and OpenAPI.
  • Applies client-type defaults while preserving omitted fields.
  • Adds typed-client creation and merge-patch tests.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
InteropTest.java Adds typed-client regression tests.
DefaultClientService.java Integrates client-type handling.
OIDCClientModelMapper.java Refactors authentication mapping.
ClientModelMapper.java Adds field-exclusion mapping.
BaseClientModelMapper.java Implements excluded-field support.
SAMLClientRepresentation.java Tracks explicit logout updates.
OIDCClientRepresentation.java Tracks explicit OIDC fields.
BaseRepresentation.java Records explicitly set fields.
BaseClientRepresentation.java Adds the client type property.
openapi.json Documents the client type schema.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings July 14, 2026 18:31
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The rea 8000 son will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings July 14, 2026 19:10
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
@arnabnandy7
arnabnandy7 force-pushed the feature/v2-client-types branch from 51c824e to b3f3ea1 Compare July 14, 2026 19:11
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 14, 2026 19:20
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings July 14, 2026 20:04
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings July 14, 2026 20:20
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings July 14, 2026 20:54
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated no new comments.

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings July 21, 2026 21:19
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/InteropTest.java:398

  • This regression only patches fields that are mutable in the oidc type, so it does not exercise the new client-type rejection path or parameterized error response. Add a typed service-account patch that explicitly removes SERVICE_ACCOUNT (or changes another fixed field) and assert the 400 response includes the controlled option in params.
            getClientsApi().client(client.getClientId()).patchClient(
                    new ByteArrayInputStream(mapper.writeValueAsBytes(patch)));

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.MultipleTabsLoginTest#multipleTabsParallelLoginTestWithAuthSessionExpiredAndRequiredAction

Keycloak CI - Forms IT (chrome)

org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

Copilot AI review requested due to automatic review settings August 14, 2026 20:22
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated no new comments.

Suppressed comments (1)

rest/admin-v2/services/src/main/java/org/keycloak/services/client/DefaultClientService.java:106

  • This preservation list omits the other SAML fields that client types can control. For example, an omitted signDocuments is mapped to removeAttribute("saml.server.signature"); if a custom SAML type fixes that option, the type-aware delegate rejects the null update, so even a minimal typed SAML creation fails. Add the SAML representation-to-option mappings and explicit-field tracking so omitted controlled SAML values are excluded while explicit updates still reach validation.
    private static final List<TypeControlledField> TYPE_CONTROLLED_FIELDS = List.of(
            new TypeControlledField("redirectUris", "redirectUris", Set.class),
            new TypeControlledField("webOrigins", "webOrigins", Set.class),
            new TypeControlledField("auth", "publicClient", Boolean.class),
            FRONT_CHANNEL_LOGOUT_FIELD);

Copilot AI review requested due to automatic review settings August 14, 2026 21:06
@arnabnandy7
arnabnandy7 force-pushed the feature/v2-client-types branch from 9e7f63a to ddcbb7c Compare August 14, 2026 21:06
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated no new comments.

Suppressed comments (4)

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/BaseClientRepresentation.java:174

  • setRoles now defensively copies, but it doesn’t call markFieldAsExplicitlySet("roles"). With the newly introduced explicit-field tracking, roles updates via JSON patch/merge-patch (or any caller using this setter) won’t be recognized as explicitly provided, which can lead to inconsistent update behavior compared to redirectUris, displayName, etc. Add markFieldAsExplicitlySet("roles") in this setter for consistent semantics.
    public Set<String> getRoles() {
        return roles;
    }

    public void setRoles(Set<String> roles) {
        this.roles = roles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(roles);
    }

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/OIDCClientRepresentation.java:100

  • setServiceAccountRoles defensively copies but doesn’t mark the field as explicitly set. Given the new “explicitly set” mechanism and its use in service logic, this is likely an omission and can cause service-account role changes to be treated as “not provided by the client.” Call markFieldAsExplicitlySet("serviceAccountRoles") here (and keep the defensive copy).
    public void setServiceAccountRoles(Set<String> serviceAccountRoles) {
        this.serviceAccountRoles = serviceAccountRoles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(serviceAccountRoles);
    }

rest/admin-v2/services/src/main/java/org/keycloak/services/client/scim/BaseClientModelSchema.java:234

  • Clearing explicit-set state is done only in the fromModel(ClientModel, List<String>) overload. If other fromModel(...) overloads (e.g., those using includeFields) also populate via setters that mark fields explicitly set, they may now return representations with different isFieldExplicitlySet(...) behavior depending on the overload used. Consider centralizing rep.clearExplicitlySetFields() in a shared private helper (or applying it consistently across all fromModel overloads) to guarantee uniform model→representation behavior.
    public R fromModel(ClientModel model, List<String> attributes) {
        R rep = createRepresentation();
        populate(rep, model, attributes, null);
        rep.clearExplicitlySetFields();
        return rep;
    }

rest/admin-v2/services/src/main/java/org/keycloak/services/ServiceExceptionMapper.java:34

  • This replaces the entity returned by KeycloakErrorHandler whenever parameters are present, which can drop existing error payload fields (e.g., any structured fields the handler already set). To avoid changing the response shape unexpectedly, consider: (1) if the existing entity is already an ErrorRepresentation, enrich it by setting params; otherwise (2) preserve the original entity and only add params via a wrapper field/structure that maintains backward-compatible fields.
        Response response = KeycloakErrorHandler.getResponse(session, exception.toWebApplicationException());
        Optional<Object[]> parameters = exception.getParameters();
        if (exception.getMessage() != null && parameters.isPresent()
                && response.getMediaType() != null && MediaType.APPLICATION_JSON_TYPE.isCompatible(response.getMediaType())) {
            ErrorRepresentation error = new ErrorRepresentation();
            error.setErrorMessage(exception.getMessage());
            error.setParams(parameters.get());
            return Response.fromResponse(response).entity(error).build();
        }
        return response;

Copilot AI review requested due to automatic review settings August 14, 2026 21:09
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Cop 8 ilot rev 10BC8 iewed 13 out of 13 changed files in this pull request and generated no new comments.

Suppressed comments (4)

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/BaseClientRepresentation.java:174

  • setRoles now defensively copies the incoming set but does not call markFieldAsExplicitlySet("roles"). With explicit-field tracking introduced in this PR, isFieldExplicitlySet("roles") will remain false even when roles were provided (including via merge-patch), which can lead to incorrect “field came from patch” detection. Add markFieldAsExplicitlySet("roles") (or document why roles should be excluded from tracking if that’s intentional).
    public void setRoles(Set<String> roles) {
        this.roles = roles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(roles);
    }

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/OIDCClientRepresentation.java:100

  • setServiceAccountRoles defensively copies the incoming set but does not mark the field as explicitly set. This makes isFieldExplicitlySet("serviceAccountRoles") inaccurate when the field is provided by clients (including merge-patch). Consider adding markFieldAsExplicitlySet("serviceAccountRoles") for consistency with other setters introduced/updated in this PR.
    public void setServiceAccountRoles(Set<String> serviceAccountRoles) {
        this.serviceAccountRoles = serviceAccountRoles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(serviceAccountRoles);
    }

rest/admin-v2/services/src/main/java/org/keycloak/services/ServiceExceptionMapper.java:37

  • This replaces whatever JSON entity KeycloakErrorHandler produced with a new ErrorRepresentation containing only errorMessage and params, potentially dropping other error fields (e.g., error codes/details) that clients may rely on. A safer approach is to preserve/augment the existing entity when it is already an ErrorRepresentation (or a known error payload type), and only fall back to creating a fresh entity when there isn’t one to enrich.
        Response response = KeycloakErrorHandler.getResponse(session, exception.toWebApplicationException());
        Optional<Object[]> parameters = exception.getParameters();
        if (exception.getMessage() != null && parameters.isPresent()
                && response.getMediaType() != null && MediaType.APPLICATION_JSON_TYPE.isCompatible(response.getMediaType())) {
            ErrorRepresentation error = new ErrorRepresentation();
            error.setErrorMessage(exception.getMessage());
            error.setParams(parameters.get());
            return Response.fromResponse(response).entity(error).build();
        }
        return response;

rest/admin-v2/tests/src/test/java/org/keycloak/tests/admin/client/v2/RepresentationDefensiveCopyTest.java:24

  • Prefer assertFalse(...) / assertTrue(...) over assertEquals(false/true, ...) for clearer intent and better failure messages in JUnit 5.
        assertEquals(false, representation.isFieldExplicitlySet("type"));
        assertEquals(true, representation.isFieldExplicitlySet("description"));

Copilot AI review requested due to automatic review settings August 14, 2026 21:15
@arnabnandy7
arnabnandy7 force-pushed the feature/v2-client-types branch from ddcbb7c to 08ec6ff Compare August 14, 2026 21:15
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Suppressed comments (2)

rest/admin-v2/services/src/main/java/org/keycloak/services/client/scim/BaseClientModelSchema.java:234

  • rep.clearExplicitlySetFields() is applied in the fromModel(ClientModel, List<String>) overload, but the file also contains a fromModel(ClientModel, Set<String>) overload (line 227) where the same clearing step may be missing. Since setters now mark fields as explicitly set, returning model-derived representations with populated explicitlySetFields can cause inconsistent behavior depending on which overload is used. Consider clearing explicitlySetFields in all model->representation fromModel(...) overloads after populate(...).
    public R fromModel(ClientModel model, List<String> attributes) {
        R rep = createRepresentation();
        populate(rep, model, attributes, null);
        rep.clearExplicitlySetFields();
        return rep;
    }

rest/admin-v2/services/src/main/java/org/keycloak/services/ServiceExceptionMapper.java:65

  • parameters is an Object[] and is stored directly into the response entity/map. Since arrays are mutable, this can lead to accidental mutation after enrichment (even if ServiceException defensively copies on construction). To keep responses stable and consistent with the defensive-copy intent, clone the array (or convert to an immutable List) before setting it on ErrorRepresentation / putting it into the map.
    static Object addParameters(Object entity, String message, Object[] parameters) {
        if (entity instanceof ErrorRepresentation error) {
            if (error.getErrorMessage() == null) {
                error.setErrorMessage(message);
            }
            error.setParams(parameters);
            return error;
        }
        if (entity instanceof OAuth2ErrorRepresentation error) {
            Map<String, Object> enriched = new LinkedHashMap<>();
            enriched.put("error", error.getError());
            if (error.getErrorDescription() != null) {
                enriched.put("error_description", error.getErrorDescription());
            }
            enriched.put("errorMessage", message);
            enriched.put("params", parameters);
            return enriched;
        }

        ErrorRepresentation error = new ErrorRepresentation();
        error.setErrorMessage(message);
        error.setParams(parameters);
        return error;
    }

…ure/v2-client-types

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings August 14, 2026 21:27
@arnabnandy7
arnabnandy7 force-pushed the feature/v2-client-types branch from 08ec6ff to 887e2f1 Compare August 14, 2026 21:27
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated no new comments.

Suppressed comments (5)

rest/admin-v2/services/src/main/java/org/keycloak/services/ServiceExceptionMapper.java:60

  • This converts OAuth2ErrorRepresentation into a Map but only carries over error and error_description, dropping any other OAuth2 fields that may be present (e.g., error_uri if supported by the representation). To avoid losing data, preserve all supported OAuth2 error fields when enriching (either by copying every relevant field into the map, or by enriching without changing the entity shape).
        if (entity instanceof OAuth2ErrorRepresentation error) {
            Map<String, Object> enriched = new LinkedHashMap<>();
            enriched.put("error", error.getError());
            if (error.getErrorDescription() != null) {
                enriched.put("error_description", error.getErrorDescription());
            }
            enriched.put("errorMessage", message);
            enriched.put("params", stableParameters);
            return enriched;
        }

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/OIDCClientRepresentation.java:75

  • This setter changes the semantic meaning of an explicit JSON null (or programmatic setLoginFlows(null)) to an empty set. For merge-patch, null typically means 'remove/unset', which is distinct from 'set to empty'. Consider preserving null internally (while still defensively copying when non-null) and letting higher-level patch handling decide how null should map to model behavior.
    public void setLoginFlows(Set<Flow> loginFlows) {
        markFieldAsExplicitlySet("loginFlows");
        this.loginFlows = loginFlows == null ? new LinkedHashSet<>() : new LinkedHashSet<>(loginFlows);
    }

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/BaseClientRepresentation.java:168

  • Same as with loginFlows: converting null to an empty set makes it impossible to represent an explicit null value in input payloads and conflates 'unset' with 'empty'. If the API relies on explicit-field tracking + merge-patch semantics, it’s safer to keep null as null (and only defensively copy when non-null) so server-side logic can distinguish omitted vs explicit-null vs explicit-empty.
    public void setRedirectUris(Set<String> redirectUris) {
        markFieldAsExplicitlySet("redirectUris");
        this.redirectUris = redirectUris == null ? new LinkedHashSet<>() : new LinkedHashSet<>(redirectUris);
    }

rest/admin-v2/api/src/main/java/org/keycloak/representations/admin/v2/BaseClientRepresentation.java:177

  • Same as with loginFlows: converting null to an empty set makes it impossible to represent an explicit null value in input payloads and conflates 'unset' with 'empty'. If the API relies on explicit-field tracking + merge-patch semantics, it’s safer to keep null as null (and only defensively copy when non-null) so server-side logic can distinguish omitted vs explicit-null vs explicit-empty.
    public void setRoles(Set<String> roles) {
        markFieldAsExplicitlySet("roles");
        this.roles = roles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(roles);
    }

rest/admin-v2/services/src/main/java/org/keycloak/services/client/DefaultClientService.java:104

  • For redirectUris / webOrigins, using Set.class as the optionType risks clientType.getTypeValue(...) returning null purely due to a type mismatch (if the underlying stored value is e.g. a List<String>), which would incorrectly treat those fields as not type-controlled. Align optionType with the actual stored option value type used by ClientType (or use a more tolerant approach for collection types) to ensure the omit/override logic behaves deterministically.
    private static final List<TypeControlledField> TYPE_CONTROLLED_FIELDS = List.of(
            new TypeControlledField("redirectUris", "redirectUris", Set.class),
            new TypeControlledField("webOrigins", "webOrigins", Set.class),
            new TypeControlledField("auth", "publicClient", Boolean.class),

@shawkins
shawkins commented Sep 2, 2026
Copy link
Copy Markdown
Contributor

Thank you for the PR @arnabnandy7, but I'm closing this until we fully triage the issue.

@shawkins shawkins closed this Sep 2, 2026
@arnabnandy7
Copy link
Copy Markdown
Contributor Author

Thank you for the PR @arnabnandy7, but I'm closing this until we fully triage the issue.

@shawkins once you do please let me know or reopen with design so that I can refactor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flaky-test status/hold PR should not be merged. On hold for later. team/cloud-native

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add v2 client handling for client types feature.

5 participants

0