Using the client registration service

Use the client registration service.

In order for an application or service to utilize Keycloak it has to register a client in Keycloak. An admin can do this through the admin console (or admin REST endpoints), but clients can also register themselves through the Keycloak client registration service.

The Client Registration Service provides built-in support for Keycloak Client Representations, OpenID Connect Client Meta Data and SAML Entity Descriptors. The Client Registration Service endpoint is /realms/<realm>/clients-registrations/<provider>.

The built-in supported providers are:

  • default - Keycloak Client Representation (JSON)

  • install - Keycloak Adapter Configuration (JSON)

  • openid-connect - OpenID Connect Client Metadata Description (JSON)

  • saml2-entity-descriptor - SAML Entity Descriptor (XML)

The following sections will describe how to use the different providers.

Authentication

To invoke the Client Registration Services you usually need a token. The token can be a bearer token, an initial access token or a registration access token. There is an alternative to register new client without any token as well, but then you need to configure Client Registration Policies (see below).

Bearer token

The bearer token can be issued on behalf of a user or a Service Account. The following permissions are required to invoke the endpoints (see Server Administration Guide for more details):

  • create-client or manage-client - To create clients

  • view-client or manage-client - To view clients

  • manage-client - To update or delete client

If you are using a bearer token to create clients it’s recommend to use a token from a Service Account with only the create-client role (see Server Administration Guide for more details).

Initial Access Token

The recommended approach to registering new clients is by using initial access tokens. An initial access token can only be used to create clients and has a configurable expiration as well as a configurable limit on how many clients can be created.

An initial access token can be created through the admin console. To create a new initial access token first select the realm in the admin console, then click on Client in the menu on the left, followed by Initial access token in the tabs displayed in the page.

You will now be able to see any existing initial access tokens. If you have access you can delete tokens that are no longer required. You can only retrieve the value of the token when you are creating it. To create a new token click on Create. You can now optionally add how long the token should be valid, also how many clients can be created using the token. After you click on Save the token value is displayed.

It is important that you copy/paste this token now as you won’t be able to retrieve it later. If you forget to copy/paste it, then delete the token and create another one.

The token value is used as a standard bearer token when invoking the Client Registration Services, by adding it to the Authorization header in the request. For example:

Authorization: bearer eyJhbGciOiJSUz...

Registration Access Token

When you create a client through the Client Registration Service the response will include a registration access token. The registration access token provides access to retrieve the client configuration later, but also to update or delete the client. The registration access token is included with the request in the same way as a bearer token or initial access token.

By default, registration access token rotation is enabled. This means a registration access token is only valid once. When the token is used, the response will include a new token. Note that registration access token rotation can be disabled by using Client Policies.

If a client was created outside of the Client Registration Service it won’t have a registration access token associated with it. You can create one through the admin console. This can also be useful if you lose the token for a particular client. To create a new token find the client in the admin console and click on Credentials. Then click on Generate registration access token.

Cross-Origin Resource Sharing (CORS)

In case when clients are registered through the user-agent it is required to configure what web origins are permitted for client registration.

For bearer tokens the permitted web origin can be configured in the client the token was issued to. When creating an initial access token, the permitted web origins can be supplied directly.

For anonymous access, or as a realm wide configuration, permitted web origins can be configured by going to clients, then client registration and updating the Allowed Registration Web Origins policy to list web origins that should be allowed.

Keycloak Representations

The default client registration provider is deprecated since Keycloak 26.8 and will be removed in a future major release. Register and manage clients via the Admin REST API or the OpenID Connect dynamic client registration endpoint (/realms/<realm>/clients-registrations/openid-connect) instead.

The default client registration provider can be used to create, retrieve, update and delete a client. It uses Keycloak Client Representation format which provides support for configuring clients exactly as they can be configured through the admin console, including for example configuring protocol mappers.

To create a client create a Client Representation (JSON) then perform an HTTP POST request to /realms/<realm>/clients-registrations/default.

It will return a Client Representation that also includes the registration access token. You should save the registration access token somewhere if you want to retrieve the config, update or delete the client later.

To retrieve the Client Representation perform an HTTP GET request to /realms/<realm>/clients-registrations/default/<client id>.

The GET request will not update the registration access token, so it will remain the same.

To update the Client Representation perform an HTTP PUT request with the updated Client Representation to: /realms/<realm>/clients-registrations/default/<client id>.

It will also return a new registration access token.

To delete the Client Representation perform an HTTP DELETE request to: /realms/<realm>/clients-registrations/default/<client id>

Keycloak adapter configuration

The install client registration provider is deprecated since Keycloak 26.8 and will be removed in a future major release. Retrieve adapter configuration via the Admin REST API (GET /admin/realms/<realm>/clients/<client-uuid>/installation/providers/<provider-id>) instead.

