Search in sources :

Example 31 with NamingException

use of javax.naming.NamingException in project neo4j by neo4j.

the class LdapGroupHasUsersAuthPlugin method authenticateAndAuthorize.

@Override
public AuthInfo authenticateAndAuthorize(AuthToken authToken) throws AuthenticationException {
    try {
        String username = authToken.principal();
        char[] password = authToken.credentials();
        LdapContext ctx = authenticate(username, password);
        Set<String> roles = authorize(ctx, username);
        return AuthInfo.of(username, roles);
    } catch (NamingException e) {
        throw new AuthenticationException(e.getMessage());
    }
}
Also used : AuthenticationException(org.neo4j.server.security.enterprise.auth.plugin.api.AuthenticationException) NamingException(javax.naming.NamingException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 32 with NamingException

use of javax.naming.NamingException in project OpenAM by OpenRock.

the class DataStore method getReferralNames.

static Set<String> getReferralNames(String realm, String referredRealm) throws EntitlementException {
    try {
        String filter = "(ou=" + REFERRAL_REALMS + "=" + DNMapper.orgNameToRealmName(referredRealm) + ")";
        String baseDNString = getSearchBaseDN(realm, REFERRAL_STORE);
        if (SMSEntry.checkIfEntryExists(baseDNString, adminToken)) {
            DN baseDN = DN.valueOf(baseDNString);
            return LDAPUtils.collectNonIdenticalValues(baseDN, SMSEntry.search(adminToken, baseDNString, filter, 0, 0, false, false));
        }
        return emptySet();
    } catch (SMSException | NamingException ex) {
        throw new EntitlementException(215, ex);
    }
}
Also used : EntitlementException(com.sun.identity.entitlement.EntitlementException) SMSException(com.sun.identity.sm.SMSException) DN(org.forgerock.opendj.ldap.DN) NamingException(javax.naming.NamingException)

Example 33 with NamingException

use of javax.naming.NamingException in project OpenAM by OpenRock.

the class AMSetupServlet method getLdapHostAndPort.

// Method to get hostname and port number with the
// provided Domain Name for Active Directory user data store.
private String[] getLdapHostAndPort(String domainName) throws NamingException, IOException {
    if (!domainName.endsWith(".")) {
        domainName += '.';
    }
    DirContext ictx;
    // The resource record type A is defined in RFC 1035.
    try {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        ictx = new InitialDirContext(env);
        Attributes attributes = ictx.getAttributes(domainName, new String[] { "A" });
        Attribute attrib = attributes.get("A");
        if (attrib == null) {
            throw new NamingException();
        }
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // then look for the LDAP server
    String serverHostName;
    String serverPortStr;
    String ldapServer = "_ldap._tcp." + domainName;
    try {
        // Attempting to resolve ldapServer to SRV record.
        // This is a mechanism defined in MSDN, querying
        // SRV records for _ldap._tcp.DOMAINNAME.
        // and get host and port from domain.
        Attributes attributes = ictx.getAttributes(ldapServer, new String[] { "SRV" });
        Attribute attr = attributes.get("SRV");
        if (attr == null) {
            throw new NamingException();
        }
        String[] srv = attr.get().toString().split(" ");
        String hostNam = srv[3];
        serverHostName = hostNam.substring(0, hostNam.length() - 1);
        serverPortStr = srv[2];
    } catch (NamingException e) {
        // throw exception.
        throw e;
    }
    // try to connect to LDAP port to make sure this machine
    // has LDAP service
    int serverPort = Integer.parseInt(serverPortStr);
    try {
        new Socket(serverHostName, serverPort).close();
    } catch (IOException e) {
        throw e;
    }
    String[] hostAndPort = new String[2];
    hostAndPort[0] = serverHostName;
    hostAndPort[1] = serverPortStr;
    return hostAndPort;
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) IOException(java.io.IOException) Socket(java.net.Socket)

Example 34 with NamingException

use of javax.naming.NamingException in project OpenAM by OpenRock.

the class SMSLdapObject method copyModItemsToModifyRequest.

// Method to covert JNDI ModificationItems to LDAPModificationSet
private static ModifyRequest copyModItemsToModifyRequest(DN dn, ModificationItem[] mods) throws SMSException {
    ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(dn);
    try {
        for (ModificationItem mod : mods) {
            Attribute attribute = mod.getAttribute();
            LinkedAttribute attr = new LinkedAttribute(attribute.getID());
            for (NamingEnumeration ne = attribute.getAll(); ne.hasMore(); ) {
                attr.add(ne.next());
            }
            switch(mod.getModificationOp()) {
                case DirContext.ADD_ATTRIBUTE:
                    modifyRequest.addModification(new Modification(ModificationType.ADD, attr));
                    break;
                case DirContext.REPLACE_ATTRIBUTE:
                    modifyRequest.addModification(new Modification(ModificationType.REPLACE, attr));
                    break;
                case DirContext.REMOVE_ATTRIBUTE:
                    modifyRequest.addModification(new Modification(ModificationType.DELETE, attr));
                    break;
            }
        }
    } catch (NamingException nne) {
        throw new SMSException(nne, "sms-cannot-copy-fromModItemToModSet");
    }
    return modifyRequest;
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) Modification(org.forgerock.opendj.ldap.Modification) Attribute(javax.naming.directory.Attribute) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) SMSException(com.sun.identity.sm.SMSException) NamingEnumeration(javax.naming.NamingEnumeration) NamingException(javax.naming.NamingException) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute)

