use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class LdapEntryManager method exportEntry.
@Override
public List<AttributeData> exportEntry(String dn) {
try {
SearchResultEntry searchResultEntry = getOperationService().lookup(dn, (String[]) null);
List<AttributeData> result = getAttributeDataList(searchResultEntry);
if (result != null) {
return result;
}
return null;
} catch (ConnectionException | SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
}
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class LdapEntryManager method persist.
@Override
protected void persist(String dn, String[] objectClasses, List<AttributeData> attributes, Integer expiration) {
List<Attribute> ldapAttributes = new ArrayList<Attribute>(attributes.size());
for (AttributeData attribute : attributes) {
String attributeName = attribute.getName();
String[] attributeValues = attribute.getStringValues();
if (ArrayHelper.isNotEmpty(attributeValues) && StringHelper.isNotEmpty(attributeValues[0])) {
if (getOperationService().isCertificateAttribute(attributeName)) {
byte[][] binaryValues = toBinaryValues(attributeValues);
ldapAttributes.add(new Attribute(attributeName + ";binary", binaryValues));
} else {
ldapAttributes.add(new Attribute(attributeName, attributeValues));
}
}
}
// Persist entry
try {
boolean result = getOperationService().addEntry(dn, ldapAttributes);
if (!result) {
throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn));
}
} catch (ConnectionException ex) {
throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex.getCause());
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to persist entry: %s", dn), ex);
}
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class LdapOperationServiceImpl method authenticateImpl.
private boolean authenticateImpl(final String bindDn, final String password) throws LDAPException, ConnectionException, SearchException {
Instant startTime = OperationDurationUtil.instance().now();
boolean result = false;
// Try to authenticate if the password was encrypted with additional mechanism
List<PasswordEncryptionMethod> additionalPasswordMethods = this.connectionProvider.getAdditionalPasswordMethods();
if ((persistenceExtension != null) || !additionalPasswordMethods.isEmpty()) {
SearchResultEntry searchResult = lookup(bindDn, USER_PASSWORD);
if (searchResult == null) {
throw new ConnectionException("Failed to find use by dn");
}
String userPassword = searchResult.getAttribute(USER_PASSWORD).getValue();
if (userPassword != null) {
if (persistenceExtension != null) {
result = persistenceExtension.compareHashedPasswords(password, userPassword);
} else {
PasswordEncryptionMethod storedPasswordMethod = PasswordEncryptionHelper.findAlgorithm(userPassword);
if (additionalPasswordMethods.contains(storedPasswordMethod)) {
LOG.debug("Authenticating '{}' using internal authentication mechanism '{}'", bindDn, storedPasswordMethod);
result = PasswordEncryptionHelper.compareCredentials(password, userPassword);
}
}
}
} else {
if (this.bindConnectionProvider == null) {
result = authenticateConnectionPoolImpl(bindDn, password);
} else {
result = authenticateBindConnectionPoolImpl(bindDn, password);
}
}
Duration duration = OperationDurationUtil.instance().duration(startTime);
OperationDurationUtil.instance().logDebug("LDAP operation: bind, duration: {}, dn: {}", duration, bindDn);
return result;
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class LdapOperationServiceImpl method deleteRecursivelyImpl.
protected boolean deleteRecursivelyImpl(String dn) {
try {
final DeleteRequest deleteRequest = new DeleteRequest(dn);
deleteRequest.addControl(new SubtreeDeleteRequestControl());
LDAPResult result = getConnectionPool().delete(deleteRequest);
return ResultCode.SUCCESS.equals(result.getResultCode());
} catch (Exception ex) {
throw new ConnectionException("Failed to delete entry", ex);
}
}
use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.
the class LdapEntryManager method authenticate.
@Override
public <T> boolean authenticate(String baseDN, Class<T> entryClass, String userName, String password) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to count entries is null");
}
// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
// Find entries
Filter searchFilter = Filter.createEqualityFilter(LdapOperationService.UID, userName);
if (objectClasses.length > 0) {
searchFilter = addObjectClassFilter(searchFilter, objectClasses);
}
SearchScope scope = SearchScope.SUB;
try {
SearchResult searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), null, 0, 1, 1, null, LdapOperationService.UID_ARRAY);
if ((searchResult == null) || (searchResult.getEntryCount() != 1)) {
return false;
}
String bindDn = searchResult.getSearchEntries().get(0).getDN();
return getOperationService().authenticate(bindDn, password, null);
} catch (ConnectionException ex) {
throw new AuthenticationException(String.format("Failed to authenticate user: %s", userName), ex);
} catch (SearchScopeException ex) {
throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
} catch (SearchException ex) {
throw new AuthenticationException(String.format("Failed to find user DN: %s", userName), ex);
}
}
Aggregations