The installation client registration provider can be used to retrieve the adapter configuration for a client. In addition to token authentication you can also authenticate with client credentials using HTTP basic authentication. To do this include the following header in the request:

Authorization: basic BASE64(client-id + ':' + client-secret)

To retrieve the Adapter Configuration then perform an HTTP GET request to /realms/<realm>/clients-registrations/install/<client id>.

No authentication is required for public clients. This means that for the JavaScript adapter you can load the client configuration directly from Keycloak using the above URL.

OpenID Connect Dynamic Client Registration

The endpoint to use these specifications to register clients in Keycloak is /realms/<realm>/clients-registrations/openid-connect[/<client id>].

This endpoint can also be found in the OpenID Connect Discovery endpoint for the realm, /realms/<realm>/.well-known/openid-configuration.

Keycloak supports most of the options mentioned in the OpenID Connect Dynamic Client Registration specification. However not everything is properly supported. For instance OpenID Connect client option application_type is not supported right now and it will be ignored by Keycloak if you use it when registering your client.

For the indication of what OpenID Connect client options are supported, you may try to see client documentation section in the Server Administration Guide or inspect the available options in the admin console when configuring the client. OIDC client options, which have counterparts in the Keycloak admin console are usually supported and can be understood by Keycloak. Note that in some cases, the semantics is a bit different. For example, OpenID Connect option response_types is mapped in the Keycloak client configuration to different individual On/Off switches like Standard flow, Implicit flow and others.

SAML Entity Descriptors

The SAML Entity Descriptor endpoint is deprecated since Keycloak 26.8 and will be removed in a future major release. Use the Admin REST API to convert the entity descriptor (POST /admin/realms/<realm>/client-description-converter) and then create the returned client representation (POST /admin/realms/<realm>/clients).

The SAML Entity Descriptor endpoint only supports using SAML v2 Entity Descriptors to create clients. It doesn’t support retrieving, updating or deleting clients. For those operations, use the Admin REST API (/admin/realms/<realm>/clients/<client-uuid>). When creating a client a Keycloak Client Representation is returned with details about the created client, including a registration access token.

To create a client perform an HTTP POST request with the SAML Entity Descriptor to /realms/<realm>/clients-registrations/saml2-entity-descriptor.

Example using CURL

The following example creates a client with the clientId myclient using CURL. You need to replace eyJhbGciOiJSUz…​ with a proper initial access token or bearer token.

curl -X POST \
    -d '{ "clientId": "myclient" }' \
    -H "Content-Type:application/json" \
    -H "Authorization: bearer eyJhbGciOiJSUz..." \
    http://localhost:8080/realms/master/clients-registrations/default

Example using Java Client Registration API

The Client Registration Java API makes it easy to use the Client Registration Service using Java. To use include the dependency org.keycloak:keycloak-client-registration-api:>VERSION< from Maven.

For full instructions on using the Client Registration refer to the JavaDocs. Below is an example of creating a client. You need to replace eyJhbGciOiJSUz…​ with a proper initial access token or bearer token.

The ClientRegistration methods reg.create(), reg.get(), reg.update(), and reg.delete() target default; reg.getAdapterConfig() targets install; and reg.saml().create() targets saml2-entity-descriptor. These providers are deprecated since Keycloak 26.8 and will be removed in a future major release. New integrations should use the replacement Admin REST API endpoints described above, or the OpenID Connect dynamic client registration endpoint for OIDC client registration.

String token = "eyJhbGciOiJSUz...";

ClientRepresentation client = new ClientRepresentation();
client.setClientId(CLIENT_ID);

ClientRegistration reg = ClientRegistration.create()
    .url("http://localhost:8080", "myrealm")
    .build();

reg.auth(Auth.token(token));

client = reg.create(client);

String registrationAccessToken = client.getRegistrationAccessToken();

Client Registration Policies

Client Registration Policies apply to requests made through the Client Registration Service. They are not executed for arbitrary Admin REST API requests.
The current plans are for the Client Registration Policies to be removed in favor of the Client Policies described in the Server Administration Guide. Client Policies are more flexible and support more use case. For example the case of enforcing client URIs like the JWKS to prevent SSRF attacks allowing only trusted patterns or domains.

Keycloak currently supports two ways how new clients can be registered through Client Registration Service.

  • Authenticated requests - Request to register new client must contain either Initial Access Token or Bearer Token as mentioned above.

  • Anonymous requests - Request to register new client doesn’t need to contain any token at all

Anonymous client registration requests are very interesting and powerful feature, however you usually don’t want that anyone is able to register new client without any limitations. Hence we have Client Registration Policy SPI, which provide a way to limit who can register new clients and under which conditions.

