Keycloak
This page shows how to let people sign in to MicroB2B with Keycloak. It also shows how a script gets a Keycloak token for the REST API. Read identity providers first for the concepts.
The examples use:
- MicroB2B at
https://b2b.example.com(soPUBLIC_URL=https://b2b.example.com), - Keycloak at
https://keycloak.example.com, with a realm you choose, - the provider name
keycloakin MicroB2B.
Create the client in Keycloak
- In your realm, create a client. Set Client type to OpenID Connect and Client ID to
microb2b. - Under Capability config:
- turn Client authentication on (this makes it a confidential client),
- turn Standard flow on,
- turn direct access grants, implicit flow and service accounts off.
- Under Access settings, set Valid redirect URIs to
https://b2b.example.com/ui/auth/keycloak/callback. Leave Web origins empty. - Under Advanced > Advanced settings, set Proof Key for Code Exchange Code Challenge Method to
S256. - On the Credentials tab, copy the client secret.
Add the provider in MicroB2B
Go to Access > Identity providers and click New identity provider.
Fill in:
Setting Value Name keycloakDisplay Name for example Company loginIssuer URL https://keycloak.example.com/realms/<realm>Client ID microb2bClient Secret the secret from Keycloak Save.
Sign out, then click Sign in with Company login on the login page.
Older Keycloak versions (before the Quarkus distribution) have /auth in the path. Their issuer URL is https://keycloak.example.com/auth/realms/<realm>.
Who may sign in
Every user of the realm can sign in, unless you restrict the client in Keycloak. For example, add a condition on a group or role to the client’s authentication flow. Remember that every MicroB2B user is an administrator.
A script with a Keycloak token
A script or another system can call the REST API with a Keycloak access token. Keycloak does not put the client in the audience of its access tokens by default, so you add an audience yourself.
- Create a second client, for example
importer. Turn Client authentication on and Service accounts roles on. Turn Standard flow off. - Open the client’s Client scopes tab and its dedicated scope. Click Add mapper > By configuration > Audience.
- Set Included Custom Audience to
microb2b-apiand turn Add to access token on. - In MicroB2B, open the
keycloakprovider and set API Audience tomicrob2b-api. - Register the client in MicroB2B as a federated service account. Choose the
keycloakprovider and enter the token’s subject (sub) exactly.
The script then gets a token and calls the API:
TOKEN=$(curl -s -d grant_type=client_credentials -d client_id=importer -d client_secret=... \
https://keycloak.example.com/realms/<realm>/protocol/openid-connect/token | jq -r .access_token)
curl -H "Authorization: Bearer $TOKEN" https://b2b.example.com/api/v1/statsMicroB2B refuses a token whose subject is not a registered service account or a MicroB2B user of that provider. If the call answers 401 INVALID_TOKEN, check the reason in the MicroB2B log. See troubleshooting sign-in.