Search in sources :

Example 1 with GroupPrincipal

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

the class JDBCLoginModule method login.

public boolean login() throws LoginException {
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        callbackHandler.handle(callbacks);
    } catch (IOException ioe) {
        throw new LoginException(ioe.getMessage());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException(uce.getMessage() + " not available to obtain information from user");
    }
    user = ((NameCallback) callbacks[0]).getName();
    char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
    if (tmpPassword == null) {
        tmpPassword = new char[0];
    }
    String password = new String(tmpPassword);
    principals = new HashSet<>();
    try {
        DataSource datasource = JDBCUtils.createDatasource(bundleContext, datasourceURL);
        try (Connection connection = datasource.getConnection()) {
            List<String> passwords = JDBCUtils.rawSelect(connection, passwordQuery, user);
            if (passwords.isEmpty()) {
                if (!this.detailedLoginExcepion) {
                    throw new LoginException("login failed");
                } else {
                    throw new LoginException("User " + user + " does not exist");
                }
            }
            if (!checkPassword(password, passwords.get(0))) {
                if (!this.detailedLoginExcepion) {
                    throw new LoginException("login failed");
                } else {
                    throw new LoginException("Password for " + user + " does not match");
                }
            }
            principals.add(new UserPrincipal(user));
            if (roleQuery != null && !"".equals(roleQuery.trim())) {
                List<String> roles = JDBCUtils.rawSelect(connection, roleQuery, user);
                for (String role : roles) {
                    if (role.startsWith(BackingEngine.GROUP_PREFIX)) {
                        principals.add(new GroupPrincipal(role.substring(BackingEngine.GROUP_PREFIX.length())));
                        for (String r : JDBCUtils.rawSelect(connection, roleQuery, role)) {
                            principals.add(new RolePrincipal(r));
                        }
                    } else {
                        principals.add(new RolePrincipal(role));
                    }
                }
            } else {
                LOGGER.debug("No roleQuery specified so no roles have been retrieved for the authenticated user");
            }
        }
    } catch (Exception ex) {
        throw new LoginException("Error has occurred while retrieving credentials from database:" + ex.getMessage());
    }
    return true;
}
Also used : Connection(java.sql.Connection) IOException(java.io.IOException) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) DataSource(javax.sql.DataSource) GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) LoginException(javax.security.auth.login.LoginException) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

Example 2 with GroupPrincipal

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

the class ListUsersCommand method doExecute.

@Override
protected Object doExecute(BackingEngine engine) throws Exception {
    List<UserPrincipal> users = engine.listUsers();
    ShellTable table = new ShellTable();
    table.column("User Name");
    table.column("Group");
    table.column("Role");
    for (UserPrincipal user : users) {
        List<String> reportedRoles = new ArrayList<>();
        String userName = user.getName();
        for (GroupPrincipal group : engine.listGroups(user)) {
            reportedRoles.addAll(displayGroupRoles(engine, userName, group, table));
        }
        for (RolePrincipal role : engine.listRoles(user)) {
            String roleName = role.getName();
            if (reportedRoles.contains(roleName)) {
                continue;
            }
            reportedRoles.add(roleName);
            table.addRow().addContent(userName, "", roleName);
        }
        if (reportedRoles.size() == 0) {
            table.addRow().addContent(userName, "", "");
        }
    }
    table.print(System.out, !noFormat);
    return null;
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) ArrayList(java.util.ArrayList) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal)

Example 3 with GroupPrincipal

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

the class PropertiesBackingEngine method listGroups.

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

Example 4 with GroupPrincipal

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

the class PublickeyBackingEngine method listGroups.

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

Example 5 with GroupPrincipal

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

the class PublickeyBackingEngine method deleteGroup.

@Override
public void deleteGroup(String username, String group) {
    deleteRole(username, GROUP_PREFIX + group);
    // garbage collection, clean up the groups if needed
    for (UserPrincipal user : listUsers()) {
        for (GroupPrincipal g : listGroups(user)) {
            if (group.equals(g.getName())) {
                // there is another user of this group, nothing to clean up
                return;
            }
        }
    }
    // nobody is using this group any more, remote it
    deleteUser(GROUP_PREFIX + group);
}
Also used : GroupPrincipal(org.apache.karaf.jaas.boot.principal.GroupPrincipal) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal)

Aggregations

GroupPrincipal (org.apache.karaf.jaas.boot.principal.GroupPrincipal)16 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)10 RolePrincipal (org.apache.karaf.jaas.boot.principal.RolePrincipal)9 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 LoginException (javax.security.auth.login.LoginException)4 Properties (org.apache.felix.utils.properties.Properties)4 File (java.io.File)3 Callback (javax.security.auth.callback.Callback)3 NameCallback (javax.security.auth.callback.NameCallback)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 FailedLoginException (javax.security.auth.login.FailedLoginException)3 Test (org.junit.Test)3 PasswordCallback (javax.security.auth.callback.PasswordCallback)2 Field (java.lang.reflect.Field)1 PublicKey (java.security.PublicKey)1 DSAPublicKey (java.security.interfaces.DSAPublicKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 Connection (java.sql.Connection)1 Subject (javax.security.auth.Subject)1