# Configure Keycloak

This page describes how to configure a Keycloak instance as an external IdP for fulfillmenttools. Log in with an admin user to the administrator console in Keycloak; the default URL is: `https://<keycloak-instance>/admin/master/console/`.

## **Create a Client**

If there are existing multiple Keycloak realms, use the dropdown (top left corner) to select the correct realm, which should be used as external IDP for fulfillmenttools. For each fulfillmenttools environment (typically a test and a production environment), create a separate client in Keycloak. To do that, follow the steps below.

{% stepper %}
{% step %}
**Create a client**

Browse to **Clients** (in the left-hand navigation) and select **Create client** to open the client configuration wizard.

<div data-full-width="false"><figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FLu0YA1gBieLgX0gmmLt1%2FScreenshot_20250109_162416.png?alt=media&#x26;token=44ae2263-c4bf-4b31-bc86-d39af9544cf3" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Select the client type**

In the **Client type** dropdown, select **OpenID Connect** type, then input a **Client ID** (for example, **fulfillmenttools-production-environment**). Then click **Next**.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2F6tXLX5pkNx8vTrSI3ktN%2FScreenshot_20250109_163105.png?alt=media&#x26;token=0f362d24-c639-4308-b96c-e1da593bcd48" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Activate implicit flow**

Click the check box to activate **Implicit flow** and then click **Next.**

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FvGBTrjWEFxeDZEfHkkBR%2FScreenshot_20250109_163331.png?alt=media&#x26;token=2a46abcb-ee72-4bf1-ae7d-4e73667fd458" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
&#x20;**Add 3 Redirect URLs**

They should have the pattern:

* `https://ocff-<tenantName>-<pre|prd>.firebaseapp.com/__/auth/handler`
* `https://ocff-<tenantName>-<pre|prd>.web.app/__/auth/handler`
* `https://pick-<tenantName>-<pre|prd>.web.app/__/auth/handler`

Then, click **Save** to create the client. Save the **Client ID** as it must be supplied to us via API.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FVQeRVNLE2nVXNUrFYVVZ%2FScreenshot_20250109_163951.png?alt=media&#x26;token=9f4842d5-46b1-49c8-aace-0bc860b816a2" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## **Activate Authentication and Create Credentials (Client Secret)**

On the Client configuration page activate **Client authentication** and press **Save**.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FmOjAQ61J9wmz84Eo2wB4%2FScreenshot_20250109_164602.png?alt=media&#x26;token=5224405a-7c34-4ef8-a136-f8392e7cad6d" alt=""><figcaption></figcaption></figure>

At the top of the Client, select the **Credentials** tab. Select **Client Id and Secret**. The **Client Secret** must be supplied to fulfillmenttools at a later step via API.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FqengkkBT0T5iMx93munm%2FScreenshot_20250109_165040.png?alt=media&#x26;token=f3c93dc9-9656-4014-9927-71b506270541" alt=""><figcaption></figcaption></figure>

## **Create Client Roles**

Now, create the client roles that correspond to the fulfillmenttools roles. Navigate to **Roles** and press **Create role**.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FYlC6irPRZsiVXqvHQ4Fl%2FScreenshot_20250109_165249.png?alt=media&#x26;token=039ef2e8-f957-4d05-b2ab-4bf39839b234" alt=""><figcaption></figcaption></figure>

Create 3 roles: `ADMINISTRATOR`, `SUPERVISOR`, and `FULFILLER`. Choose an arbitrary **Description**. The **Role name** must be one of the above-mentioned roles: `ADMINISTRATOR`, `SUPERVISOR`, or `FULFILLER`.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2Fj4NWRP1lEuMzTwIGgHs8%2FScreenshot_20250109_165352.png?alt=media&#x26;token=114c7596-bcb4-473b-908c-fe6adbf15719" alt=""><figcaption></figcaption></figure>

The app roles should look similar to this:

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2F6Apgswouu8NfktiIcPzM%2FScreenshot_20250109_165608.png?alt=media&#x26;token=b4ad9365-d7a2-4b09-a337-73aebe281824" alt=""><figcaption></figcaption></figure>

## **Linking Client Roles to users/groups**

Next, link Keycloak groups to these client roles. Individual Keycloak users can be added to these groups to assign roles to users.

In the Keycloak administration console, select **Groups** on the left navigation pane.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FeSJvvPEcNjACDz5R6LMb%2FScreenshot_20250109_165923.png?alt=media&#x26;token=ad4c39d3-cd1a-43be-9344-afcfd7fd831a" alt=""><figcaption></figcaption></figure>

Typically, there exist three Keycloak groups, one for each fulfillmenttools role. By clicking **Create group***,* create one Keycloak group for each fulfillmenttools role (for example, fft-role-administrator, fft-role-supervisor, fft-role-fulfiller).

