use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class RestAuthenticatorIT method testParsingError.
@Test
public void testParsingError() {
final RestAuthenticator authenticator = new RestAuthenticator("http://localhost:" + PORT + "?r=pe");
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(GOOD_USERNAME, PASSWORD);
TestsHelper.expectException(() -> authenticator.validate(credentials, MockWebContext.create()), TechnicalException.class, "com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'bad': was expecting ('true', 'false' or 'null')\n" + " at [Source: (String)\"bad\"; line: 1, column: 7]");
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class FormClient method retrieveCredentials.
@Override
protected UsernamePasswordCredentials retrieveCredentials(final WebContext context) {
CommonHelper.assertNotNull("credentialsExtractor", getCredentialsExtractor());
CommonHelper.assertNotNull("authenticator", getAuthenticator());
final String username = context.getRequestParameter(this.usernameParameter);
UsernamePasswordCredentials credentials;
try {
// retrieve credentials
credentials = getCredentialsExtractor().extract(context);
logger.debug("usernamePasswordCredentials: {}", credentials);
if (credentials == null) {
throw handleInvalidCredentials(context, username, "Username and password cannot be blank -> return to the form with error", MISSING_FIELD_ERROR);
}
// validate credentials
getAuthenticator().validate(credentials, context);
} catch (final CredentialsException e) {
throw handleInvalidCredentials(context, username, "Credentials validation fails -> return to the form with error", computeErrorMessage(e));
}
return credentials;
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class IndirectBasicAuthClient method retrieveCredentials.
@Override
protected UsernamePasswordCredentials retrieveCredentials(final WebContext context) {
assertNotNull("credentialsExtractor", getCredentialsExtractor());
assertNotNull("authenticator", getAuthenticator());
// set the www-authenticate in case of error
context.setResponseHeader(HttpConstants.AUTHENTICATE_HEADER, "Basic realm=\"" + realmName + "\"");
final UsernamePasswordCredentials credentials;
try {
// retrieve credentials
credentials = getCredentialsExtractor().extract(context);
logger.debug("credentials : {}", credentials);
if (credentials == null) {
throw HttpAction.unauthorized(context);
}
// validate credentials
getAuthenticator().validate(credentials, context);
} catch (final CredentialsException e) {
throw HttpAction.unauthorized(context);
}
return credentials;
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class CouchProfileServiceTests method authentFailed.
@Test(expected = AccountNotFoundException.class)
public void authentFailed() {
final CouchProfileService couchProfileService = new CouchProfileService(couchDbConnector);
couchProfileService.setPasswordEncoder(PASSWORD_ENCODER);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(BAD_USERNAME, PASSWORD);
couchProfileService.validate(credentials, null);
}
use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.
the class InMemoryProfileServiceTests method testCreateUpdateFindDelete.
@Test
public void testCreateUpdateFindDelete() {
final CommonProfile profile = new CommonProfile();
profile.setId(TEST_ID);
profile.setLinkedId(TEST_LINKED_ID);
profile.addAttribute(USERNAME, TEST_USER);
// create
inMemoryProfileService.create(profile, TEST_PASS);
// check credentials
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(TEST_USER, TEST_PASS);
inMemoryProfileService.validate(credentials, null);
final CommonProfile profile1 = credentials.getUserProfile();
assertNotNull(profile1);
// check data
final List<Map<String, Object>> results = getData(TEST_ID);
assertEquals(1, results.size());
final Map<String, Object> result = results.get(0);
assertEquals(5, result.size());
assertEquals(TEST_ID, result.get("id"));
assertEquals(TEST_LINKED_ID, result.get(AbstractProfileService.LINKEDID));
assertNotNull(result.get(AbstractProfileService.SERIALIZED_PROFILE));
assertEquals(TEST_USER, result.get(USERNAME));
// findById
final CommonProfile profile2 = inMemoryProfileService.findById(TEST_ID);
assertEquals(TEST_ID, profile2.getId());
assertEquals(TEST_LINKED_ID, profile2.getLinkedId());
assertEquals(TEST_USER, profile2.getUsername());
assertEquals(1, profile2.getAttributes().size());
// update with password
profile.addAttribute(USERNAME, TEST_USER2);
inMemoryProfileService.update(profile, TEST_PASS2);
List<Map<String, Object>> results2 = getData(TEST_ID);
assertEquals(1, results2.size());
Map<String, Object> result2 = results2.get(0);
assertEquals(5, result2.size());
assertEquals(TEST_ID, result2.get("id"));
assertEquals(TEST_LINKED_ID, result2.get(AbstractProfileService.LINKEDID));
assertNotNull(result2.get(AbstractProfileService.SERIALIZED_PROFILE));
assertEquals(TEST_USER2, result2.get(USERNAME));
// check credentials
final UsernamePasswordCredentials credentials2 = new UsernamePasswordCredentials(TEST_USER2, TEST_PASS2);
inMemoryProfileService.validate(credentials2, null);
final CommonProfile profile3 = credentials.getUserProfile();
assertNotNull(profile3);
// update with no password update
inMemoryProfileService.update(profile, null);
results2 = getData(TEST_ID);
assertEquals(1, results2.size());
result2 = results2.get(0);
assertEquals(5, result2.size());
assertEquals(TEST_USER2, result2.get(USERNAME));
// check credentials
inMemoryProfileService.validate(credentials2, null);
final CommonProfile profile4 = credentials.getUserProfile();
assertNotNull(profile4);
// remove
inMemoryProfileService.remove(profile);
final List<Map<String, Object>> results3 = getData(TEST_ID);
assertEquals(0, results3.size());
}
Aggregations