My setup has three components:
The frontend will use Keycloak to let users sign in and use the access tokens to authenticate requests to the backend. So far so good.
Now I want third party applications to be able to make authenticated requests against the backend and I am wondering how that can be realized using Keycloak? My idea is to issue a new set of credentials for each customer. Their application then talks to Keycloak to get access tokens. I can then use Keycloak to manage access control for all users of the API.
I finally found a solution that works well and seems to be "the Keycloak way" to issue credentials to external applications. To create a new set of credentials, add a new Keycloak client and change the following settings:
The external application will use our newly created client's name as the client_id
. The client_secret
was generated automatically and can be found under the Credentials tab.
If your Keycloak-protected services are configured to check the aud
claim of incoming Bearer tokens, a second step is necessary. By default, the audience of the JWT tokens that Keycloak issues to your client will be set to your client's name, so they will be rejected by your services. You can use Client Scopes to modify that behavior:
Keycloak will now add your service's name to the aud
claim of all JWT tokens it issues to your new client. Check out the Keycloak documentation on Service Accounts for more details.
An external application can now use its credentials to obtain an access token from Keycloak's token endpoint:
POST {keycloak-url}/auth/realms/atlas/protocol/openid-connect/token
Content-Type
header to application/x-www-form-urlencoded
grant_type=client_credentials
in the request body