8000 Make kcreg default to the OIDC registration endpoint by edewit · Pull Request #55582 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@ Applications that loaded their `keycloak.json` anonymously from the removed endp

The `default` client registration provider remains deprecated and is not affected by this change.

=== `kcreg` uses the OpenID Connect registration endpoint by default
// https://github.com/keycloak/keycloak/issues/55462

The Client Registration CLI (`kcreg`) now uses the OpenID Connect dynamic client registration endpoint when `-e` / `--endpoint` is omitted.
The `-e` option accepts only `oidc` (or `openid-connect`).
Values such as `default` and `saml2` are rejected.
Use OIDC JSON field names (for example `client_id`, `redirect_uris`) with `kcreg`.
For Keycloak JSON client representations, use the Admin REST API or `kcadm`.

=== SAML Entity Descriptor client registration endpoint removed
// https://github.com/keycloak/keycloak/issues/53478

Expand All @@ -413,7 +422,7 @@ Requests to `/realms/<realm>/clients-registrations/saml2-entity-descriptor` now
The following related APIs have also been removed:

* The `ClientRegistration.saml()` method and the `ClientRegistration.SAMLClientRegistration` class in the Java client registration API.
* The `saml2` endpoint type of the `kcreg` command-line tool. `kcreg create -e saml2` is no longer accepted, and `kcreg create` no longer treats an XML input document as a SAML entity descriptor. Use JSON input, or the `default` and `oidc` endpoint types.
* The `saml2` endpoint type of the `kcreg` command-line tool. `kcreg create -e saml2` is no longer accepted, and `kcreg create` no longer treats an XML input document as a SAML entity descriptor. Use OIDC JSON input with `kcreg`, or the Admin REST API / `kcadm` for other formats.

To create a client from a SAML entity descriptor, convert the descriptor with the Admin REST API (`POST /admin/realms/<realm>/client-description-converter`), then create the returned client representation (`POST /admin/realms/<realm>/clients`).
The conversion endpoint itself is not affected by this change.
Expand Down
31 changes: 16 additions & 15 deletions docs/guides/securing-apps/client-registration-cli.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ You can configure application clients from a command line with the Client Regist

To allow a particular user to use `Client Registration CLI`, the {project_name} administrator typically uses the Admin Console to configure a new user with proper roles or to configure a new client and client secret to grant access to the Client Registration REST API.

[WARNING]
[NOTE]
====
The `kcreg` CLI tool primarily uses the `default` client registration provider, which is deprecated since {project_name} 26.8 and will be removed in a future major release.
For new integrations, register and manage clients using the Admin REST API or the standards-compliant OpenID Connect dynamic client registration endpoint (`/realms/<realm>/clients-registrations/openid-connect`).
The `kcreg` CLI tool uses the OpenID Connect dynamic client registration endpoint (`/realms/<realm>/clients-registrations/openid-connect`).
The optional `-e` / `--endpoint` flag accepts only `oidc`.
For Keycloak JSON client representations or SAML metadata, use the Admin REST API or `kcadm` instead.
====

[[_configuring_a_user_for_client_registration_cli]]
Expand Down Expand Up @@ -113,15 +114,15 @@ For example, on:
[options="npwrap",subs="attributes+"]
----
$ kcreg.sh config credentials --server http://localhost:8080 --realm demo --user user --client reg-cli
$ kcreg.sh create -s clientId=my_client -s 'redirectUris=["http://localhost:8980/myapp/*"]'
$ kcreg.sh create -s client_id=my_client -s 'redirect_uris=["http://localhost:8980/myapp/*"]'
$ kcreg.sh get my_client
----
* Windows:
+
[options="npwrap",subs="attributes+"]
----
c:\> kcreg config credentials --server http://localhost:8080 --realm demo --user user --client reg-cli
c:\> kcreg create -s clientId=my_client -s "redirectUris=[\"http://localhost:8980/myapp/*\"]"
c:\> kcreg create -s client_id=my_client -s "redirect_uris=[\"http://localhost:8980/myapp/*\"]"
c:\> kcreg get my_client
----
+
Expand Down Expand Up @@ -205,28 +206,28 @@ For example, on:
[options="nowrap"]
----
$ kcreg.sh config initial-token $TOKEN
$ kcreg.sh create -s clientId=myclient
$ kcreg.sh create -s client_id=myclient
----

or

[options="nowrap"]
----
$ kcreg.sh create -s clientId=myclient -t $TOKEN
$ kcreg.sh create -s client_id=myclient -t $TOKEN
----

* Windows:
[options="nowrap"]
----
c:\> kcreg config initial-token %TOKEN%
c:\> kcreg create -s clientId=myclient
c:\> kcreg create -s client_id=myclient
----

or

[options="nowrap"]
----
c:\> kcreg create -s clientId=myclient -t %TOKEN%
c:\> kcreg create -s client_id=myclient -t %TOKEN%
----

