All Projects → sventorben → keycloak-restrict-client-auth

sventorben / keycloak-restrict-client-auth

Licence: MIT License
A Keycloak authenticator to restrict authorization on clients

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to keycloak-restrict-client-auth

keycloak-home-idp-discovery
Keycloak: Home IdP Discovery - discover home identity provider or realm by email domain
Stars: ✭ 42 (+23.53%)
Mutual labels:  keycloak, keycloak-provider, keycloak-extension, keycloak-authenticator
Php Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
Stars: ✭ 865 (+2444.12%)
Mutual labels:  roles, authorization, access-control
access-controller
A highly scalable open-source implementation of an access-control engine inspired by Google Zanzibar-"Google’s Consistent, Global Authorization System"
Stars: ✭ 61 (+79.41%)
Mutual labels:  authorization, access-control, access-management
Monarchy
Hierarchical access management system with advanced roles inheritance. 🦋
Stars: ✭ 48 (+41.18%)
Mutual labels:  roles, authorization, access-control
Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+4967.65%)
Mutual labels:  roles, authorization, access-control
Security.identity
.NET DevPack Identity is a set of common implementations to help you implementing Identity, Jwt, claims validation and another facilities
Stars: ✭ 165 (+385.29%)
Mutual labels:  roles, authorization, access-control
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+355.88%)
Mutual labels:  roles, authorization, access-control
nova-permissions
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.
Stars: ✭ 115 (+238.24%)
Mutual labels:  roles, authorization, access-control
lastkeypair
A serverless SSH certificate authority to control access to machines using IAM and Lambda
Stars: ✭ 39 (+14.71%)
Mutual labels:  authorization, access-control
HeimGuard
🛡 A simple library that allows you to easily manage permissions in your .NET projects.
Stars: ✭ 77 (+126.47%)
Mutual labels:  roles, authorization
speedle-plus
Speedle+ is an open source project for access management. It is based on Speedle open source project and maintained by previous Speedle maintainers.
Stars: ✭ 45 (+32.35%)
Mutual labels:  authorization, access-control
server
AuthzForce Server (Community Edition)
Stars: ✭ 48 (+41.18%)
Mutual labels:  authorization, access-control
keycloak-mysql-user-federation
Keycloak user federation extension to connect to a MySQL database containing users
Stars: ✭ 17 (-50%)
Mutual labels:  keycloak, keycloak-spi
authcheck
Analysis for access-control vulnerabilities in Java Spring Security applications.
Stars: ✭ 14 (-58.82%)
Mutual labels:  authorization, access-control
secure-oauth2-oidc-workshop
Hands-On Workshop for OAuth 2.0 and OpenID Connect 1.0
Stars: ✭ 58 (+70.59%)
Mutual labels:  keycloak, authorization
casbin-ex
An authorization library that supports access control models like ACL, RBAC, ABAC in Elixir
Stars: ✭ 37 (+8.82%)
Mutual labels:  authorization, access-control
react-rbac-ui-manager
react-rbac-ui-manager is a simple RBAC (Role Based Access Control) user interface library based on the material design system using the Material-UI lib.
Stars: ✭ 73 (+114.71%)
Mutual labels:  authorization, roles-management
objection-authorize
isomorphic, "magical" authorization integration with Objection.js 🎉
Stars: ✭ 71 (+108.82%)
Mutual labels:  authorization, access-control
sheriff
Build simple and robust authorization systems with just Elixir and Plug
Stars: ✭ 39 (+14.71%)
Mutual labels:  authorization, access-control
Casbin4D
An authorization library that supports access control models like ACL, RBAC, ABAC in Delphi
Stars: ✭ 25 (-26.47%)
Mutual labels:  authorization, access-control

Keycloak: Restrict user authorization on clients

This is a simple Keycloak authenticator to restrict user authorization on clients.

GitHub release (latest SemVer) Keycloak Dependency Version Keycloak.X ready GitHub Release Date Github Last Commit

CI build open issues

What is it good for?

Every now and then I get asked whether it is possible to restrict user authorization on certain clients.

Generally the question goes like this

Is it possible to allow specific users to authenticate to a client while rejecting others?

