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);
}
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);
}
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");
}
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");
}
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();
}
Aggregations