Search in sources :

Example 1 with ConnectionException

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);
    }
}
Also used : SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 2 with ConnectionException

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);
    }
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) ConnectionException(io.jans.orm.exception.operation.ConnectionException) MappingException(io.jans.orm.exception.MappingException) ParseException(java.text.ParseException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException)

Example 3 with ConnectionException

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;
}
Also used : PasswordEncryptionMethod(io.jans.orm.operation.auth.PasswordEncryptionMethod) Instant(java.time.Instant) Duration(java.time.Duration) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 4 with ConnectionException

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);
    }
}
Also used : LDAPResult(com.unboundid.ldap.sdk.LDAPResult) DeleteRequest(com.unboundid.ldap.sdk.DeleteRequest) SubtreeDeleteRequestControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl) MappingException(io.jans.orm.exception.MappingException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) InvalidSimplePageControlException(io.jans.orm.ldap.exception.InvalidSimplePageControlException) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Example 5 with ConnectionException

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);
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) AuthenticationException(io.jans.orm.exception.AuthenticationException) SearchScope(io.jans.orm.model.SearchScope) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) ConnectionException(io.jans.orm.exception.operation.ConnectionException) MappingException(io.jans.orm.exception.MappingException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException)

Aggregations

ConnectionException (io.jans.orm.exception.operation.ConnectionException)10 MappingException (io.jans.orm.exception.MappingException)5 SearchException (io.jans.orm.exception.operation.SearchException)5 AuthenticationException (io.jans.orm.exception.AuthenticationException)4 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)3 KeyConversionException (io.jans.orm.exception.KeyConversionException)3 ConfigurationException (io.jans.orm.exception.operation.ConfigurationException)3 SearchScopeException (io.jans.orm.exception.operation.SearchScopeException)3 AttributeData (io.jans.orm.model.AttributeData)3 SpannerException (com.google.cloud.spanner.SpannerException)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 ResultSet (com.google.cloud.spanner.ResultSet)1 Type (com.google.cloud.spanner.Type)1 StructField (com.google.cloud.spanner.Type.StructField)1 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 Attribute (com.unboundid.ldap.sdk.Attribute)1