Keycloak Setup#
In the section Docker With Keycloak, we have created a
Keycloakserver by docker. Now, we will continue to set up thisKeycloakfor OAuth2 using.
Realm#
What Is The Realm?#
Realmis a space in ourKeycloakauthorization server and by using this space, we can have our own users, roles and clients. Whatever we created in a particularRealmwill be separated with otherRealms.- Let's image that we are working in an enterprise applications and it will have many environments like DEV, TEST, UAT, STAGING and PRODUCTION. So we have to create many
Realmsfor those environments and eachRealmis a dedicated space for handling a specific environment or a specific application or a specific scenario.
Create A Realm#
- To create a
Realm, let's select the button as in the image below.
- Then put a name for it and click button
Create.
- Then you can see your
Realmis created.
Create Realm Roles#
-
Now, let's create some
Rolesfor ourRealm. So inKeycloakwe have 3 roles;- Realm Role
- Client Role
- Composite Role
-
There are no
User RolesinKeycloak. You most likely confused that with User Role Mapping, which is basically mapping a role (realm, client, or composite) to the specific user. - In order to find out how these roles actually work, let's first take a look at a simple
Realmmodel. As you can see in picture below, every Realm has one or multipleClientsand everyClientcan have multipleUsersattached to it.
| Role | Descriptions |
|---|---|
| Realm Role: | It is a global role, belonging to that specific realm. You can access it from any client and map to any user. Ex Role: 'Global Admin, Admin' |
| Client Role: | It is a role which belongs only to that specific client. You cannot access that role from a different client. You can only map it to the Users from that client. Ex Roles: 'Employee, Customer' |
| Composite Role: | It is a role that has one or more roles (realm or client ones) associated to it. |
- To create
Realm Role, let's click onRealms rolesand then click buttonCreate role.
- Then we put a role name and click save button.
- Then you can see
Realm roleshave been created.
Client Credentials Grant Type#
- In this example, we will try to setup the OpenID Connect for
Client Credentials Grant TypeonKeycloakserver. Basically theOpenID Connectuses the same flow as theOAuth2and it is just different about thescopethat is sent to the authorization server. - If you have not know
Client Credentials Grant Typeor OAuth2 Flows, so you can read this topic OAUTH2 Flow. - Basically the
Client Credentials Grant Typeflow is described as the diagram below. We use this authentication flow only if there is no user and UI involved. Like 2 different applications want to share data between them using backend APIs.
sequenceDiagram
Client->>Auth Server: 1. I want to access protected resources. Here <br/> are my client credentials. No user involved in this.
Auth Server->>Client: 2. Hey Client, The credentials providided are <br/> correct. Here is the TOKEN to access the user <br/> resources.
Client->>Resource Server: 3. Hey Resource Server, I want to access protected resources. <br/> Here is the token from the Authz Server
Resource Server->>Client: 4. Hey Client. Your token is validated successfully. <br/> Here are the resources you requested.
- Note: In
Client Credentials Grant Typewe should not return therefresh tokenfollowing RFC6749 section 4.4.3.- Because authentication server have to take one more step for verifying the refresh token with using the same credentials which is used for getting the access token.
Create Client#
- So, to create a
Clientin aRealm, let's follow steps as in the image below. We will select theRealmthen clickClientsand click buttonCreate client.
- Then in the
General Setting, choose theClient TypeasOpenID Connectand put a name for fieldClient IDand clickNextbutton.
- Now, at step
Capability config, let's- turn on
Client authentication. - uncheck
Standard flowbecause it is used forAuthorization Code Grant Type Flow - uncheck
Direct access grantsbecause it is used forResource Owner Password Credentials Grant. - check
Service accounts rolesbecause it is used forClient Credentials Grant Typewhich is matched with this example. - click button
Save
- turn on
- Then you can see your
Clientas below.
Add Realm Roles#
- Next, we will add
Realm Rolesinto our client. You should chooseService accounts rolestab. Then click buttonAssign role.
- Then select the
Realm Rolesthat you want to add and click buttonAssign.
Testing#
- So,
Keycloakprovide for us an api to get the information about ourRealmand it calledDiscovery endpointand the endpoint will have the pattern as below.
1 2 3 4 5 | |
- We can try it with postman and you can see the result as below.
-
Now, with the information from the
discovery endpoint, we will get thetoken_endpointand use it with the client credentials from the created client in ourRealmfor getting theaccess_token. -
The
client_idcan be got as the image below:
- The
client_secretcan be got as the image below:
- The
grant_typeshould be hard code asclient_credentials, because we are usingClient Credentials Grant Type. - For the
scopeyou should put it with valueopenIdto let theKeycloakserver know you are usingOpenId Connect. You can also put any more value that you get from the fieldscopes_supportedin thediscovery endpointresponse. - Now, let's use postman and try to call the
token_endpoint, you will received anaccess_tokenand theid_tokenas in the image below.
Authorization Code Grant Type#
- In this example, we will try to setup the OpenID Connect for
Authorization Code Grant TypeonKeycloakserver. Basically theOpenID Connectuses the same flow as theOAuth2and it is just different about thescopethat is sent to the authorization server. - If you have not know
Authorization Code Grant Typeor OAuth2 Flows, so you can read this topic OAUTH2 Flow. - The
Authorization Code Grant Typeflow is described as the diagram below.
sequenceDiagram
User->>Client: 1. I want to access my resources
Client->>User: 2. Tell the Auth Server that you are fine to do this action.
User->>Auth Server: 3. Hello Auth Server, please allowed the client to access my resources. <br/> Here is my credentials to prove my identity
Auth Server->>Client: 4. Hey Client, user allowed you to access his <br/> resources. Here is AUTHORIZATION CODE.
Client->>Auth Server: 5. Here is my client credentials, AUTHZ <br/> CODE. Please provide me a token.
Auth Server->>Client: 6. Here is the token from Authorization Server
Client->>Resource Server: 7. Hey Resource Server, I want to access the user resources. <br/> Here is the token from the Authz Server
Resource Server->>Client: 8. Hey Client. Your token is validated successfully. <br/> Here are the resources you requested.
- The
Authorization Code grant typeis used by confidential and public clients to exchange an authorization code for an access token. After the user returns to the client via the redirect URL, the application will get the authorization code from the URL and use it to request an access token.
Create Client#
- Like we created a client in a
Realmbefore, let's follow steps as in the image below. We will select theRealmthen clickClientsand click buttonCreate client.
- Then in the
General Setting, choose theClient TypeasOpenID Connectand put a name for fieldClient IDand clickNextbutton.
- Now, at step
Capability config, let's- check
Client authenticationbecause it defines the type of the OIDC client. When it's ON, the OIDC type is set to confidential access type. When it's OFF, it is set to public access type - check
Standard flowbecause it is used forAuthorization Code Grant Type Flow - uncheck
Direct access grantsbecause it is used forResource Owner Password Credentials Grant. - click button
Save
- check
- Then you can see your
Clientas below.
Create Realm User#
- For
Authorization Code Grant Type, we need to create a Realm User because Keycloak authorization server will require clients to prove their identity by giving username and password which are created in the Keycloak Realm users. - So, to create a user for our Realm, we should flow steps below.
- We will select the
Realmthen clickUserand click buttonAdd user.
- Then we put a username, give some information and click button
Create.
- Next, when user is created then we continue to choose
Credentialstab and clickSet passwordand put the password as in the image below. We turn off fieldTemporarybecause we don't want the user have to change password at the first login.
- Next, we will choose the
Role mappingtab and clickAssign rolebutton to add the Realm role for this user.
Password Grant Type Flow#
- In this example, we will try to setup the OpenID Connect for
Authorization Code Grant TypeonKeycloakserver. Basically theOpenID Connectuses the same flow as theOAuth2and it is just different about thescopethat is sent to the authorization server. - If you have not know
Password Grant Type Flowor OAuth2 Flows, so you can read this topic OAUTH2 Flow. - The
Password Grant Type Flowflow is described as the diagram below.
sequenceDiagram
User->>Client: 1. I want to access my resources
Client->>Auth Server: 2. Hello Auth Server, User want to access <br/> his/her resources. Here are the credentials of <br/> the User
Auth Server->>Client: 3. Hey Client, The credentials providided are <br/> correct. Here is the TOKEN to access the user <br/> resources.
Client->>Resource Server: 4. Hey Resource Server, I want to access the user resources. <br/> Here is the token from the Authz Server
Resource Server->>Client: 5. Hey Client. Your token is validated successfully. <br/> Here are the resources you requested.
- We use this authentication flow only if the
Client,Authorization ServerandResource Serverare maintained by the same organization. - This flow will be usually followed by the enterprise applications who want to separate the Auth flow and business flow. Once the Auth flow is separated different applications in the same organization can leverage it.
Create Client#
- Like we created a client in a
Realmbefore, let's follow steps as in the image below. We will select theRealmthen clickClientsand click buttonCreate client.
- Then in the
General Setting, choose theClient TypeasOpenID Connectand put a name for fieldClient IDand clickNextbutton.
- Now, at step
Capability config, let's- Uncheck
Standard flowbecause it is used forAuthorization Code Grant Type Flow - Check
Direct access grantsbecause it is used forResource Owner Password Credentials Grant. - click button
Save
- Uncheck
- Then you can see your
Clientas below.
Create Realm User#
- Like
Authorization Code Grant Type, we need to create a Realm User because Keycloak authorization server will require clients to prove their identity by giving username and password which are created in the Keycloak Realm users. - So, to create a user for our Realm, we should flow steps below.
- We will select the
Realmthen clickUserand click buttonAdd user.
- Then we put a username, give some information and click button
Create.
- Next, when user is created then we continue to choose
Credentialstab and clickSet passwordand put the password as in the image below. We turn off fieldTemporarybecause we don't want the user have to change password at the first login.
- Next, we will choose the
Role mappingtab and clickAssign rolebutton to add the Realm role for this user.



