As of today, my general answer is "no, at least not out of the box". And in general my advice is to redesign and relocate the policy enforcement point (PEP). However, since this question popped up so often, I wrote this simple authenticator to support this functionality.

How does it work?

The authenticator can work either role-based or policy-based.

Role-based mode

In this mode, the authenticator uses client roles to restrict authentication. It works like this:

  • The authenticator checks whether a client defines a role named restricted-access
    • If it does the authenticator checks whether the user has that role
      • If it does, the authenticator returns success (i.e. authentication is successful)
      • If it does not, the authenticator returns failure (i.e. authentication is unsuccessful)
    • If it does not, the authenticator returns success (i.e. authentication is successful).

This means that you can enable the authenticator on a per-client basis by adding a client role named restricted-access to your client. A client with that role has the authenticator enabled. Only users with that role can authenticate to that client.

Policy-based mode

In this mode, the authenticator uses client resources, permissions and policies to restrict authentication. This mode only works for confidential OIDC clients with authorization enabled. It works like this:

  • The authenticator checks whether a client defines a resource named Keycloak Client Resource
    • If it does, the authenticator checks whether policies and permission evaluate to PERMIT
      • If it does, the authenticator returns success (i.e. authentication is successful)
      • If it does not, the authenticator returns failure (i.e. authentication is unsuccessful)
    • If it does not, the authenticator returns success (i.e. authentication is successful).

This means that you can enable the authenticator on a per-client basis by adding a resource named Keycloak Client Resource to your client. A client with that resource has the authenticator enabled. Users will only be able to authenticate to such a client if the associated policies and permission permit access.

How to install?

Download a release (*.jar file) that works with your Keycloak version from the list of releases. Follow the below instructions depending on your distribution and runtime environment.

Wildfly-based distro

Create a Wildfly module and deploy it to your Keycloak instance. For details please refer to the official documentation.

For convenience, here is a module.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="keycloak-restrict-client-auth">
    <resources>
        <resource-root path="keycloak-restrict-client-auth.jar"/>
    </resources>
    <dependencies>
        <module name="org.keycloak.keycloak-services"/>
    </dependencies>
</module>

Quarkus-based distro (Keycloak.X)

Copy the jar to the providers folder and execute the following command:

${kc.home.dir}/bin/kc.sh config

Container image (Docker)

For Docker-based setups mount or copy the jar to

  • /opt/jboss/keycloak/providers for Wildfly-based distro or Keycloak.X prior to version 15.1.0
  • /opt/keycloak/providers for Keycloak.X from version 15.1.0

You may want to check docker-compose.yml as an example.

Maven/Gradle

Packages are being released to GitHub Packages. You find the coordinates here!

It may happen that I remove older packages without prior notice, because the storage is limited on the free tier.

How to configure?

  • Add the authenticator to the desired flow.

  • Mark the authenticator as Required.

  • Then bind your newly created flow as desired - either as a default for the whole realm or on a per-client basis.

    See the image below for an example. Example flow

  • Follow instructions below for the desired mode

⚠️ User identity:

The authenticator needs a user identity to check whether the user has the desired role or not. Hence, ensure that you have steps/executions in your flow prior to this authenticator that can ensure user's identity.

Role-based mode

  1. Configure the authenticator by clicking on Actions -> Config and select client-role as the Access Provider. Role-based access restriction

  2. Add a role named restricted-access to the client you want to restrict access to.

    See the image below for an example. Client role configuration

  3. Afterwards, no user can authenticate to this client. To allow a user to authenticate, assign the role restricted-access to the user. You may do so either by assigning the role to the user directly or via groups or combined roles.

Changing the role name

You do not like the role name restricted-access or you do have some kind of naming conventions in place? You can change the role name globally by configuring the provider.

Wildfly-based distro
via CLI:
/subsystem=keycloak-server/spi=restrict-client-auth-access-provider:add(default-provider=client-role)
/subsystem=keycloak-server/spi=restrict-client-auth-access-provider/provider=client-role:add(properties={clientRoleName=my-custom-role-name,enabled=true})
via standalone.xml:
<spi name="restrict-client-auth-access-provider">
    <default-provider>client-role</default-provider>
    <provider name="client-role" enabled="true">
        <properties>
            <property name="clientRoleName" value="my-custom-role-name"/>
        </properties>
    </provider>
