> ## Documentation Index
> Fetch the complete documentation index at: https://tyk.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Step by step guide using Keycloak

<Warning>
  **Legacy: Tyk Classic Portal**

  You're viewing documentation for the **Tyk Classic Portal**, which is no longer actively maintained.

  If you're looking for the latest API documentation for the **new Tyk Developer Portal**, please refer to the
  [Postman collection](/5.12/product-stack/tyk-enterprise-developer-portal/api-documentation/tyk-edp-api) or visit the
  [Tyk Developer Portal](/5.12/portal/overview/intro) section.

  The Classic Portal is in maintenance mode and will be deprecated soon. For questions or support, contact us at
  [support@tyk.io](<mailto:support@tyk.io?subject=Tyk classic developer portal>).
</Warning>

We are going walk you through a basic integration of Tyk with Keycloak using the [OpenID Connect Dynamic Client Registration protocol](https://tools.ietf.org/html/rfc7591). Our current implementation provides support for the client credentials flow with support for <Tooltip tip="JSON Web Tokens">JWT</Tooltip>. To the developer it works like this:

1. An API with its corresponding security policy is created in Tyk. It is then added to the Developer Portal Catalog.

2. A developer signs up and creates a Dynamic Client Registration provider using the Developer Portal.
   Tyk sends the Dynamic Client Registration call to your <Tooltip tip="Identity Provider">IDP</Tooltip>. The IDP replies with the client ID and secret.

3. Using the previous information, the developer (or your application) triggers a call to the token endpoint of the IDP.
   The developer (or your application) then triggers a call to Tyk, using the token that was generated by the IDP. Tyk validates this token using the <Tooltip tip="JSON Web Key Sets">JWKS</Tooltip> provided by the IDP.

### Requirements

* A [Keycloak](https://www.keycloak.org/) instance.
* A [Tyk Self Managed installation](/5.12/tyk-self-managed/install) (Gateway + Dashboard).

### Getting started with Keycloak

To get started with Dynamic Client Registration in Keycloak you'll need to generate an [initial access token](https://openid.net/specs/openid-connect-registration-1_0.html#Terminology) using the Keycloak Administration Console. After logging in, click **Realm settings**  under **Configure** and select the **Client Registration** tab:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_1.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=8d93acbbabc04d58142a9f29e9a76b0d" alt="Step 1" width="900" height="529" data-path="img/dcr/keycloak/step_1.png" />

To generate an initial access token, click **Create** and set the expiration time and maximum number of clients to be created using this token:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_2.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=0a71c394aefecfad1da58f7c4fba3a3f" alt="Step 2" width="1600" height="957" data-path="img/dcr/keycloak/step_2.png" />

Click **Save** and the token will be created. Keep it safe as you'll use this token to configure Tyk.

### Setting up Tyk

Now you're ready to set up Tyk. For compatibility reasons, check your `tyk_analytics.conf` and make sure that a proper `oauth_redirect_uri_separator` parameter is set. You may use the following value:

```json theme={null}
    "oauth_redirect_uri_separator": ";",
```

**Note:** If you're using a self-signed certificate on your Keycloak instance, you will need to set additional flags on both gateway and dashboard. For skipping DCR endpoint SSL verification, add the following flag to `tyk_analytics.conf`:

```json theme={null}
    "dcr_ssl_insecure_skip_verify": true
```

Also add the following flag to `tyk.conf`, this will instruct the gateway to skip SSL verification when the JWT middleware is in use, particularly when JWKS are retrieved from your IDP:

```json theme={null}
    "jwt_ssl_insecure_skip_verify": true
```

Remember to restart the services after applying the above changes.

Open the Tyk Dashboard and click **APIs** under **System Management**. Create a new API called "Keycloak API":

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_3.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=51985b072c9a1b3cd4e989b40b3f5406" alt="Step 3" width="1200" height="788" data-path="img/dcr/keycloak/step_3.png" />

Complete first part of the API creation form, then click **Configure API** and set the Authentication mode as in the image below:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_4.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=ff73d00ad5708b409971233fdbebd8ef" alt="Step 4" width="1600" height="845" data-path="img/dcr/keycloak/step_4.png" />

<Note>
  Where do I get the proper JWKS URI for my Keycloak environment?

  The JWKS URI is a required field in the `.well-known/openid-configuration` endpoint of your OpenID Connect Provider metadata. Please see the [OpenID spec](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse) for further information.
</Note>

For the **Identity Source** field use `"sub"` and for **Policy Field Name** use `"pol"`.

1. Click **Save**
2. Select **Policies** under **System Management**
3. Click **Create a Policy** and call it **Keycloak Policy**. Use the default values for this policy.
4. In the **Access rights** section, select your previously created **Keycloak API**. You will also need to enter an expiration setting for your keys.

After the policy is created, switch back to the API settings and make sure that the API is using your **Keycloak API** policy:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_5.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=a6b30e5b0e4678b4b5a94ce1f4c9d201" alt="Step 5" width="1600" height="970" data-path="img/dcr/keycloak/step_5.png" />

Now you're ready to add this API to the Developer Portal.

1. Click **Catalog** under **Portal Management** on the navigation menu.
2. Click **Add New API**, enter a name for it and select the newly created policy. Again, you will use **Keycloak Policy**:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_6.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=42f1927468189fd837473f34249dcf3c" alt="Step 6" width="1600" height="752" data-path="img/dcr/keycloak/step_6.png" />

1. Click **Save** then open the API added again

2. Open the **Settings** tab.

3. In **API Details** select the **Override global settings** option.

   <Note>
     Tyk lets you set global portal settings that apply to **all portal-listed APIs**, in this guide we assume you’re enabling and setting up DCR for a single API. In case you want to enable DCR for all the APIs, you should go to the **Settings** section under **Portal Management**, and in the **API Access** tab you can enter your DCR settings there.
   </Note>

4. Scroll down to the DCR section and enter the following settings:

<img src="https://mintcdn.com/tyk/LfMGUR8oN-JNy9-Z/img/dcr/keycloak/step_7.png?fit=max&auto=format&n=LfMGUR8oN-JNy9-Z&q=85&s=9bd5b8d96276a839a7091a33cdb13ac4" alt="Step 7" width="1600" height="886" data-path="img/dcr/keycloak/step_7.png" />

**Providers:** Different providers might implement the standard in slightly different ways, Tyk provides a specific driver for each one. For IDPs that aren’t on the list use the **Other** option.

**Grant Types:** The [OAuth 2.0 grant types](/5.12/api-management/authentication/oauth-2) that will be used by the client, see the [specification](https://openid.net/specs/openid-connect-registration-1_0.html#rfc.section.2) for more details.

**Token Endpoint Auth Method:** defines the way the client will authenticate against the token endpoint.

**Response Types:** OAuth 2.0 response types that will be used by the client.

**Identity Provider Host:** Base IDP URL, e.g. `https://keycloak:8443/`

**Client Registration Endpoint:** OpenID Connect client registration endpoint. This value is found in your well-known discovery document as `registration_endpoint`. The well-known location URL is typically `https://keycloak:8443/.well-known/openid-configuration`

**Initial Registration Access Token:** the token that’s used to register new clients, this was generated in the early steps of the guide.

### Testing the flow

Now that both Tyk and Keycloak are ready we can test the complete flow.

1. Click **Developers** under **Portal Management**
2. Click on **Add developer** and create a developer user.

After the developer is created, open your Developer Portal, click on the **OAuth Clients** navigation bar button and follow the wizard:

<img src="https://mintcdn.com/tyk/LfMGUR8oN-JNy9-Z/img/dcr/keycloak/step_8.png?fit=max&auto=format&n=LfMGUR8oN-JNy9-Z&q=85&s=d351fa2571de14aa348afeeac4c12009" alt="Step 8" width="1600" height="896" data-path="img/dcr/keycloak/step_8.png" />

Click **Create first OAuth Client**. You’ll see your previously created **Keycloak API**, select it and click **Save and continue**. The following screen will require you to enter a client name. It’s also possible to set redirect URLs if you also plan to use this client for other flow types. This setting can be left blank for the purposes of this guide.

<img src="https://mintcdn.com/tyk/LfMGUR8oN-JNy9-Z/img/dcr/keycloak/step_9.png?fit=max&auto=format&n=LfMGUR8oN-JNy9-Z&q=85&s=252908a9a4361ae8f856a674c9d46972" alt="Step 9" width="1600" height="1138" data-path="img/dcr/keycloak/step_9.png" />

Once you click **Create**, Tyk will trigger a registration on your IDP and the details of your client will be displayed:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_10.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=e83041683b28d39c8b14d0c9812a62d1" alt="Step 10" width="1600" height="939" data-path="img/dcr/keycloak/step_10.png" />

If you check the Keycloak dashboard you will see this client too:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_11.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=f72fe355e684120c829be224101e8242" alt="Step 11" width="1000" height="583" data-path="img/dcr/keycloak/step_11.png" />

The next step is to generate a token and use it for accessing your **Keycloak API**. We'll use Postman for this. You will need your token URL which is also the well-known URL for your organization.
For this guide we use `https://keycloak:8443/auth/realms/master/protocol/openid-connect/token`

Your Postman request should contain the following body, where `"client_id"` and `"client_secret"` are the credentials you got from the developer portal:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_12.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=9a06f8dc67ebc62602d45ecd56424808" alt="Step 12" width="1200" height="715" data-path="img/dcr/keycloak/step_12.png" />

Note that we aren’t using any additional headers for this request, the client credentials are enough.

Once we get a response from the IDP, we can copy the `"access_token"` and use it to access our **Keycloak API**, this request will be proxied by Tyk:

<img src="https://mintcdn.com/tyk/gQqrg0xptB2_TmVu/img/dcr/keycloak/step_13.png?fit=max&auto=format&n=gQqrg0xptB2_TmVu&q=85&s=e2240a36af040bcb120ce890bf4b4919" alt="Step 13" width="1200" height="706" data-path="img/dcr/keycloak/step_13.png" />
