Search in sources :

Example 96 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project perun by CESNET.

the class ExtSourceLdapTest method getUsersSubjectsTest.

@Test
public void getUsersSubjectsTest() throws Exception {
    System.out.println("getUsersSubjectsTest");
    // define needed attributes
    String base = "cn=firstName,dc=dc";
    String usersQuery = "dc=cz";
    Map<String, String> mapOfAttributes = new HashMap<>();
    mapOfAttributes.put("usersQuery", usersQuery);
    mapOfAttributes.put("base", base);
    doReturn(mapOfAttributes).when(extSourceLdap).getAttributes();
    // mock connection and define received attributes
    DirContext dirContext = mock(DirContext.class);
    doReturn(dirContext).when(extSourceLdap).getContext();
    Attribute attribute = new BasicAttribute("firstName", "josef");
    Attribute attribute2 = new BasicAttribute("dc", "cz");
    Attributes attributes = new BasicAttributes();
    attributes.put(attribute);
    attributes.put(attribute2);
    NamingEnumeration<SearchResult> namingEnumeration = mock(NamingEnumeration.class);
    doReturn(namingEnumeration).when(dirContext).search(anyString(), anyString(), any());
    doReturn(true, false).when(namingEnumeration).hasMore();
    SearchResult searchResult = new SearchResult("name", namingEnumeration, attributes);
    doReturn(searchResult).when(namingEnumeration).next();
    // create expected subject to get
    List<Map<String, String>> expectedSubjects = new ArrayList<>();
    Map<String, String> subject = new HashMap<>();
    subject.put("cn", "josef");
    subject.put("dc", "cz");
    expectedSubjects.add(subject);
    // test the method
    List<Map<String, String>> actualSubjects = extSourceLdap.getUsersSubjects();
    assertEquals("subjects should be same", expectedSubjects, actualSubjects);
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) HashMap(java.util.HashMap) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) SearchResult(javax.naming.directory.SearchResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DirContext(javax.naming.directory.DirContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 97 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project perun by CESNET.

the class ExtSourceLdapTest method getUsersSubjectsNullQueryTest.

@Test
public void getUsersSubjectsNullQueryTest() throws Exception {
    System.out.println("getUsersSubjectsNullQueryTest");
    // define needed attributes
    String base = "cn=firstName,dc=dc";
    Map<String, String> mapOfAttributes = new HashMap<>();
    mapOfAttributes.put("usersQuery", null);
    mapOfAttributes.put("base", base);
    doReturn(mapOfAttributes).when(extSourceLdap).getAttributes();
    // mock connection and define received attributes
    DirContext dirContext = mock(DirContext.class);
    doReturn(dirContext).when(extSourceLdap).getContext();
    Attributes attributes = new BasicAttributes();
    attributes.put(new BasicAttribute("firstName", "josef"));
    attributes.put(new BasicAttribute("dc", "cz"));
    doReturn(attributes).when(dirContext).getAttributes(base);
    // create expected subject to get
    List<Map<String, String>> expectedSubjects = new ArrayList<>();
    Map<String, String> subject = new HashMap<>();
    subject.put("cn", "josef");
    subject.put("dc", "cz");
    expectedSubjects.add(subject);
    // test the method
    List<Map<String, String>> actualSubjects = extSourceLdap.getUsersSubjects();
    assertEquals("subjects should be same", expectedSubjects, actualSubjects);
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) HashMap(java.util.HashMap) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DirContext(javax.naming.directory.DirContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 98 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project spring-security by spring-projects.

the class LdapUserDetailsMapperTests method testNonRetrievedRoleAttributeIsIgnored.

/**
 * SEC-303. Non-retrieved role attribute causes NullPointerException
 */
@Test
public void testNonRetrievedRoleAttributeIsIgnored() {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setRoleAttributes(new String[] { "userRole", "nonRetrievedAttribute" });
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(new BasicAttribute("userRole", "x"));
    DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetailsImpl user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getAuthorities()).hasSize(1);
    assertThat(AuthorityUtils.authorityListToSet(user.getAuthorities())).contains("ROLE_X");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Example 99 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project spring-security by spring-projects.

the class LdapUserDetailsMapperTests method testPasswordAttributeIsMappedCorrectly.

@Test
public void testPasswordAttributeIsMappedCorrectly() {
    LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
    mapper.setPasswordAttributeName("myappsPassword");
    BasicAttributes attrs = new BasicAttributes();
    attrs.put(new BasicAttribute("myappsPassword", "mypassword".getBytes()));
    DirContextAdapter ctx = new DirContextAdapter(attrs, new DistinguishedName("cn=someName"));
    ctx.setAttributeValue("uid", "ani");
    LdapUserDetails user = (LdapUserDetailsImpl) mapper.mapUserFromContext(ctx, "ani", AuthorityUtils.NO_AUTHORITIES);
    assertThat(user.getPassword()).isEqualTo("mypassword");
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) DistinguishedName(org.springframework.ldap.core.DistinguishedName) DirContextAdapter(org.springframework.ldap.core.DirContextAdapter) Test(org.junit.jupiter.api.Test)

Example 100 with BasicAttributes

use of javax.naming.directory.BasicAttributes in project eap-additional-testsuite by jboss-set.

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