</spi>

For details, please refer to Manage Subsystem Configuration section in the server installation guide.

Keycloak.X distro:
spi.restrict-client-auth-access-provider.client-role.enabled=true
spi.restrict-client-auth-access-provider.client-role.client-role-name=custom-role

For details on SPI and provider configuration in Keycloak.X, please refer to SPIs and Provider Configuration section in the community guide.

Policy-based mode

⚠️ OIDC only:

Policy-based mode only works with OIDC clients (Client Protocol must be openid-connect)

  1. Configure the authenticator by clicking on Actions -> Config and select policy as the Access Provider. Policy-based access restriction
  2. Configure the Access Type of the client to confidential
  3. Set Authorization Enabled to on
  4. Go to Authorization -> Resources and click Create to create a new resource Resource configuration
  5. Set the Name and Display name to Keycloak Client Resource and keep the other fields blank
  6. Save the resource Resources overview
  7. Click Create Permission to add permissions and policies (see Authorization Services Guide for details)
  8. Afterwards, no user can authenticate to this client unless permissions have been granted by configured policies.

Using a custom error message

If a user tries to log in via a browser-based flow and access gets denied by the authenticator, a custom error message can be displayed. In the flow choose the Actions button and then choose Config. You will see the following configuration screen.

Error message configuration

You can directly define a particular message or use a property, which will be used for mapping the error message. If you choose a property, the property will be looked up from your custom theme's messages*.properties files and therefore supports internationalization.

# messages.properties
restricted-access.denied=Access denied. User is missing required role 'restricted-access'
# messages_de.properties
restricted-access.denied=Zugriff verweigert. Dem Benutzer fehlt die notwendige Rolle 'restricted-access'.

If the field is left blank, default property access-denied is used. In this case you do not need a custom theme, since this property comes with Keycloak out of the box. For details on how to add custom messages to Keycloak, please refer to section Messages and Internationalization in the server developer guide.

Frequently asked questions

Does it (already) work with Keycloak.X?

On October 28th 2021 the Keycloak project announced the roadmap for the new Quarkus-based Keycloak-X distribution. According to this Keycloak 16 will be the last preview of the Quarkus distribution. As of December 2021, Keycloak 17 will make the Quarkus distribution fully supported the WildFly distribution will be deprecated. Support for the Wildfly distribution will be removed by mid 2022.

Therefore, I will focus all further development of this library towards the Quarkus-based Keycloak.X distribution. Once the Wildfly support will be removed from the Keycloak project, I will remove all support for Wildfly here as well.

Don't worry, I will ensure this library stays compatible with the Wildfly distribution as well as with Keycloak.X until then.

Does it work with Keycloak version X.Y.Z?

If you are using Keycloak version X (e.g. X.y.z), version X.b.c should be compatible. Keycloak SPIs are quite stable. So, there is a high chance this authenticator will work with other versions, too. Check the details of latest build results for an overview or simply give it a try.

Authenticator version X.b.c is compiled against Keycloak version X.y.z. For example, version 12.3.1 will be compiled against Keycloak version 12.y.z.

I do not guarantee what version a.b or y.z will be. Neither do I backport features to older version, nor maintain any older versions of this authenticator. If you need the latest features or bugfixes for an older version, please fork this project or update your Keycloak instance. I recommend doing the latter on regular basis anyways.

Why not use "Allow/Deny Access" authenticators with conditions?

With Keycloak 13 two new authenticators have been added, namely Allow Access and Deny Access. Together with Condition - User Role authenticator authentication may be restricted in a similar way with out-of-the-box features. So, the question is why not use that and override authentication flows on a per client basis?

Here are some reasons/thoughts

  • It is not really flexible. Since Condition - User Role only allows for checking one concrete (realm or client-specific) role, a very complex flow handling all clients, or a totally separate flow for each individual client would be needed.
  • It simply does not work well with federated authentication (ie. identity provider redirects), since there is no way to configure client specific behaviour for First login flow or Post login flows. In other words, there is no feature like Authentication flow overrides at an IdP level. Hence, the same flow will be used for all clients. As said before, this becomes very complicated.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].