When using an Initial Access Token, the server response includes a newly issued Registration Access Token. Any subsequent operation for that client needs to be performed by authenticating with that token, which is only valid for that client.
Expand All @@ -248,12 +249,12 @@ The following example shows how to read a JSON file, override any client id it m
* Linux:
[options="nowrap"]
----
$ kcreg.sh create -f client-template.json -s clientId=myclient -s baseUrl=/myclient -s 'redirectUris=["/myclient/*"]' -o
$ kcreg.sh create -f client-template.json -s client_id=myclient -s client_uri=/myclient -s 'redirect_uris=["/myclient/*"]' -o
----
* Windows:
[options="nowrap"]
----
C:\> kcreg create -f client-template.json -s clientId=myclient -s baseUrl=/myclient -s "redirectUris=[\"/myclient/*\"]" -o
C:\> kcreg create -f client-template.json -s client_id=myclient -s client_uri=/myclient -s "redirect_uris=[\"/myclient/*\"]" -o
----

Run the [command]`kcreg create --help` for more information about the [command]`kcreg create` command.
Expand Down Expand Up @@ -312,12 +313,12 @@ For example, on:
* Linux:
[options="nowrap"]
----
$ kcreg.sh update myclient -s enabled=false -d redirectUris
$ kcreg.sh update myclient -s client_name=disabled-client -d redirect_uris
----
* Windows:
[options="nowrap"]
----
C:\> kcreg update myclient -s enabled=false -d redirectUris
C:\> kcreg update myclient -s client_name=disabled-client -d redirect_uris
----

You can also use a file that contains only changes to be applied so you do not have to specify too many values as arguments. In this case, specify [command]`--merge` to tell the Client Registration CLI that rather than treating the JSON file as a full, new configuration, it should treat it as a set of attributes to be applied over the existing configuration.
Expand All @@ -327,12 +328,12 @@ For example, on:
* Linux:
[options="nowrap"]
----
$ kcreg.sh update myclient --merge -d redirectUris -f mychanges.json
$ kcreg.sh update myclient --merge -d redirect_uris -f mychanges.json
----
* Windows:
[options="nowrap"]
----
C:\> kcreg update myclient --merge -d redirectUris -f mychanges.json
C:\> kcreg update myclient --merge -d redirect_uris -f mychanges.json
----

Run the [command]`kcreg update --help` command for more information about the [command]`kcreg update` command.
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.keycloak.client.cli.common.AttributeOperation;
import org.keycloak.client.cli.util.AttributeException;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.oidc.OIDCClientRepresentation;
import org.keycloak.util.JsonSerialization;

