Search in sources :

Example 1 with LdapProfile

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);
}
Also used : LdapProfile(org.pac4j.ldap.profile.LdapProfile) AuthenticationResponse(org.ldaptive.auth.AuthenticationResponse) AuthenticationRequest(org.ldaptive.auth.AuthenticationRequest)

Example 2 with LdapProfile

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));
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) LdapProfile(org.pac4j.ldap.profile.LdapProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 3 with LdapProfile

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));
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) Collection(java.util.Collection) LdapProfile(org.pac4j.ldap.profile.LdapProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 4 with LdapProfile

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());
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) LdapProfile(org.pac4j.ldap.profile.LdapProfile) Map(java.util.Map) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 5 with LdapProfile

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());
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) LdapProfile(org.pac4j.ldap.profile.LdapProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

LdapProfile (org.pac4j.ldap.profile.LdapProfile)5 Test (org.junit.Test)4 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)4 CommonProfile (org.pac4j.core.profile.CommonProfile)4 Collection (java.util.Collection)1 Map (java.util.Map)1 AuthenticationRequest (org.ldaptive.auth.AuthenticationRequest)1 AuthenticationResponse (org.ldaptive.auth.AuthenticationResponse)1