Search in sources :

Example 51 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class DbProfileServiceTests method testCreateUpdateFindDelete.

@Test
public void testCreateUpdateFindDelete() {
    final DbProfile profile = new DbProfile();
    profile.setId("" + DB_ID);
    profile.setLinkedId(DB_LINKED_ID);
    profile.addAttribute(USERNAME, DB_USER);
    final DbProfileService dbProfileService = new DbProfileService(ds);
    dbProfileService.setPasswordEncoder(DbServer.PASSWORD_ENCODER);
    // create
    dbProfileService.create(profile, DB_PASS);
    // check credentials
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DB_USER, DB_PASS);
    dbProfileService.validate(credentials, null);
    final CommonProfile profile1 = credentials.getUserProfile();
    assertNotNull(profile1);
    // check data
    final List<Map<String, Object>> results = getData(DB_ID);
    assertEquals(1, results.size());
    final Map<String, Object> result = results.get(0);
    assertEquals(5, result.size());
    assertEquals(DB_ID, result.get(ID));
    assertEquals(DB_LINKED_ID, result.get(AbstractProfileService.LINKEDID));
    assertNotNull(result.get(AbstractProfileService.SERIALIZED_PROFILE));
    assertTrue(DbServer.PASSWORD_ENCODER.matches(DB_PASS, (String) result.get(PASSWORD)));
    assertEquals(DB_USER, result.get(USERNAME));
    // findById
    final DbProfile profile2 = dbProfileService.findById("" + DB_ID);
    assertEquals("" + DB_ID, profile2.getId());
    assertEquals(DB_LINKED_ID, profile2.getLinkedId());
    assertEquals(DB_USER, profile2.getUsername());
    assertEquals(1, profile2.getAttributes().size());
    // update
    profile.addAttribute(USERNAME, DB_USER2);
    dbProfileService.update(profile, null);
    final List<Map<String, Object>> results2 = getData(DB_ID);
    assertEquals(1, results2.size());
    final Map<String, Object> result2 = results2.get(0);
    assertEquals(5, result2.size());
    assertEquals(DB_ID, result2.get(ID));
    assertEquals(DB_LINKED_ID, result2.get(AbstractProfileService.LINKEDID));
    assertNotNull(result2.get(AbstractProfileService.SERIALIZED_PROFILE));
    assertTrue(DbServer.PASSWORD_ENCODER.matches(DB_PASS, (String) result2.get(PASSWORD)));
    assertEquals(DB_USER2, result2.get(USERNAME));
    // remove
    dbProfileService.remove(profile);
    final List<Map<String, Object>> results3 = getData(DB_ID);
    assertEquals(0, results3.size());
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) DbProfile(org.pac4j.sql.profile.DbProfile) Map(java.util.Map) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 52 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class DbProfileServiceTests method testGoodUsernameNoAttribute.

@Test
public void testGoodUsernameNoAttribute() {
    final UsernamePasswordCredentials credentials = login(GOOD_USERNAME, PASSWORD, "");
    final CommonProfile profile = credentials.getUserProfile();
    assertNotNull(profile);
    assertTrue(profile instanceof DbProfile);
    final DbProfile dbProfile = (DbProfile) profile;
    assertEquals(GOOD_USERNAME, dbProfile.getId());
    assertNull(dbProfile.getAttribute(FIRSTNAME));
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) DbProfile(org.pac4j.sql.profile.DbProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 53 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials 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)

Example 54 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class MongoProfileServiceIT method testGoodUsernameAttribute.