In Keycloak admin console, you can click to Client Registration tab and then Client Registration Policies sub-tab. Here you will see what policies are configured by default for anonymous requests and what policies are configured for authenticated requests.

The anonymous requests (requests without any token) are allowed just for creating (registration) of new clients. So when you register new client through anonymous request, the response will contain Registration Access Token, which must be used for Read, Update or Delete request of particular client. However using this Registration Access Token from anonymous registration will be then subject to Anonymous Policy too! This means that for example request for update client also needs to come from Trusted Host if you have Trusted Hosts policy. Also for example it won’t be allowed to disable Consent Required when updating client and when Consent Required policy is present etc.

Currently we have these policy implementations:

  • Trusted Hosts Policy - Configures trusted hosts and domains for registration requests and client URLs. When Client URIs Must Match is enabled, all supplied client URI fields must use a trusted host or domain on both registration and update. This includes root, home and admin URLs, redirect and post-logout redirect URIs, logout endpoints, web origins, JWKS and request URIs, the CIBA notification endpoint, logo, policy, terms of service and sector identifier attributes, and SAML endpoints and metadata URLs. The policy checks supplied fields regardless of the client’s protocol. Add external hosts, such as a logo CDN, to the policy when needed. Web origin markers `, `*` and `null`, post-logout markers ` and -, and embedded data: logos remain accepted. Redirect URIs referenced by + are checked separately; explicit URLs supplied alongside the marker are also checked. Web origins without a scheme and scheme-relative browser URLs, including front-channel logout and its home URL fallback, are checked against their own host. Relative logo URLs are checked against the Keycloak frontend host instead of the client’s root URL, and their configured values are preserved. Redirect paths retain the runtime resolution against the client’s root URL, including paths starting with //. By default, the trusted-host list is empty, so anonymous registration is disabled.

Keycloak normalizes loopback addresses to localhost to avoid platform-specific reverse-DNS differences (for example on Windows a reverse lookup of loopback may return the numeric IP rather than localhost). All other trusted domains must be based upon the primary reverse-DNS lookup returned by the platform.
  • Consent Required Policy - Newly registered clients will have Consent Allowed switch enabled. So after successful authentication, user will always see consent screen when he needs to approve permissions (client scopes). It means that client won’t have access to any personal info or permission of user unless user approves it.

  • Protocol Mappers Policy - Allows to configure a list of allowed protocol mapper implementations. A new client can’t be registered or updated if it contains some non-allowed protocol mapper. Note that this policy is used for authenticated requests as well, so even for authenticated requests there are some limitations on which protocol mappers can be used.

    While the Allowed Protocol Mapper Types list in the policy configuration contains a default set of built-in mapper types, they are not sufficient by default. For a protocol mapper type to be permitted during Dynamic Client Registration, it must be present in the intersection of the policy’s Allowed Protocol Mapper Types list and the realm-level Allowed protocol mapper types for client administrators setting (configured under Realm settings). Since the realm-level setting is empty by default, new Dynamic Client Registration clients cannot include protocol mappers, and updates cannot add, modify, or delete protocol mappers, until a realm administrator explicitly adds the allowed mapper types to BOTH the policy configuration and the realm-level setting. Existing mappers of a disallowed type may still be included unchanged in an update. This is intentional: allowing protocol mapper creation via DCR effectively grants the registering party the ability to shape token claims, which carries significant security risk. Only add mapper types that are absolutely required and treat any addition as a security-sensitive configuration decision.
  • Client Scope Policy - Allows you to specify Client Scopes that can be used with newly registered or updated clients. There are no allowed scopes by default; only the client scopes defined as Realm Default Client Scopes are allowed by default. The policy also checks scopes requested through the deprecated clientTemplate field, after converting spaces in legacy names to underscores.

    Realm administrators should take care before adding Allowed Client Scopes to this client registration policy. Client scopes can contain role scope mappings and various protocol mappers, which can modify issued tokens or assertions. When a client is assigned an allowed client scope, all of those role scope mappings and protocol mappers will be applied.
  • Full Scope Policy - Newly registered clients will have Full Scope Allowed switch disabled. This means they won’t have any scoped realm roles or client roles of other clients. This policy is present by default for both anonymous and authenticated client registration.

  • Default Roles Policy - Rejects create or update requests that would change the realm-wide default-role composite through the deprecated defaultRoles field. This policy is present by default for both anonymous and authenticated client registration. Realm administrators who still need this behaviour through Dynamic Client Registration can remove the policy.

  • Max Clients Policy - Rejects registration if current number of clients in the realm is same or bigger than specified limit. It’s 200 by default for anonymous registrations.

  • Client Disabled Policy - Newly registered client will be disabled. This means that admin needs to manually approve and enable all newly registered clients. This policy is not used by default even for anonymous registration.

On this page