the/experts. Blog

Cover image for Identity and Access Management (IAM) with Keycloak
Maik Kingma
Maik Kingma

Posted on

Identity and Access Management (IAM) with Keycloak

An overview

Keycloak is a popular open-source solution for identity and access management by Red Hat that can be used to secure a variety of applications and services. It provides a centralized platform for managing user accounts, assigning roles and permissions, and enforcing security policies. Keycloak offers a range of features, including support for multiple authentication methods (e.g. username/password, social login, and two-factor authentication), user federation and identity brokering, and fine-grained access control.

Keycloak can be used to secure web applications, mobile applications, and RESTful APIs, and it integrates with many popular platforms and frameworks. This makes it easy to add authentication and authorization to your applications, without having to write a lot of custom code. Keycloak also provides a user-friendly web-based administration interface, which allows you to manage users, roles, and policies from a central location.

Overall, Keycloak is a powerful and flexible solution for managing access to applications and services. It can help to improve security and compliance, and it can make it easier to manage user accounts and access control policies across your organization.

It supports the most important standards for identity and access management (IAM) out of the box, including the following:

  • OpenID Connect (OIDC): Keycloak supports OIDC for authenticating users and obtaining their user profile information. OIDC is an open, interoperable standard for single sign-on and identity provision, and it is built on top of OAuth 2.0.
  • OAuth 2.0: for securing APIs and delegating access to user accounts. OAuth 2.0 is a widely-used standard for authorization that allows users to grant third-party applications access to their resources without sharing their credentials.
  • SAML 2.0: for federating identities between Keycloak and other identity providers. SAML 2.0 is an XML-based standard for securely exchanging authentication and authorization data between systems.
  • LDAP: for integrating with existing LDAP directories. LDAP is a widely-used protocol for managing and accessing directory information, such as user accounts and group membership data.

The Keycloak solution consists of several components that work together to provide authentication and authorization services. Some of the main components of Keycloak include the following:

  • User database: Keycloak maintains a local user database that stores user accounts, roles, and other information. This user database can be populated manually, or it can be synchronized with external sources such as LDAP directories or social login providers.
  • User interface: Keycloak provides a web-based user interface that allows administrators to manage users, roles, and policies. The user interface also provides login and registration pages for end users, as well as a user account management page.
  • Administration REST API: Keycloak exposes a RESTful API that can be used to manage Keycloak from external applications or services. The API allows you to create and manage users, roles, and policies, and to perform other administrative tasks.
  • Authentication flows: Keycloak defines a set of authentication flows that specify the steps required to authenticate a user. These flows can be customized to support different authentication requirements, such as username/password authentication, social login, or two-factor authentication.
  • Realms: They separate and isolate different groups of users, applications, and services. Each realm in Keycloak has its own unique set of users, roles, and policies, and it can be configured independently of other realms. Realms are useful for organizing and managing users and applications in large or complex systems, and they can be used to enforce security and compliance policies in different parts of your system.
  • Clients: Clients are entities that represent applications or services that can authenticate with Keycloak and request access to protected resources. Clients can be web applications, mobile applications, or RESTful APIs, and they can use a variety of protocols, such as OIDC, OAuth 2.0, SAML, or LDAP. When you create a client in Keycloak, you can configure settings such as the client's name and description, the authentication flow that should be used, and the access policies that should be enforced. You can also specify the redirect URIs that the client is allowed to use. Since the release of Keycloak 20.0.0 you can also configure the logout redirect URIs, which adds an extra layer of security to your application landscape. Additionally, you can also configure mappers that determine how user attributes and claims are mapped between Keycloak and the client. Clients are an important part of Keycloak's architecture, as they provide a way to securely authenticate users and manage access to protected resources. Clients can be associated with specific realms, and they can be configured to use the users, roles, and policies defined in those realms. This allows Keycloak to provide a centralized and consistent approach to authentication and authorization for all the applications and services in your system.
  • Protocol mappers: Keycloak includes a number of protocol mappers that can be used to map user attributes and claims from Keycloak to the claims and attributes used by the different protocols (OIDC, OAuth 2.0, SAML, or LDAP). Protocol mappers allow Keycloak to work with a wide range of external systems and applications.
  • Policies: Keycloak allows you to define policies that determine whether a user is authorized to access a specific resource or perform a specific action. These policies can be based on the user's role, their group membership, or custom attributes.

There are several ways to customize Keycloak to meet your specific needs. Some of the ways you can customize Keycloak include the following:

  • Themes: Keycloak allows you to customize the look and feel of the login and user account pages by creating your own themes. You can create a new theme by copying the default theme and then modifying the CSS, JavaScript, and other static resources. Alternatively, you can also extend already existing themes by basing your custom theme on those.
  • Localization: Keycloak provides support for multiple languages, and you can customize the language-specific messages and labels used in the user interface. You can do this by creating your own language-specific properties files and adding them to Keycloak.
  • Login and registration pages: Keycloak allows you to customize the login and registration pages by providing your own HTML and JavaScript. You can create custom pages that match the look and feel of your application, and you can use the Keycloak JavaScript adapter to integrate Keycloak's authentication and authorization functionality into your custom pages.
  • Custom authentication flows: If the standard authentication flows provided by Keycloak do not meet your requirements, it allows you to define your own authentication flows to support custom authentication requirements. For example, you could create a flow that requires users to authenticate using both their username and password, as well as a one-time password generated by a mobile app (2FA).
  • Custom user attributes: Keycloak allows you to add custom attributes to user profiles to store additional information about users. You can define the name, data type, and other properties of your custom attributes, and then retrieve and use this information in your applications and services.
  • Custom event listeners: Keycloak allows you to define custom event listeners that can be used to trigger custom actions in response to events in Keycloak. For example, you could create an event listener that sends an email to a user when they are added to a new role, or that updates an external system when a user's profile is updated.

The ways how to customize Keycloak that are listed above, are only a sample of what is actually possible. You can extend and customize your Keycloak instance in any way you want (the question really is how much should you customize). I want to use this blog series to show how to achieve these customizations by providing code samples and best practice guides. I will be using configuration as code exclusively, applying all my customizations with the Keycloak Admin REST client. Furthermore, I will also elaborate later on, why I choose configuration as code and what benefits this entails.

Stay tuned for some coding fun with Keycloak!

Discussion (0)