use of org.springframework.ldap.InvalidNameException in project coprhd-controller by CoprHD.
the class StorageOSLdapAuthenticationHandler method doAuthenticationOverSingleServer.
private boolean doAuthenticationOverSingleServer(LdapOrADServer server, UsernamePasswordCredentials usernamePasswordCredentials) {
_log.info("Do authentication to the server {}", server.getContextSource().getUrls()[0]);
String password = usernamePasswordCredentials.getPassword();
List<String> dns = new ArrayList<String>();
final String filter = LdapFilterUtil.getPersonFilterWithValues(_rawFilter, usernamePasswordCredentials.getUserName());
_log.debug("Filter for authentication is {}", filter);
LdapTemplate ldapTemplate = new LdapTemplate(server.getContextSource());
// To avoid the exceptions due to referrals returned
ldapTemplate.setIgnorePartialResultException(true);
try {
ldapTemplate.search(new StorageOSSearchExecutor(filter), new StorageOSNameClassPairCallbackHandler(dns));
} catch (CommunicationException e) {
_log.warn("Connection to LDAP server {} failed", Arrays.toString(server.getContextSource().getUrls()));
throw e;
} catch (AuthenticationException e) {
_alertLog.error(MessageFormat.format("Manager bind failed during search for user {0} in domain(s) {1}. Check manager DN and password. {2}. " + "Note that any change to the manager DN username or password in the authentication provider must be manually changed in ViPR.", usernamePasswordCredentials.getUserName(), _domains, e.getMessage()));
throw UnauthorizedException.unauthorized.managerBindFailed();
} catch (InvalidNameException e) {
_alertLog.error(MessageFormat.format("Search failed because the search path provided is syntactically invalid for user {0}. {1}", usernamePasswordCredentials.getUserName(), e.getMessage()));
throw UnauthorizedException.unauthorized.userSearchFailed();
} catch (Exception e) {
_alertLog.error(MessageFormat.format("Search or bind failed. An exception was thrown while trying to authenticate user {0}. {1}", usernamePasswordCredentials.getUserName(), e.getMessage()));
throw UnauthorizedException.unauthorized.bindSearchGenericException();
}
if (dns.isEmpty()) {
_log.info("Search for " + filter + " returned 0 results.");
return false;
}
if (dns.size() > 1) {
_log.warn("Search for " + filter + " returned multiple results, which is not allowed.");
return false;
}
try {
DirContext test = server.getContextSource().getContext(dns.get(0), password);
if (test != null) {
try {
test.close();
} catch (NamingException e) {
_log.error("Failed to close test context", e);
}
_log.info("Authenticate user {} against server {} successfully", usernamePasswordCredentials.getUserName(), server.getContextSource().getUrls()[0]);
return true;
}
} catch (AuthenticationException e) {
_log.warn("Failed to authenticate user {}", usernamePasswordCredentials.getUserName());
return false;
} catch (CommunicationException e) {
_alertLog.error(MessageFormat.format("Connection to LDAP server {0} failed for domain(s) {1}. {2}", Arrays.toString(server.getContextSource().getUrls()), _domains, e.getMessage()));
throw e;
} catch (Exception e) {
_alertLog.error(MessageFormat.format("Second bind failed. An exception was thrown while trying to authenticate user {0}. {1}", usernamePasswordCredentials.getUserName(), e.getMessage()));
throw UnauthorizedException.unauthorized.bindSearchGenericException();
}
return false;
}
Aggregations