use of org.pac4j.ldap.profile.LdapProfile in project pac4j by pac4j.
the class LdapProfileService method validate.
@Override
public void validate(final UsernamePasswordCredentials credentials, final WebContext context) {
init();
final String username = credentials.getUsername();
CommonHelper.assertNotBlank(Pac4jConstants.USERNAME, username);
final AuthenticationResponse response;
try {
logger.debug("Attempting LDAP authentication for: {}", credentials);
final List<String> attributesToRead = defineAttributesToRead();
final AuthenticationRequest request = new AuthenticationRequest(username, new Credential(credentials.getPassword()), attributesToRead.toArray(new String[attributesToRead.size()]));
response = this.ldapAuthenticator.authenticate(request);
} catch (final LdapException e) {
throw new TechnicalException("Unexpected LDAP error", e);
}
logger.debug("LDAP response: {}", response);
if (response.getResult()) {
final LdapEntry entry = response.getLdapEntry();
final List<Map<String, Object>> listAttributes = new ArrayList<>();
listAttributes.add(getAttributesFromEntry(entry));
final LdapProfile profile = convertAttributesToProfile(listAttributes, username);
credentials.setUserProfile(profile);
return;
}
if (AuthenticationResultCode.DN_RESOLUTION_FAILURE == response.getAuthenticationResultCode()) {
throw new AccountNotFoundException(username + " not found");
}
throw new BadCredentialsException("Invalid credentials for: " + username);
}
use of org.pac4j.ldap.profile.LdapProfile in project pac4j by pac4j.
the class LdapProfileServiceTests method authentSuccessSingleAttribute.
@Test
public void authentSuccessSingleAttribute() {
final LdapProfileService ldapProfileService = new LdapProfileService(connectionFactory, authenticator, LdapServer.SN, LdapServer.BASE_PEOPLE_DN);
ldapProfileService.setUsernameAttribute(LdapServer.CN);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
ldapProfileService.validate(credentials, null);
final CommonProfile profile = credentials.getUserProfile();
assertNotNull(profile);
assertTrue(profile instanceof LdapProfile);
final LdapProfile ldapProfile = (LdapProfile) profile;
assertEquals(GOOD_USERNAME, ldapProfile.getId());
assertEquals(1, ldapProfile.getAttributes().size());
assertEquals(FIRSTNAME_VALUE, ldapProfile.getAttribute(LdapServer.SN));
}
use of org.pac4j.ldap.profile.LdapProfile in project pac4j by pac4j.
the class LdapProfileServiceTests method authentSuccessMultiAttribute.
@Test
public void authentSuccessMultiAttribute() {
final LdapProfileService ldapProfileService = new LdapProfileService(connectionFactory, authenticator, LdapServer.SN + "," + LdapServer.ROLE, LdapServer.BASE_PEOPLE_DN);
ldapProfileService.setUsernameAttribute(LdapServer.CN);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME2, PASSWORD);
ldapProfileService.validate(credentials, null);
final CommonProfile profile = credentials.getUserProfile();
assertNotNull(profile);
assertTrue(profile instanceof LdapProfile);
final LdapProfile ldapProfile = (LdapProfile) profile;
assertEquals(GOOD_USERNAME2, ldapProfile.getId());
assertEquals(1, ldapProfile.getAttributes().size());
assertNull(ldapProfile.getAttribute(LdapServer.SN));
final Collection<String> attributes = (Collection<String>) ldapProfile.getAttribute(LdapServer.ROLE);
assertEquals(2, attributes.size());
assertTrue(attributes.contains(LdapServer.ROLE1));
assertTrue(attributes.contains(LdapServer.ROLE2));
}
use of org.pac4j.ldap.profile.LdapProfile in project pac4j by pac4j.
the class LdapProfileServiceTests method testCreateUpdateFindDelete.
@Test
public void testCreateUpdateFindDelete() {
final LdapProfile profile = new LdapProfile();
profile.setId(LDAP_ID);
profile.setLinkedId(LDAP_LINKED_ID);
profile.addAttribute(USERNAME, LDAP_USER);
final LdapProfileService ldapProfileService = new LdapProfileService(connectionFactory, authenticator, LdapServer.BASE_PEOPLE_DN);
ldapProfileService.setIdAttribute(LdapServer.CN);
ldapProfileService.setUsernameAttribute(LdapServer.SN);
ldapProfileService.setPasswordAttribute("userPassword");
// create
ldapProfileService.create(profile, LDAP_PASS);
// check credentials
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(LDAP_ID, LDAP_PASS);
ldapProfileService.validate(credentials, null);
final CommonProfile profile1 = credentials.getUserProfile();
assertNotNull(profile1);
// check data
final List<Map<String, Object>> results = getData(ldapProfileService, LDAP_ID);
assertEquals(1, results.size());
final Map<String, Object> result = results.get(0);
assertEquals(4, result.size());
assertEquals(LDAP_ID, result.get(LdapServer.CN));
assertEquals(LDAP_LINKED_ID, result.get(AbstractProfileService.LINKEDID));
assertNotNull(result.get(AbstractProfileService.SERIALIZED_PROFILE));
assertEquals(LDAP_USER, result.get(LdapServer.SN));
// findById
final LdapProfile profile2 = ldapProfileService.findById(LDAP_ID);
assertEquals(LDAP_ID, profile2.getId());
assertEquals(LDAP_LINKED_ID, profile2.getLinkedId());
assertEquals(LDAP_USER, profile2.getUsername());
assertEquals(1, profile2.getAttributes().size());
// update
profile.addAttribute(USERNAME, LDAP_USER2);
ldapProfileService.update(profile, LDAP_PASS2);
final List<Map<String, Object>> results2 = getData(ldapProfileService, LDAP_ID);
assertEquals(1, results2.size());
final Map<String, Object> result2 = results2.get(0);
assertEquals(4, result2.size());
assertEquals(LDAP_ID, result2.get(LdapServer.CN));
assertEquals(LDAP_LINKED_ID, result2.get(AbstractProfileService.LINKEDID));
assertNotNull(result2.get(AbstractProfileService.SERIALIZED_PROFILE));
assertEquals(LDAP_USER2, result2.get(LdapServer.SN));
// check credentials
final UsernamePasswordCredentials credentials2 = new UsernamePasswordCredentials(LDAP_ID, LDAP_PASS2);
ldapProfileService.validate(credentials2, null);
final CommonProfile profile3 = credentials.getUserProfile();
assertNotNull(profile3);
// remove
ldapProfileService.remove(profile);
final List<Map<String, Object>> results3 = getData(ldapProfileService, LDAP_ID);
assertEquals(0, results3.size());
}
use of org.pac4j.ldap.profile.LdapProfile in project pac4j by pac4j.
the class LdapProfileServiceTests method authentSuccessNoAttribute.
@Test
public void authentSuccessNoAttribute() {
final LdapProfileService ldapProfileService = new LdapProfileService(connectionFactory, authenticator, "", LdapServer.BASE_PEOPLE_DN);
ldapProfileService.setUsernameAttribute(LdapServer.CN);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
ldapProfileService.validate(credentials, null);
final CommonProfile profile = credentials.getUserProfile();
assertNotNull(profile);
assertTrue(profile instanceof LdapProfile);
final LdapProfile ldapProfile = (LdapProfile) profile;
assertEquals(GOOD_USERNAME, ldapProfile.getId());
assertEquals(0, ldapProfile.getAttributes().size());
}
Aggregations