Search in sources :

Example 6 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.

the class LDAPBackingEngine method listRoles.

@Override
public List<RolePrincipal> listRoles(Principal principal) {
    try {
        String[] userAndNameSpace = cache.getUserDnAndNamespace(principal.getName());
        if (userAndNameSpace == null || userAndNameSpace.length < 2)
            return Collections.emptyList();
        ArrayList<RolePrincipal> roles = new ArrayList<>();
        for (String role : cache.getUserRoles(principal.getName(), userAndNameSpace[0], userAndNameSpace[1])) {
            roles.add(new RolePrincipal(role));
        }
        return roles;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) NamingException(javax.naming.NamingException)

Example 7 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.

the class SyncopeBackingEngine method listRoles.

public List<RolePrincipal> listRoles(Principal principal) {
    List<RolePrincipal> roles = new ArrayList<>();
    HttpGet request = new HttpGet(address + "/users?username=" + principal.getName());
    request.setHeader("Content-Type", "application/xml");
    try {
        HttpResponse response = client.execute(request);
        String responseTO = EntityUtils.toString(response.getEntity());
        if (responseTO != null && !responseTO.isEmpty()) {
            int index = responseTO.indexOf("<roleName>");
            while (index != 1) {
                responseTO = responseTO.substring(index + "<roleName>".length());
                int end = responseTO.indexOf("</roleName>");
                if (end == -1) {
                    index = -1;
                    break;
                }
                String role = responseTO.substring(0, end);
                roles.add(new RolePrincipal(role));
                responseTO = responseTO.substring(end + "</roleName>".length());
                index = responseTO.indexOf("<roleName>");
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error listing roles", e);
    }
    return roles;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 8 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.

the class PropertiesBackingEngine method listRoles.

private List<RolePrincipal> listRoles(String name) {
    List<RolePrincipal> result = new ArrayList<>();
    String userInfo = users.get(name);
    String[] infos = userInfo.split(",");
    for (int i = 1; i < infos.length; i++) {
        String roleName = infos[i];
        if (roleName.startsWith(GROUP_PREFIX)) {
            for (RolePrincipal rp : listRoles(roleName)) {
                if (!result.contains(rp)) {
                    result.add(rp);
                }
            }
        } else {
            RolePrincipal rp = new RolePrincipal(roleName);
            if (!result.contains(rp)) {
                result.add(rp);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 9 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.

the class PublickeyBackingEngine method listRoles.

private List<RolePrincipal> listRoles(String name) {
    List<RolePrincipal> result = new ArrayList<>();
    String userInfo = users.get(name);
    String[] infos = userInfo.split(",");
    for (int i = 1; i < infos.length; i++) {
        String roleName = infos[i];
        if (roleName.startsWith(GROUP_PREFIX)) {
            for (RolePrincipal rp : listRoles(roleName)) {
                if (!result.contains(rp)) {
                    result.add(rp);
                }
            }
        } else {
            RolePrincipal rp = new RolePrincipal(roleName);
            if (!result.contains(rp)) {
                result.add(rp);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 10 with RolePrincipal

use of org.apache.karaf.jaas.boot.principal.RolePrincipal in project karaf by apache.

the class PublickeyBackingEngine method addRole.

@Override
public void addRole(String username, String role) {
    String userInfos = users.get(username);
    if (userInfos != null) {
        for (RolePrincipal rp : listRoles(username)) {
            if (role.equals(rp.getName())) {
                return;
            }
        }
        String newUserInfos = userInfos + "," + role;
        users.put(username, newUserInfos);
    }
    try {
        users.save();
    } catch (Exception ex) {
        LOGGER.error("Cannot update users file,", ex);
    }
}
Also used : RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Aggregations

RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)61 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)20 Subject (javax.security.auth.Subject)19 Principal (java.security.Principal)15 Test (org.junit.Test)15 LoginException (javax.security.auth.login.LoginException)14 IOException (java.io.IOException)13 NameCallback (javax.security.auth.callback.NameCallback)13 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)13 ArrayList (java.util.ArrayList)12 Callback (javax.security.auth.callback.Callback)11 PasswordCallback (javax.security.auth.callback.PasswordCallback)10 FailedLoginException (javax.security.auth.login.FailedLoginException)10 GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)9 BundleContext (org.osgi.framework.BundleContext)8 Hashtable (java.util.Hashtable)7 HashSet (java.util.HashSet)6 File (java.io.File)4 Configuration (org.osgi.service.cm.Configuration)4 Attribute (ddf.security.assertion.Attribute)3