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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations