use of org.apache.karaf.jaas.boot.principal.UserPrincipal in project karaf by apache.
the class SyncopeBackingEngine method lookupUserSyncope1.
private UserPrincipal lookupUserSyncope1(String username) {
HttpGet request = new HttpGet(address + "/users?username=" + username);
request.setHeader("Content-Type", "application/xml");
try {
HttpResponse response = client.execute(request);
String responseTO = EntityUtils.toString(response.getEntity());
if (responseTO != null && !responseTO.isEmpty()) {
return new UserPrincipal(username);
}
} catch (Exception e) {
throw new RuntimeException("Error getting user", e);
}
return null;
}
use of org.apache.karaf.jaas.boot.principal.UserPrincipal in project karaf by apache.
the class SyncopeBackingEngine method listUsersSyncope1.
private List<UserPrincipal> listUsersSyncope1() {
List<UserPrincipal> users = new ArrayList<>();
HttpGet request = new HttpGet(address + "/users");
request.setHeader("Content-Type", "application/xml");
try {
HttpResponse response = client.execute(request);
String responseTO = EntityUtils.toString(response.getEntity());
if (responseTO != null && !responseTO.isEmpty()) {
// extracting the user
int index = responseTO.indexOf("<username>");
while (index != -1) {
responseTO = responseTO.substring(index + "<username>".length());
int end = responseTO.indexOf("</username>");
if (end == -1) {
index = -1;
}
String username = responseTO.substring(0, end);
users.add(new UserPrincipal(username));
responseTO = responseTO.substring(end + "</username>".length());
index = responseTO.indexOf("<username>");
}
}
} catch (Exception e) {
throw new RuntimeException("Error listing users", e);
}
return users;
}
use of org.apache.karaf.jaas.boot.principal.UserPrincipal in project karaf by apache.
the class PublickeyLoginModule method login.
public boolean login() throws LoginException {
File f = new File(usersFile);
Properties users;
try {
users = new Properties(f);
} catch (IOException ioe) {
throw new LoginException("Unable to load user properties file " + f);
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username: ");
callbacks[1] = new PublickeyCallback();
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");
}
String user = ((NameCallback) callbacks[0]).getName();
if (user == null) {
throw new FailedLoginException("Unable to retrieve user name");
}
PublicKey key = ((PublickeyCallback) callbacks[1]).getPublicKey();
if (key == null) {
throw new FailedLoginException("Unable to retrieve public key");
}
// user infos container read from the users properties file
String userInfos = null;
try {
userInfos = users.get(user);
} catch (NullPointerException e) {
// error handled in the next statement
}
if (userInfos == null) {
if (!this.detailedLoginExcepion) {
throw new FailedLoginException("login failed");
} else {
throw new FailedLoginException("User " + user + " does not exist");
}
}
// the password is in the first position
String[] infos = userInfos.split(",");
String storedKey = infos[0];
// check the provided password
if (!equals(key, storedKey)) {
if (!this.detailedLoginExcepion) {
throw new FailedLoginException("login failed");
} else {
throw new FailedLoginException("Public key for " + user + " does not match");
}
}
principals = new HashSet<>();
principals.add(new UserPrincipal(user));
for (int i = 1; i < infos.length; i++) {
if (infos[i].trim().startsWith(BackingEngine.GROUP_PREFIX)) {
// it's a group reference
principals.add(new GroupPrincipal(infos[i].trim().substring(BackingEngine.GROUP_PREFIX.length())));
String groupInfo = users.get(infos[i].trim());
if (groupInfo != null) {
String[] roles = groupInfo.split(",");
for (int j = 1; j < roles.length; j++) {
principals.add(new RolePrincipal(roles[j].trim()));
}
}
} else {
// it's an user reference
principals.add(new RolePrincipal(infos[i].trim()));
}
}
users.clear();
if (debug) {
LOG.debug("Successfully logged in " + user);
}
succeeded = true;
return true;
}
use of org.apache.karaf.jaas.boot.principal.UserPrincipal in project karaf by apache.
the class LDAPLoginModule method doLogin.
protected boolean doLogin() throws LoginException {
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username: ");
callbacks[1] = new PasswordCallback("Password: ", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException ioException) {
throw new LoginException(ioException.getMessage());
} catch (UnsupportedCallbackException unsupportedCallbackException) {
throw new LoginException(unsupportedCallbackException.getMessage() + " not available to obtain information from user.");
}
user = Util.doRFC2254Encoding(((NameCallback) callbacks[0]).getName());
char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
// If either a username or password is specified don't allow authentication = "none".
// This is to prevent someone from logging into Karaf as any user without providing a
// valid password (because if authentication = none, the password could be any
// value - it is ignored).
LDAPOptions options = new LDAPOptions(this.options);
if (options.isUsernameTrim()) {
if (user != null) {
user = user.trim();
}
}
String authentication = options.getAuthentication();
if ("none".equals(authentication) && (user != null || tmpPassword != null)) {
logger.debug("Changing from authentication = none to simple since user or password was specified.");
// default to simple so that the provided user/password will get checked
authentication = "simple";
Map<String, Object> opts = new HashMap<>(this.options);
opts.put(LDAPOptions.AUTHENTICATION, authentication);
options = new LDAPOptions(opts);
}
boolean allowEmptyPasswords = options.getAllowEmptyPasswords();
if (!"none".equals(authentication) && !allowEmptyPasswords && (tmpPassword == null || tmpPassword.length == 0)) {
throw new LoginException("Empty passwords not allowed");
}
if (tmpPassword == null) {
tmpPassword = new char[0];
}
String password = new String(tmpPassword);
principals = new HashSet<>();
LDAPCache cache = LDAPCache.getCache(options);
// step 1: get the user DN
final String[] userDnAndNamespace;
try {
logger.debug("Get the user DN.");
userDnAndNamespace = cache.getUserDnAndNamespace(user);
if (userDnAndNamespace == null) {
return false;
}
} catch (Exception e) {
logger.warn("Can't connect to the LDAP server: {}", e.getMessage(), e);
throw new LoginException("Can't connect to the LDAP server: " + e.getMessage());
}
// step 2: bind the user using the DN
DirContext context = null;
try {
// switch the credentials to the Karaf login user so that we can verify his password is correct
logger.debug("Bind user (authentication).");
Hashtable<String, Object> env = options.getEnv();
env.put(Context.SECURITY_AUTHENTICATION, authentication);
logger.debug("Set the security principal for " + userDnAndNamespace[0] + "," + options.getUserBaseDn());
env.put(Context.SECURITY_PRINCIPAL, userDnAndNamespace[0] + "," + options.getUserBaseDn());
env.put(Context.SECURITY_CREDENTIALS, password);
logger.debug("Binding the user.");
context = new InitialDirContext(env);
logger.debug("User " + user + " successfully bound.");
context.close();
} catch (Exception e) {
logger.warn("User " + user + " authentication failed.", e);
throw new LoginException("Authentication failed: " + e.getMessage());
} finally {
if (context != null) {
try {
context.close();
} catch (Exception e) {
// ignore
}
}
}
principals.add(new UserPrincipal(user));
// step 3: retrieving user roles
try {
String[] roles = cache.getUserRoles(user, userDnAndNamespace[0], userDnAndNamespace[1]);
for (String role : roles) {
principals.add(new RolePrincipal(role));
}
} catch (Exception e) {
throw new LoginException("Can't get user " + user + " roles: " + e.getMessage());
}
succeeded = true;
return true;
}
use of org.apache.karaf.jaas.boot.principal.UserPrincipal in project karaf by apache.
the class PropertiesBackingEngine method listUsers.
@Override
public List<UserPrincipal> listUsers() {
List<UserPrincipal> result = new ArrayList<>();
for (Object user : users.keySet()) {
String userName = (String) user;
if (userName.startsWith(GROUP_PREFIX))
continue;
UserPrincipal userPrincipal = new UserPrincipal(userName);
result.add(userPrincipal);
}
return result;
}
Aggregations