Search in sources :

Example 1 with DbProfile

use of org.pac4j.sql.profile.DbProfile in project pac4j by pac4j.

the class DbProfileServiceTests method testGoodUsernameAttribute.

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

Example 2 with DbProfile

use of org.pac4j.sql.profile.DbProfile in project pac4j by pac4j.

the class DbProfileService method internalInit.

@Override
protected void internalInit(final boolean forceReinit) {
    assertNotNull("passwordEncoder", getPasswordEncoder());
    assertNotNull("dataSource", this.dataSource);
    this.dbi = new DBI(this.dataSource);
    defaultProfileDefinition(new CommonProfileDefinition(x -> new DbProfile()));
    setSerializer(new JsonSerializer(DbProfile.class));
    super.internalInit(forceReinit);
}
Also used : AbstractProfileService(org.pac4j.core.profile.service.AbstractProfileService) List(java.util.List) Handle(org.skife.jdbi.v2.Handle) PasswordEncoder(org.pac4j.core.credentials.password.PasswordEncoder) CommonProfileDefinition(org.pac4j.core.profile.definition.CommonProfileDefinition) DbProfile(org.pac4j.sql.profile.DbProfile) DBI(org.skife.jdbi.v2.DBI) Map(java.util.Map) DataSource(javax.sql.DataSource) CommonHelper(org.pac4j.core.util.CommonHelper) JsonSerializer(org.pac4j.core.util.serializer.JsonSerializer) ArrayList(java.util.ArrayList) DBI(org.skife.jdbi.v2.DBI) JsonSerializer(org.pac4j.core.util.serializer.JsonSerializer) DbProfile(org.pac4j.sql.profile.DbProfile) CommonProfileDefinition(org.pac4j.core.profile.definition.CommonProfileDefinition)

Example 3 with DbProfile

use of org.pac4j.sql.profile.DbProfile in project pac4j by pac4j.

the class DbProfileService method internalInit.

@Override
protected void internalInit() {
    CommonHelper.assertNotNull("passwordEncoder", getPasswordEncoder());
    defaultProfileDefinition(new CommonProfileDefinition<>(x -> new DbProfile()));
    CommonHelper.assertNotNull("dataSource", this.dataSource);
    this.dbi = new DBI(this.dataSource);
    super.internalInit();
}
Also used : AbstractProfileService(org.pac4j.core.profile.service.AbstractProfileService) List(java.util.List) Handle(org.skife.jdbi.v2.Handle) PasswordEncoder(org.pac4j.core.credentials.password.PasswordEncoder) CommonProfileDefinition(org.pac4j.core.profile.definition.CommonProfileDefinition) DbProfile(org.pac4j.sql.profile.DbProfile) DBI(org.skife.jdbi.v2.DBI) Map(java.util.Map) DataSource(javax.sql.DataSource) CommonHelper(org.pac4j.core.util.CommonHelper) ArrayList(java.util.ArrayList) DBI(org.skife.jdbi.v2.DBI) DbProfile(org.pac4j.sql.profile.DbProfile)

Example 4 with DbProfile

use of org.pac4j.sql.profile.DbProfile 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 5 with DbProfile

use of org.pac4j.sql.profile.DbProfile 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)

Aggregations

DbProfile (org.pac4j.sql.profile.DbProfile)5 Map (java.util.Map)3 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)3 CommonProfile (org.pac4j.core.profile.CommonProfile)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DataSource (javax.sql.DataSource)2 PasswordEncoder (org.pac4j.core.credentials.password.PasswordEncoder)2 CommonProfileDefinition (org.pac4j.core.profile.definition.CommonProfileDefinition)2 AbstractProfileService (org.pac4j.core.profile.service.AbstractProfileService)2 CommonHelper (org.pac4j.core.util.CommonHelper)2 DBI (org.skife.jdbi.v2.DBI)2 Handle (org.skife.jdbi.v2.Handle)2 JsonSerializer (org.pac4j.core.util.serializer.JsonSerializer)1