Search in sources :

Example 76 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project polymap4-core by Polymap4.

the class GetRole method scanRole.

/**
 * The Method scanRole search with ldap for the users domain roles
 *
 * @param userName userPrincipalName of the loginuser
 */
public void scanRole(String userName) {
    String principal = "yourPrincipal";
    String credentials = "yourCredential";
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://yourServer:yourPort/yourDomain");
    env.put(Context.SECURITY_PRINCIPAL, principal);
    env.put(Context.SECURITY_CREDENTIALS, credentials);
    try {
        // Erstellt eine Initial Directory Context
        DirContext ctx = new InitialDirContext(env);
        Attributes attr = new BasicAttributes("userPrincipalName", userName);
        NamingEnumeration userData = ctx.search("cn=Users", attr);
        while (userData.hasMoreElements()) {
            SearchResult sr = (SearchResult) userData.next();
            sr.getAttributes();
            Attributes userAttributes = sr.getAttributes();
            Attribute at = userAttributes.get("memberOf");
            if (at != null) {
                role = at.getAll();
            } else {
                role = null;
            }
        }
    } catch (NamingException e) {
        e.printStackTrace();
        role = null;
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) BasicAttributes(javax.naming.directory.BasicAttributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 77 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project alien4cloud by alien4cloud.

the class AbstractLdapTest method createUserList.

public List<User> createUserList(int userCount) throws NamingException {
    List<User> userList = com.google.common.collect.Lists.newArrayList();
    for (int i = 0; i < userCount; i++) {
        Attributes attrUser = new BasicAttributes();
        attrUser.put(userIdKey, "id_" + i);
        attrUser.put(userFirstNameKey, "firstName_" + i);
        attrUser.put(userLastNameKey, "lastName_" + i);
        attrUser.put(userEmailKey, "lastName_" + i + "@test.com");
        attrUser.put(userActiveKey, userActiveValue);
        User user = attributeMapper.mapFromAttributes(attrUser);
        assertUserMapper(attrUser, user);
        userList.add(user);
    }
    return userList;
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) User(alien4cloud.security.model.User) Attributes(javax.naming.directory.Attributes) BasicAttributes(javax.naming.directory.BasicAttributes)

Example 78 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project teiid by teiid.

the class LDAPUpdateExecution method executeInsert.

// Private method to actually do an insert operation.  Per JNDI doc at
// http://java.sun.com/products/jndi/tutorial/ldap/models/operations.html, JNDI method to add new entry to LDAP that does not contain a Java object is
// DirContext.createSubContext(), so that is what is used here.
// 
// The insert must include an element named "DN" (case insensitive)
// which will be the fully qualified LDAP distinguished name of the
// entry to add.
// 
// Also, while we make no effort to prevent insert operations that
// break these rules, the underlying LDAP operation will fail (and
// pass back an explanatory message, which we will return in a
// ConnectorException, in the following cases:
// -if the parent context for this entry does not exist in the directory
// -if the insert does not specify values for all required attributes
// of the class.  Since objectClass is required for all LDAP entries,
// if it is not specified this condition will apply - and once it is
// specified then all of the other required attributes for that
// objectClass will of course also be required.
// 
// TODO - maybe automatically specify objectClass based off of
// Name/NameInSource RESTRICT property settings, like with read support
private void executeInsert() throws TranslatorException {
    List<ColumnReference> insertElementList = ((Insert) command).getColumns();
    List<Expression> insertValueList = ((ExpressionValueSource) ((Insert) command).getValueSource()).getValues();
    // create a new attribute list with case ignored in attribute
    // names
    Attributes insertAttrs = new BasicAttributes(true);
    String distinguishedName = null;
    // input).
    for (int i = 0; i < insertElementList.size(); i++) {
        ColumnReference insertElement = insertElementList.get(i);
        // call utility class to get NameInSource/Name of element
        String nameInsertElement = getNameFromElement(insertElement);
        // special handling for DN attribute - use it to set
        // distinguishedName value.
        Expression literal = insertValueList.get(i);
        if (nameInsertElement.toUpperCase().equals("DN")) {
            // $NON-NLS-1$
            Object insertValue = ((Literal) literal).getValue();
            if (insertValue == null) {
                // $NON-NLS-1$
                final String msg = LDAPPlugin.Util.getString("LDAPUpdateExecution.columnSourceNameDNNullError");
                throw new TranslatorException(msg);
            }
            if (!(insertValue instanceof java.lang.String)) {
                // $NON-NLS-1$
                final String msg = LDAPPlugin.Util.getString("LDAPUpdateExecution.columnSourceNameDNTypeError");
                throw new TranslatorException(msg);
            }
            distinguishedName = (String) insertValue;
        } else // for other attributes specified in the insert command,
        // create a new
        {
            Attribute insertAttr = createBasicAttribute(nameInsertElement, literal, insertElement.getMetadataObject());
            insertAttrs.put(insertAttr);
        }
    }
    // the LDAP add operation, so throw an exception
    if (distinguishedName == null) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("LDAPUpdateExecution.noInsertSourceNameDNError");
        throw new TranslatorException(msg);
    }
    // we'll return in a ConnectorException
    try {
        ldapCtx.createSubcontext(distinguishedName, insertAttrs);
    } catch (NamingException ne) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("LDAPUpdateExecution.insertFailed", distinguishedName, ne.getExplanation());
        throw new TranslatorException(ne, msg);
    } catch (Exception e) {
        // $NON-NLS-1$
        final String msg = LDAPPlugin.Util.getString("LDAPUpdateExecution.insertFailedUnexpected", distinguishedName);
        throw new TranslatorException(e, msg);
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) TranslatorException(org.teiid.translator.TranslatorException) NamingException(javax.naming.NamingException)

Example 79 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project teiid by teiid.

the class TestLDAPDirectQueryExecution method testCreate.

@Test
public void testCreate() throws Exception {
    String input = "exec native('create;uid=doe,ou=people,o=teiid.org;attributes=one,two,three', 'one', 2, 3.0)";
    TranslationUtility util = FakeTranslationFactory.getInstance().getExampleTranslationUtility();
    Command command = util.parseCommand(input);
    ExecutionContext ec = Mockito.mock(ExecutionContext.class);
    RuntimeMetadata rm = Mockito.mock(RuntimeMetadata.class);
    LdapContext connection = Mockito.mock(LdapContext.class);
    LdapContext ctx = Mockito.mock(LdapContext.class);
    Mockito.stub(connection.lookup("")).toReturn(ctx);
    LDAPDirectCreateUpdateDeleteQueryExecution execution = (LDAPDirectCreateUpdateDeleteQueryExecution) TRANSLATOR.createExecution(command, ec, rm, connection);
    execution.execute();
    ArgumentCaptor<String> nameArgument = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<BasicAttributes> createItemArgument = ArgumentCaptor.forClass(BasicAttributes.class);
    Mockito.verify(ctx).createSubcontext(nameArgument.capture(), createItemArgument.capture());
    assertEquals("uid=doe,ou=people,o=teiid.org", nameArgument.getValue());
    assertEquals("one", createItemArgument.getValue().get("one").getID());
    assertEquals("one", createItemArgument.getValue().get("one").get());
    assertEquals("two", createItemArgument.getValue().get("two").getID());
    assertEquals("2", createItemArgument.getValue().get("two").get());
    assertEquals("three", createItemArgument.getValue().get("three").getID());
    assertEquals("3.0", createItemArgument.getValue().get("three").get());
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) TranslationUtility(org.teiid.cdk.api.TranslationUtility) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 80 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project wildfly by wildfly.

the class OtpSaslTestCase method assertSequenceAndHash.

/**
 * Check correct user attribute values in the LDAP when using OTP algorithm.
 */
private void assertSequenceAndHash(Integer expectedSequence, byte[] expectedHash) throws NamingException {
    final Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, LDAP_URL);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    final LdapContext ctx = new InitialLdapContext(env, null);
    NamingEnumeration<?> namingEnum = ctx.search("dc=wildfly,dc=org", new BasicAttributes("cn", "jduke"));
    if (namingEnum.hasMore()) {
        SearchResult sr = (SearchResult) namingEnum.next();
        Attributes attrs = sr.getAttributes();
        assertEquals("Unexpected sequence number in LDAP attribute", expectedSequence, new Integer(attrs.get("telephoneNumber").get().toString()));
        assertEquals("Unexpected hash value in LDAP attribute", Base64.getEncoder().encodeToString(expectedHash), attrs.get("title").get().toString());
    } else {
        fail("User not found in LDAP");
    }
    namingEnum.close();
    ctx.close();
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) InitialLdapContext(javax.naming.ldap.InitialLdapContext) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) Properties(java.util.Properties) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Aggregations

BasicAttributes (javax.naming.directory.BasicAttributes)100 Attributes (javax.naming.directory.Attributes)62 BasicAttribute (javax.naming.directory.BasicAttribute)57 Attribute (javax.naming.directory.Attribute)44 Test (org.junit.Test)22 SearchResult (javax.naming.directory.SearchResult)21 DirContext (javax.naming.directory.DirContext)18 NamingException (javax.naming.NamingException)15 InitialDirContext (javax.naming.directory.InitialDirContext)14 NamingEnumeration (javax.naming.NamingEnumeration)12 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 LdapContext (javax.naming.ldap.LdapContext)9 HashMap (java.util.HashMap)8 InitialLdapContext (javax.naming.ldap.InitialLdapContext)8 File (java.io.File)7 Map (java.util.Map)7 MutablePartitionConfiguration (org.apache.directory.server.core.configuration.MutablePartitionConfiguration)7 AbstractBootstrapSchema (org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema)7 IOException (java.io.IOException)6