In this guide, we explain how to integrate the WSO2 API Store with an external Identity and Access Management server (IAM) using the Keycloak Open Source IAM to manage the OAuth clients and tokens required by WSO2 API Manager. This is a sample client implementation that consumes APIs exposed by Keycloak.
Please note that the following instructions and implementations are supported only on WSO2 API Manager v3.1.0
Download the Keycloak server 9.0.0 . Create a new realm to be used with WSO2 API Manager. This is where the clients will be created. Then create a new user and grant admin privileges inside the same realm to be able to invoke the Keycloak admin REST API.
-
Go to the Keycloak download page and download the Standalone server distribution.
-
After Downloading extract the keycloak distribution to a location in the server. This extracted directory will be referred to as KEYCLOAK_HOME :
-
Create admin user and login :
-
Create a new realm :
-
Create a client and an admin user in the newly create realm to be used for admin API invocations.
-
From the left menu click on Clients. Click the Create button on the upper right had side. Provide the Client Id as
apim-client
and the Client Protocol as openid-connect. -
From the left menu click on Users. Click the Add User button on the upper right had side. Provide the username as
apim-user
and provideapim-password
as password on the password tab. Make sure to turn off the Temporary field value. -
Move to the Role Mappings tab and from the Client Role drop down select realm-management. Select all the roles and click on add selected.
-
-
Build the project by following the steps here or you can download the built JAR artifact for the respective API Manager versions from here
-
Copy that JAR file into the
<API-M_HOME>/repository/components/lib
directory. -
Configure the
<apim-3.1.0>/repository/conf/deployment.toml
to configure the Keycloak Key Manager Change the values based on your third-party API.Note : Replace the value of the
keycloakInstanceUrl
parameter with your Keycloak deployment's base URL.The
org.wso2.keycloak.client.KeycloakClient
class, mentioned in the following example, extends the Key Manager interface.[apim.key_manager] key_manager_client_impl = "org.wso2.keycloak.client.KeycloakClient" key_validation_handler_impl = "org.wso2.keycloak.client.KeycloakKeyValidationHandler" [apim.key_manager.configuration] keycloakInstanceUrl = "http://localhost:8080" keycloakRealmName = "apim" client_id = "apim-client" username = "apim-user" password = "apim-password" [[apim.jwt.issuer]] name = "http://localhost:8080/auth/realms/apim" [apim.jwt.issuer.jwks] url = "http://localhost:8080/auth/realms/apim/protocol/openid-connect/certs"
-
Add the following configurations in the
deployment.toml
of the API Manager server to disable the grant types which are not supported by Keycloak by other thanimplicit
,authorization_code
,client_credentials
,password
# following configurations are to disable the grant_types [oauth.grant_type.uma_ticket] enable = false [oauth.grant_type.jwt_bearer] enable = false [oauth.grant_type.iwa_ntlm] enable = false [oauth.grant_type.saml_bearer] enable = false
You have connected WSO2 API Manager with a third-party Keycloak IAM server. Let's see how WSO2 API Manager creates OAuth clients in Keycloak, when applications are registered in the Devportal.
-
Start WSO2 API Manager
-
Sign in to the WSO2 API Manager Devportal
-
Create an application :
Navigate to the Applications screen and click on ADD NEW APPLICATION to create a new application.
-
Generate an Application Key :
Register an OAuth client in the Keycloak and generate the access token.
After creating an application, go to the Production Keys tab of the Application, then click the Generate Keys button.
Element Description Application Grant Types (Mandatory) Select the grant types for the application. Applicable Grant Types are: [ Authorization Code
,Implicit
,Client Credentials
,Password
]Callback URL (Optional) Redirection URI string for use in redirect-based flows. E.g., http://google.lk/ -
Validate tokens by subscribing to the Keycloak client :
If the requirement is to generate JWT tokens with scopes and invoke the APIs, please refer to Access Token Generation with Scopes to configure and generate JWT tokens with Scopes
-
Sign in to the API Publisher and deploy the sample API (PizzaShackAPI)
-
Assuming you still have the Keycloak client created earlier and subscribe to the deployed API using the created application
-
Copy the Access Token that generated in the previous step and paste it in the API Console UI and invoke the API
-
If you have published an API with Scope validation, then please follow the instructions to configure the Keycloak to generate JWT tokens with the required scopes.
Attached is a screenshot of the sample Pizzashack API
resource configured with a scope named menu
-
Login to the Keycloak's Admin console > select the respective Realm > and click on
Client Scopes
section. -
Click on
Create
and enter the required details to create a client scope in the Keycloak server. -
Once done, go to the respective created Keycloak client (e.g.
admin_KeycloakApplication_PRODUCTION
) and go to theClient Scopes
tab and select the created scope and pressAdd selected
to engage the created scope with the client
-
Login to the Devportal and navigate to the created
Application
>Production Keys
>OAuth2 Tokens
screen -
Click on
Generate Access Token
and select the scope in the displayed modal and click onGenerate
to generate the JWT token with the required scopes -
Copy and paste the Token in the Try-out console and invoke the API
Execute the following command from the root directory of the project to build the project
mvn clean install
Copy the built JAR artifact from <project>/target/
folder to <apim-3.1.0>/repository/components/lib
directory and follow the above-given instructions to configure the API Manager v3.1.0 and restart the server to take effect on the changes.