Now, assign client roles to these groups. Therefore, select the created group in the *Groups* section.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FVIAbrQgjM2tGo2YI6GHf%2FScreenshot_20250109_170430.png?alt=media&#x26;token=292e602d-8ca5-463f-a6a7-1d1407c90473" alt=""><figcaption></figcaption></figure>

Then, select **Role mapping** in the tabs and press **Assign role**. Create a total of three groups for a production setup, one for each role. Then, every member of the group is assigned the corresponding fulfillmenttools role.

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FdM5Bzqu1gvxMKiFz2mj3%2FScreenshot_20250109_170634.png?alt=media&#x26;token=5abf05db-f2cb-4297-932c-6a0b143d46f0" alt=""><figcaption></figcaption></figure>

Select the corresponding role from the client, and press **Assign**.

{% hint style="info" %}
In the following screenshot, the administrator role is created. This has to be done for SUPERVISOR and FULFILLER similarly.
{% endhint %}

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2F2YxyWgxQF1M7a2jdizFR%2FScreenshot_20250109_171400.png?alt=media&#x26;token=0280aff0-4361-46cf-a393-d4b003ae9f3d" alt=""><figcaption></figcaption></figure>

## **Adding (facility) groups to the token**

fulfillmenttools models the assigned facilities to a user using Keycloak groups. In the Keycloak administration console, select **Clients** and the **Client scopes** tab. Select the \<client ID>-dedicated entry:

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FG3KlYYeOKd7wfGf9uDFv%2FScreenshot_20250109_171753.png?alt=media&#x26;token=ae686df5-a4cc-46d6-a13b-7d9261e6c125" alt=""><figcaption></figcaption></figure>

Then, select **Configure a new mapper.**

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2F7g46SAoPiXwDhrnFAvY2%2FScreenshot_20250109_171938.png?alt=media&#x26;token=a2fa4955-8763-412b-baa1-e2e3eca562be" alt=""><figcaption></figcaption></figure>

Select **Group Membership***.*

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FMcCjAqCuWwf88fZDLu7h%2FScreenshot_20250109_172032.png?alt=media&#x26;token=54126974-093a-428d-8a08-dffec6ae74ef" alt=""><figcaption></figcaption></figure>

The name of the token mapper must be **groups***.* Then press **Save***.*

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FHCz94uEXSrGSxqaTQYa8%2FScreenshot_20250109_172135.png?alt=media&#x26;token=66e93406-fab6-4c4d-b191-fa5e738ded6b" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Now, all groups of a user are put into the token. fulfillmenttools leverages this to identify which user is allowed to access a facility in fulfillmenttools.
{% endhint %}

## **Disable "Full scope allowed"**

In the Keycloak administration console, select **Clients** and the **Client scopes** tab. Select the \<client ID>-dedicated entry:

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FG3KlYYeOKd7wfGf9uDFv%2FScreenshot_20250109_171753.png?alt=media&#x26;token=ae686df5-a4cc-46d6-a13b-7d9261e6c125" alt=""><figcaption></figcaption></figure>

Then, open the **Scope** tab and disable the **Full scope allowed***:*

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FxNOUBHtOkyynHmrrDASW%2FScreenshot_20250114_150507.png?alt=media&#x26;token=336b7110-50a4-4601-989a-d1cf09441a4f" alt=""><figcaption></figcaption></figure>

## Configure Client Scopes

In the client configuration, select the **Client Scopes** tab. Add the email, profile, and openid scopes:

<figure><img src="https://4170739437-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLrrr5jgTsDuR38gNJIrm%2Fuploads%2FRDR0MrACZxRjkR47fONr%2FScreenshot_20250114_152013.png?alt=media&#x26;token=0d5062ea-2e27-46a4-bdcc-ef996a726704" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If the OpenID scope is unavailable on the Keycloak instance, create it in the admin console (Client Scopes in the left navigation pane).
{% endhint %}

## Summary

Gather the following information to configure the external IDP via API:

* `CLIENT_ID`
  * This is the `Client ID` of the Client in Keycloak
* `CLIENT_SECRET`
  * This is the value of the secret created
* Keycloak Facility Group Names
  * group names of the Keycloak groups representing a facility
* `FACILITY_ID_1`, `FACILITY_ID_1`
  * id of the facilities in fulfillmenttools that this group should have access to

To register the OIDC provider to fulfillmenttools, execute the following call:

```http
POST https://{projectId}.api.fulfillmenttools.com/api/configurations/oidcproviders
```

```json
{
    "name": "Keycloak Login",
    "status": "ACTIVE",
    "clientId": "{CLIENT_ID}",
    "clientSecret": "{CLIENT_SECRET}",
    "issuer": "https://{URL}/realms/{realm}",
    "customParameters": [],
    "assignedGroups": [
        {
            "group": "{KEYCLOAK_GROUP_NAME}",
            "facilityRefs": [
                "{FACILITY_ID_1}",
                "{FACILITY_ID_2}"
            ]
        }
    ]
}
```
