In the part 1 of this article, I gave an overview of OAuth 2 framework. In this part I will introduce Keycloak and start getting the hands dirty by setting it up for the example.
What is Keycloak?
Keycloak is an open source product Identity and Access Management (IAM) which deals with users and profile management. This system provides out of the box authentication/authorization functionalities. Avoiding the hassle and risk of building your own solution to authenticate and authorize users. On this example, Keycloak will work as the Authentication Server (see the part 1 for more details) providing realm and application token, allowing the access to a protected endpoint (Resource Server).
Let’s get the hands dirty
On this section I will explain how to set up Keycloak for implementing an OAuth authentication. This includes the creation of a realm, create a client and configure roles.
1) Run Keycloak on docker
Before we can start using Keycloak, it’s necessary to run it. For the sake of simplicity, we can simple use a docker image. Also a database is necessary and in this example I will use Postgres.
For this example, use the following docker-compose:
Then run the following command on the terminal:
docker-compose up -d
2) Creating a realm
A realm represents a security domain for the application and is highest part where users, clients and roles are associated. To create a new realm on Keycloak, you should hover on left side where is “Master” (this is the default realm created by Keycloak), then click on the button “Add realm”
3) Create a client
A client on Keycloak is an application that want to authenticate and provide a single sign-on solution . To create a new client, go to Clients on the left side menu, then click on Create (Figure 3.1).
To add a client, specify a Client ID and select openid-connect as Client Protocol ID (Figure 3.2). The Client ID works like the username of the application.
After creating the client, change the Access Type to confidential, this will make the client secret required in order to authenticate our application (Figure 3.3). Pay attention if the option Service Accounts Enabled is “On”.
On the same page, specify a “Valid Redirect URIs”. For this example add http://localhost:8080/* (Figure 3.4)
On the tab Credentials on the client creation page, the client secret is shown (Figure 3.5). The secret works like a password of the application and will be used later to generate the authentication token.
4) Create client scope
The scopes allows the creation of a common set of mappers and roles that can be shared between multiple clients.
To create new client scope, go to the option Client Scopes on the left side menu and click on Create (Figure 4.1).
Next specify a name a click on save (Figure 4.2). This will create the client scope which can be associated with a client.
The next step is the client scope association which can be done by accessing the Clients option on the left side menu. Then, clicking on the Client Scopes tab will show all Available Client Scopes. Next, select the recently created scope and click on the Add selected button.
We have configured Keycloak for this example setting up the client, client scope and associating it with the client. On the next part I will show how we can put everything together using Spring Boot and Spring Security. See you on the next part 🙂
Be First to Comment