@Test
public void testGoodUsernameAttribute() {
    final UsernamePasswordCredentials credentials = login(GOOD_USERNAME, PASSWORD, FIRSTNAME);
    final CommonProfile profile = credentials.getUserProfile();
    assertNotNull(profile);
    assertTrue(profile instanceof MongoProfile);
    final MongoProfile dbProfile = (MongoProfile) profile;
    assertEquals(GOOD_USERNAME, dbProfile.getId());
    assertEquals(FIRSTNAME_VALUE, dbProfile.getAttribute(FIRSTNAME));
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MongoProfile(org.pac4j.mongo.profile.MongoProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 55 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class MongoProfileServiceIT method testCreateUpdateFindDelete.

@Test
public void testCreateUpdateFindDelete() {
    final ObjectId objectId = new ObjectId();
    final MongoProfile profile = new MongoProfile();
    profile.setId(MONGO_ID);
    profile.setLinkedId(MONGO_LINKEDID);
    profile.addAttribute(USERNAME, MONGO_USER);
    profile.addAttribute(FIRSTNAME, objectId);
    final MongoProfileService mongoProfileService = new MongoProfileService(getClient());
    mongoProfileService.setPasswordEncoder(MongoServer.PASSWORD_ENCODER);
    // create
    mongoProfileService.create(profile, MONGO_PASS);
    // check credentials
    final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(MONGO_USER, MONGO_PASS);
    mongoProfileService.validate(credentials, null);
    final CommonProfile profile1 = credentials.getUserProfile();
    assertNotNull(profile1);
    // check data
    final List<Map<String, Object>> results = getData(mongoProfileService, MONGO_ID);
    assertEquals(1, results.size());
    final Map<String, Object> result = results.get(0);
    assertEquals(6, result.size());
    assertEquals(MONGO_ID, result.get(ID));
    assertEquals(MONGO_LINKEDID, result.get(AbstractProfileService.LINKEDID));
    assertNotNull(result.get(AbstractProfileService.SERIALIZED_PROFILE));
    assertTrue(MongoServer.PASSWORD_ENCODER.matches(MONGO_PASS, (String) result.get(PASSWORD)));
    assertEquals(MONGO_USER, result.get(USERNAME));
    // findById
    final MongoProfile profile2 = mongoProfileService.findByLinkedId(MONGO_LINKEDID);
    assertEquals(MONGO_ID, profile2.getId());
    assertEquals(MONGO_LINKEDID, profile2.getLinkedId());
    assertEquals(MONGO_USER, profile2.getUsername());
    assertEquals(objectId, profile2.getAttribute(FIRSTNAME));
    assertEquals(2, profile2.getAttributes().size());
    // update
    profile.setLinkedId(MONGO_LINKEDID2);
    mongoProfileService.update(profile, MONGO_PASS2);
    final List<Map<String, Object>> results2 = getData(mongoProfileService, MONGO_ID);
    assertEquals(1, results2.size());
    final Map<String, Object> result2 = results2.get(0);
    assertEquals(6, result2.size());
    assertEquals(MONGO_ID, result2.get(ID));
    assertEquals(MONGO_LINKEDID2, result2.get(AbstractProfileService.LINKEDID));
    assertNotNull(result2.get(AbstractProfileService.SERIALIZED_PROFILE));
    assertTrue(MongoServer.PASSWORD_ENCODER.matches(MONGO_PASS2, (String) result2.get(PASSWORD)));
    assertEquals(MONGO_USER, result2.get(USERNAME));
    // remove
    mongoProfileService.remove(profile);
    final List<Map<String, Object>> results3 = getData(mongoProfileService, MONGO_ID);
    assertEquals(0, results3.size());
}
Also used : ObjectId(org.bson.types.ObjectId) CommonProfile(org.pac4j.core.profile.CommonProfile) MongoProfile(org.pac4j.mongo.profile.MongoProfile) Map(java.util.Map) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)91 lombok.val (lombok.val)35 Test (org.junit.Test)25 JEEContext (org.pac4j.jee.context.JEEContext)24 CommonProfile (org.pac4j.core.profile.CommonProfile)23 Test (org.junit.jupiter.api.Test)21 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)21 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)21 MockSessionStore (org.pac4j.core.context.session.MockSessionStore)17 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)10 BasicAuthExtractor (org.pac4j.core.credentials.extractor.BasicAuthExtractor)10 OAuth20DefaultCode (org.apereo.cas.ticket.code.OAuth20DefaultCode)8 HardTimeoutExpirationPolicy (org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy)8 HashMap (java.util.HashMap)7 SimpleTestUsernamePasswordAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator)7 Map (java.util.Map)6 WebContext (org.pac4j.core.context.WebContext)5 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)4 CasRestProfile (org.pac4j.cas.profile.CasRestProfile)4 CredentialsException (org.pac4j.core.exception.CredentialsException)4