Expand All @@ -38,7 +37,6 @@
public class CmdStdinContext {

private EndpointType regType;
private ClientRepresentation client;
private OIDCClientRepresentation oidcClient;
private String content;
public static final String CLIENT_OPTION_WARN = "You're using what looks like an OPTION as CLIENT: %s";
Expand All @@ -54,14 +52,6 @@ public void setEndpointType(EndpointType regType) {
this.regType = regType;
}

public ClientRepresentation getClient() {
return client;
}

public void setClient(ClientRepresentation client) {
this.client = client;
}

public OIDCClientRepresentation getOidcClient() {
return oidcClient;
}
Expand All @@ -79,109 +69,75 @@ public void setContent(String content) {
}

public String getRegistrationAccessToken() {
if (client != null) {
return client.getRegistrationAccessToken();
} else if (oidcClient != null) {
if (oidcClient != null) {
return oidcClient.getRegistrationAccessToken();
}
return null;
}

public static CmdStdinContext parseFileOrStdin(String file, EndpointType type) {

String content = readFileOrStdin(file).trim();
ClientRepresentation client = null;
OIDCClientRepresentation oidcClient = null;


if (content.length() == 0) {
throw new RuntimeException("Document provided by --file option is empty");
}

if (!content.startsWith("{")) {
throw new RuntimeException(
"Expected OIDC JSON input. Keycloak JSON (default) and SAML metadata are no longer supported by kcreg; "
+ "use OIDC field names or the Admin REST API / kcadm instead.");
}

if (type == null) {
// guess the correct endpoint from content of the file
if (content.startsWith("{")) {
// looks like JSON?
// try parse as ClientRepresentation
try {
client = JsonSerialization.readValue(content, ClientRepresentation.class);
type = EndpointType.DEFAULT;

} catch (JsonParseException e) {
throw new RuntimeException("Failed to read the input document as JSON: " + e.getMessage(), e);
} catch (Exception ignored) {
// deliberately not logged
}

if (client == null) {
// try parse as OIDCClientRepresentation
try {
oidcClient = JsonSerialization.readValue(content, OIDCClientRepresentation.class);
type = EndpointType.OIDC;
} catch (IOException ne) {
throw new RuntimeException("Unable to determine input document type. Use -e TYPE to specify the registration endpoint to use");
} catch (Exception e) {
throw new RuntimeException("Failed to read the input document as JSON", e);
}
}

} else if (content.length() == 0) {
throw new RuntimeException("Document provided by --file option is empty");
} else {
throw new RuntimeException("Unable to determine input document type. Use -e TYPE to specify the registration endpoint to use");
}
type = EndpointType.OIDC;
} else if (type != EndpointType.OIDC) {
throw new RuntimeException("Only the 'oidc' endpoint type is supported");
}

// check content type, making sure it can be parsed into .json
if (content != null) {
try {
if (type == EndpointType.DEFAULT && client == null) {
client = JsonSerialization.readValue(content, ClientRepresentation.class);
} else if (type == EndpointType.OIDC && oidcClient == null) {
oidcClient = JsonSerialization.readValue(content, OIDCClientRepresentation.class);
}
} catch (JsonParseException e) {
throw new RuntimeException("Not a valid JSON document - " + e.getMessage(), e);
} catch (UnrecognizedPropertyException e) {
throw new RuntimeException("Attribute '" + e.getPropertyName() + "' not supported on document type '" + type.getName() + "'", e);
} catch (IOException e) {
throw new RuntimeException("Not a valid JSON document", e);
}

try {
oidcClient = JsonSerialization.readValue(content, OIDCClientRepresentation.class);
} catch (JsonParseException e) {
throw new RuntimeException("Failed to read the input document as JSON: " + e.getMessage(), e);
} catch (UnrecognizedPropertyException e) {
throw new RuntimeException("Attribute '" + e.getPropertyName()
+ "' not supported on document type 'oidc'. Keycloak JSON (default) format is no longer supported by kcreg; "
+ "use OIDC field names or the Admin REST API / kcadm.", e);
} catch (IOException e) {
throw new RuntimeException("Failed to read the input document as JSON", e);
}

CmdStdinContext ctx = new CmdStdinContext();
ctx.setEndpointType(type);
ctx.setContent(content);
ctx.setClient(client);
ctx.setOidcClient(oidcClient);
return ctx;
}

public static CmdStdinContext mergeAttributes(CmdStdinContext ctx, List<AttributeOperation> attrs) {
String content = ctx.getContent();
ClientRepresentation client = ctx.getClient();
OIDCClientRepresentation oidcClient = ctx.getOidcClient();
EndpointType type = ctx.getEndpointType();
try {
if (content == null) {
if (type == EndpointType.DEFAULT) {
client = new ClientRepresentation();
} else if (type == EndpointType.OIDC) {
oidcClient = new OIDCClientRepresentation();
}
oidcClient = new OIDCClientRepresentation();
}
Object rep = client != null ? client : oidcClient;
if (rep != null) {
if (oidcClient != null) {
try {
setAttributes(rep, attrs);
setAttributes(oidcClient, attrs);
} catch (AttributeException e) {
throw new RuntimeException("Failed to set attribute '" + e.getAttributeName() + "' on document type '" + type.getName() + " 10BC8 ;'", e);
}
content = JsonSerialization.writeValueAsString(rep);
content = JsonSerialization.writeValueAsString(oidcClient);
} else {
throw new RuntimeException("Setting attributes is not supported for type: " + type.getName());
}
} catch (IOException e) {
throw new RuntimeException("Failed to merge set attributes with configuration from file", e);
}

ctx.setContent(content);
ctx.setClient(client);
ctx.setOidcClient(oidcClient);
return ctx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,34 @@
* @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
*/
public enum EndpointType {
DEFAULT("default", "default"),
OIDC("openid-connect", "oidc", "oidc");

private String endpoint;
private String preferredName;
private Set<String> alternativeNames;
private static final Set<String> REMOVED = Set.of(
"default",
"saml2",
"saml2-entity-descriptor",
"install",
"adapter");

private final String endpoint;
private final String preferredName;
private final Set<String> alternativeNames;

private EndpointType(String endpoint, String preferredName, String ... alternativeNames) {
this.endpoint = endpoint;
this.preferredName = preferredName;
this.alternativeNames = new HashSet(Arrays.asList(alternativeNames));
this.alternativeNames = new HashSet<>(Arrays.asList(alternativeNames));
}

public static EndpointType of(String name) {
if (DEFAULT.endpoint.equals(name) || DEFAULT.alternativeNames.contains(name)) {
return DEFAULT;
} else if (OIDC.endpoint.equals(name) || OIDC.alternativeNames.contains(name)) {
if (OIDC.endpoint.equals(name) || OIDC.alternativeNames.contains(name)) {
return OIDC;
}
if (REMOVED.contains(name)) {
throw new IllegalArgumentException(
"Endpoint '" + name + "' is no longer supported by kcreg. Use 'oidc' (OpenID Connect dynamic client registration). "
+ "For Keycloak JSON or SAML metadata, use the Admin REST API or kcadm instead.");
}
throw new IllegalArgumentException("Endpoint not supported: " + name);
}

Expand All @@ -58,12 +67,9 @@ public String getName() {
}

public static String getExpectedContentType(EndpointType type) {
switch (type) {
case DEFAULT:
case OIDC:
return HttpUtil.APPLICATION_JSON;
default:
throw new RuntimeException("Unsupported endpoint type: " + type);
if (type == OIDC) {
return HttpUtil.APPLICATION_JSON;
}
throw new RuntimeException("Unsupported endpoint type: " + type);
}
}
Loading
Loading
0