use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class BasicAuthExtractor method extract.
@Override
public UsernamePasswordCredentials extract(WebContext context) {
final TokenCredentials credentials = this.extractor.extract(context);
if (credentials == null) {
return null;
}
final byte[] decoded = Base64.getDecoder().decode(credentials.getToken());
String token;
try {
token = new String(decoded, "UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new CredentialsException("Bad format of the basic auth header");
}
final int delim = token.indexOf(":");
if (delim < 0) {
throw new CredentialsException("Bad format of the basic auth header");
}
return new UsernamePasswordCredentials(token.substring(0, delim), token.substring(delim + 1));
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class CouchProfileServiceTests method authentSuccessSingleAttribute.
@Test
public void authentSuccessSingleAttribute() {
final CouchProfileService couchProfileService = new CouchProfileService(couchDbConnector);
couchProfileService.setPasswordEncoder(PASSWORD_ENCODER);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
couchProfileService.validate(credentials, null);
final CommonProfile profile = credentials.getUserProfile();
assertNotNull(profile);
assertTrue(profile instanceof CouchProfile);
final CouchProfile couchProfile = (CouchProfile) profile;
assertEquals(GOOD_USERNAME, couchProfile.getUsername());
assertEquals(2, couchProfile.getAttributes().size());
assertEquals(FIRSTNAME_VALUE, couchProfile.getAttribute(FIRSTNAME));
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class CouchProfileServiceTests method testCreateUpdateFindDelete.
@Test
public void testCreateUpdateFindDelete() {
final CouchProfile profile = new CouchProfile();
profile.setId(COUCH_ID);
profile.setLinkedId(COUCH_LINKED_ID);
profile.addAttribute(USERNAME, COUCH_USER);
final CouchProfileService couchProfileService = new CouchProfileService(couchDbConnector);
couchProfileService.setPasswordEncoder(PASSWORD_ENCODER);
// create
couchProfileService.create(profile, COUCH_PASS);
// check credentials
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(COUCH_USER, COUCH_PASS);
couchProfileService.validate(credentials, null);
final CommonProfile profile1 = credentials.getUserProfile();
assertNotNull(profile1);
// check data
final List<Map<String, Object>> results = getData(couchProfileService, COUCH_ID);
assertEquals(1, results.size());
final Map<String, Object> result = results.get(0);
assertEquals(5, result.size());
assertEquals(COUCH_ID, result.get(COUCH_ID_FIELD));
assertEquals(COUCH_LINKED_ID, result.get(AbstractProfileService.LINKEDID));
assertNotNull(result.get(AbstractProfileService.SERIALIZED_PROFILE));
assertEquals(COUCH_USER, result.get(USERNAME));
// findById
final CouchProfile profile2 = couchProfileService.findById(COUCH_ID);
assertEquals(COUCH_ID, profile2.getId());
assertEquals(COUCH_LINKED_ID, profile2.getLinkedId());
assertEquals(COUCH_USER, profile2.getUsername());
assertEquals(1, profile2.getAttributes().size());
// update with password
profile.addAttribute(USERNAME, COUCH_USER2);
couchProfileService.update(profile, COUCH_PASS2);
List<Map<String, Object>> results2 = getData(couchProfileService, COUCH_ID);
assertEquals(1, results2.size());
Map<String, Object> result2 = results2.get(0);
assertEquals(5, result2.size());
assertEquals(COUCH_ID, result2.get(COUCH_ID_FIELD));
assertEquals(COUCH_LINKED_ID, result2.get(AbstractProfileService.LINKEDID));
assertNotNull(result2.get(AbstractProfileService.SERIALIZED_PROFILE));
assertEquals(COUCH_USER2, result2.get(USERNAME));
// check credentials
final UsernamePasswordCredentials credentials2 = new UsernamePasswordCredentials(COUCH_USER2, COUCH_PASS2);
couchProfileService.validate(credentials2, null);
CommonProfile profile3 = credentials.getUserProfile();
assertNotNull(profile3);
// update with no password update
couchProfileService.update(profile, null);
results2 = getData(couchProfileService, COUCH_ID);
assertEquals(1, results2.size());
result2 = results2.get(0);
assertEquals(5, result2.size());
assertEquals(COUCH_USER2, result2.get(USERNAME));
// check credentials
couchProfileService.validate(credentials2, null);
profile3 = credentials.getUserProfile();
assertNotNull(profile3);
// remove
couchProfileService.remove(profile);
final List<Map<String, Object>> results3 = getData(couchProfileService, COUCH_ID);
assertEquals(0, results3.size());
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class InMemoryProfileServiceTests method authentFailed.
@Test(expected = AccountNotFoundException.class)
public void authentFailed() {
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(BAD_USERNAME, PASSWORD);
inMemoryProfileService.validate(credentials, null);
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class InMemoryProfileServiceTests method authentSuccessSingleAttribute.
@Test
public void authentSuccessSingleAttribute() {
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
inMemoryProfileService.validate(credentials, null);
final CommonProfile profile = credentials.getUserProfile();
assertNotNull(profile);
assertEquals(GOOD_USERNAME, profile.getUsername());
assertEquals(2, profile.getAttributes().size());
assertEquals(FIRSTNAME_VALUE, profile.getAttribute(FIRSTNAME));
}
Aggregations