Example 35 with NamingException

use of javax.naming.NamingException in project OpenAM by OpenRock.

the class SMSEmbeddedLdapObject method copyModItemsToLDAPModList.

// Method to covert JNDI ModificationItems to LDAPModificationSet
private static List copyModItemsToLDAPModList(ModificationItem[] mods) throws SMSException {
    if ((mods == null) || (mods.length == 0)) {
        return null;
    }
    List<LDAPModification> modList = new ArrayList<>(mods.length);
    try {
        for (ModificationItem mod : mods) {
            Attribute dAttr = mod.getAttribute();
            String attrName = dAttr.getID();
            List<String> values = new ArrayList<>();
            for (NamingEnumeration ne = dAttr.getAll(); ne.hasMore(); ) {
                values.add((String) ne.next());
            }
            ModificationType modType = null;
            switch(mod.getModificationOp()) {
                case DirContext.ADD_ATTRIBUTE:
                    modType = ModificationType.ADD;
                    break;
                case DirContext.REPLACE_ATTRIBUTE:
                    modType = ModificationType.REPLACE;
                    break;
                case DirContext.REMOVE_ATTRIBUTE:
                    modType = ModificationType.DELETE;
                    break;
            }
            if (modType != null) {
                modList.add(new LDAPModification(modType, new LDAPAttribute(attrName, values)));
            }
        }
    } catch (NamingException nne) {
        throw (new SMSException(nne, "sms-cannot-copy-fromModItemToModSet"));
    }
    return (modList);
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) LDAPAttribute(org.opends.server.protocols.ldap.LDAPAttribute) Attribute(javax.naming.directory.Attribute) LDAPAttribute(org.opends.server.protocols.ldap.LDAPAttribute) ModificationType(org.forgerock.opendj.ldap.ModificationType) SMSException(com.sun.identity.sm.SMSException) LDAPModification(org.opends.server.protocols.ldap.LDAPModification) ArrayList(java.util.ArrayList) NamingEnumeration(javax.naming.NamingEnumeration) NamingException(javax.naming.NamingException)

Aggregations

NamingException (javax.naming.NamingException)1246 InitialContext (javax.naming.InitialContext)417 Context (javax.naming.Context)259 IOException (java.io.IOException)163 Attribute (javax.naming.directory.Attribute)111 DirContext (javax.naming.directory.DirContext)100 SearchResult (javax.naming.directory.SearchResult)98 ArrayList (java.util.ArrayList)95 SQLException (java.sql.SQLException)93 NameNotFoundException (javax.naming.NameNotFoundException)88 Attributes (javax.naming.directory.Attributes)85 DataSource (javax.sql.DataSource)84 Properties (java.util.Properties)77 Reference (javax.naming.Reference)77 InitialDirContext (javax.naming.directory.InitialDirContext)77 Test (org.junit.Test)75 Hashtable (java.util.Hashtable)73 SearchControls (javax.naming.directory.SearchControls)73 HashMap (java.util.HashMap)55 LdapContext (javax.naming.ldap.LdapContext)55