Java Code Examples for javax.naming.ldap.LdapName#toString()

The following examples show how to use javax.naming.ldap.LdapName#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadOnlyLDAPUserStoreManager.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * @param userName
 * @return
 * @throws UserStoreException
 */
protected String getNameInSpaceForUserName(String userName) throws UserStoreException {

    // check the cache first
    LdapName ldn = null;
    if (userName != null) {
        ldn = getFromUserCache(userName);
    } else {
        throw new UserStoreException("userName value is null.");
    }
    if (ldn != null) {
        return ldn.toString();
    }

    return getNameInSpaceForUsernameFromLDAP(userName);
}
 
Example 2
Source File: LDAPAuthorizationBackend2.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
private String getRoleFromEntry(final Connection ldapConnection, final LdapName ldapName, final String role) {

        if (ldapName == null || Strings.isNullOrEmpty(role)) {
            return null;
        }

        if("dn".equalsIgnoreCase(role)) {
            return ldapName.toString();
        }

        try {
            final LdapEntry roleEntry = LdapHelper.lookup(ldapConnection, ldapName.toString());

            if(roleEntry != null) {
                final LdapAttribute roleAttribute = roleEntry.getAttribute(role);
                if(roleAttribute != null) {
                    return Utils.getSingleStringValue(roleAttribute);
                }
            }
        } catch (LdapException e) {
            log.error("Unable to handle role {} because of ",ldapName, e.toString(), e);
        }

        return null;
    }
 
Example 3
Source File: LDAPAuthorizationBackend.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
private String getRoleFromEntry(final Connection ldapConnection, final LdapName ldapName, final String role) {

        if (ldapName == null || Strings.isNullOrEmpty(role)) {
            return null;
        }

        if("dn".equalsIgnoreCase(role)) {
            return ldapName.toString();
        }

        try {
            final LdapEntry roleEntry = LdapHelper.lookup(ldapConnection, ldapName.toString());

            if(roleEntry != null) {
                final LdapAttribute roleAttribute = roleEntry.getAttribute(role);
                if(roleAttribute != null) {
                    return Utils.getSingleStringValue(roleAttribute);
                }
            }
        } catch (LdapException e) {
            log.error("Unable to handle role {} because of ",ldapName, e.toString(), e);
        }

        return null;
    }
 
Example 4
Source File: LdapManager.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a properly encoded URL for use as the PROVIDER_URL.
 * If the encoding fails then the URL will contain the raw base dn.
 *
 * @param baseDN the base dn to use in the URL.
 * @return the properly encoded URL for use in as PROVIDER_URL.
 */
String getProviderURL(LdapName baseDN) throws NamingException
{
    StringBuffer ldapURL = new StringBuffer();

    try
    {
        for ( String host : hosts )
        {
            // Create a correctly-encoded ldap URL for the PROVIDER_URL
            final URI uri = new URI(sslEnabled ? "ldaps" : "ldap", null, host, port, "/" + baseDN.toString(), null, null);
            ldapURL.append(uri.toASCIIString());
            ldapURL.append(" ");
        }
        return ldapURL.toString().trim();
    }
    catch ( Exception e )
    {
        Log.error( "Unable to generate provider URL for baseDN: '{}'.", baseDN, e );
        throw new NamingException( "Unable to generate provider URL for baseDN: '"+baseDN+"': " + e.getMessage() );
    }
}
 
Example 5
Source File: ReadOnlyLDAPUserStoreManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This method escapes the special characters in a LdapName
 * according to the ldap filter escaping standards
 * @param ldn
 * @return
 */
private String escapeLdapNameForFilter(LdapName ldn){

    if (ldn == null) {
        if (log.isDebugEnabled()) {
            log.debug("Received null value to escape special characters. Returning null");
        }
        return null;
    }

    boolean replaceEscapeCharacters = true;

    String replaceEscapeCharactersAtUserLoginString = realmConfig
            .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_REPLACE_ESCAPE_CHARACTERS_AT_USER_LOGIN);

    if (replaceEscapeCharactersAtUserLoginString != null) {
        replaceEscapeCharacters = Boolean
                .parseBoolean(replaceEscapeCharactersAtUserLoginString);
        if (log.isDebugEnabled()) {
            log.debug("Replace escape characters configured to: "
                    + replaceEscapeCharactersAtUserLoginString);
        }
    }

    if (replaceEscapeCharacters) {
        String escapedDN = "";
        for (int i = ldn.size()-1; i > -1; i--) { //escaping the rdns separately and re-constructing the DN
            escapedDN = escapedDN + escapeSpecialCharactersForFilterWithStarAsRegex(ldn.get(i));
            if (i != 0) {
                escapedDN += ",";
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Escaped DN value for filter : " + escapedDN);
        }
        return escapedDN;
    } else {
        return ldn.toString();
    }
}
 
Example 6
Source File: DirContextAdapter.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public String getNameInNamespace() {
       if(base.size() == 0) {
           return dn.toString();
       }

       try {
           LdapName result = (LdapName) dn.clone();
           result.addAll(0, base);
           return result.toString();
       } catch (InvalidNameException e) {
           throw new org.springframework.ldap.InvalidNameException(e);
       }
}
 
Example 7
Source File: ReadOnlyLDAPUserStoreManager.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
public boolean doCheckExistingUser(String userName) throws UserStoreException {

        if (log.isDebugEnabled()) {
            log.debug("Searching for user " + userName);
        }

        if (userName == null) {
            return false;
        }

        boolean bFound = false;
        String userSearchFilter = realmConfig.getUserStoreProperty(LDAPConstants.USER_NAME_SEARCH_FILTER);
        userSearchFilter = userSearchFilter.replace("?", escapeSpecialCharactersForFilter(userName));
        try {
            String searchBase = null;
            String userDN = null;
            LdapName ldn = getFromUserCache(userName);
            if(ldn == null){
                String userDNPattern = realmConfig.getUserStoreProperty(LDAPConstants.USER_DN_PATTERN);
                if (userDNPattern != null && userDNPattern.trim().length() > 0) {
                    String[] patterns = userDNPattern.split("#");
                    for (String pattern : patterns) {
                        searchBase = MessageFormat.format(pattern, escapeSpecialCharactersForDN(userName));
                        userDN = getNameInSpaceForUserName(userName, searchBase, userSearchFilter);
                        if (userDN != null && userDN.length() > 0) {
                            bFound = true;
                            LdapName ldapName = new LdapName(userDN);
                            putToUserCache(userName, ldapName);
                            break;
                        }
                    }
                }
            } else {
                userDN = ldn.toString();
                searchBase = MessageFormat.format(userDN, escapeSpecialCharactersForDN(userName));
                userDN = getNameInSpaceForUserName(userName, searchBase, userSearchFilter);
                if (userDN != null && userDN.length() > 0) {
                    bFound = true;
                } else {
                    removeFromUserCache(userName);
                }
            }
            if(!bFound){
                searchBase = realmConfig.getUserStoreProperty(LDAPConstants.USER_SEARCH_BASE);
                userDN = getNameInSpaceForUserName(userName, searchBase, userSearchFilter);
                if(userDN != null && userDN.length() > 0){
                    bFound = true;
                }
            }
        } catch (Exception e) {
            String errorMessage = "Error occurred while checking existence of user : " + userName;
            if (log.isDebugEnabled()) {
                log.debug(errorMessage, e);
            }
            throw new UserStoreException(errorMessage, e);
        }
        if (log.isDebugEnabled()) {
            log.debug("User: " + userName + " exist: " + bFound);
        }
        return bFound;
    }