How can i restrict client access to only one group of users in keycloak?

后端 未结 8 748
一生所求
一生所求 2020-12-24 15:08

I have a client in keycloak for my awx(ansible tower) webpage. I need only the users from one specific keycloak group to be able to log in through

8条回答
  •  -上瘾入骨i
    2020-12-24 15:24

    I tried Allan's solution and it is working fine using Keycloak 11.0.3 but it has some cons mentioned below. Here is my solution for the authenticator script which does not grant access for users if they are not member at least one of the given groups. In such case a unique error message is shown.

    AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");
    
     function authenticate(context) {
        var allowed_groups = ['foo', 'bar'];
        var username = user ? user.username : "anonymous";
        var groups = user.getGroups();
        var group_array = groups.toArray();
        
        for (var i in group_array) {
            var gn = group_array[i].getName();
            if (allowed_groups.indexOf(gn) >= 0) {
                LOG.info("Access granted for user '" + username + "' for being member of LDAP group '" + gn + "'");
                return context.success();
            }    
        }
    
        LOG.info("Access denied for user '" + username + ". for not being member of any of the following LDAP groups: " + allowed_groups);
        context.failure(AuthenticationFlowError.IDENTITY_PROVIDER_DISABLED, context.form().setError(
            "User doesn't have the required LDAP group membership to view this page", null).createForm("error.ftl"));
        return;
     }
    

    There are two minor user experience related cons with this solution worth mentioning:

    • When a not logged in user tries to connect to a client which access gets denied by the authenticator script the whole authentication flow is considered failure. This means the user doesn't get logged in into Keycloak despite the fact they provided the correct credentials
    • When a logged in user tries to connect to a client which access gets denied by the authenticator script the Keycloak login page is displayed (without showing any error message) which is deceptive as the user can have the false feeling they are not logged in

    In addition if you maintain multiple clients and you need to have different groups (or roles) checked per client then you have to implement as many new authentication flows as many different checks you need. In short the solution works, but it has some disadvantages. I believe a simple feature such as restricting the access based on groups or roles is essential for an identity and access management system and should be supported natively!

提交回复
热议问题