Search in sources :

Example 16 with UsernamePasswordCredentials

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]");
}
Also used : UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 17 with UsernamePasswordCredentials

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;
}
Also used : CredentialsException(org.pac4j.core.exception.CredentialsException) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 18 with UsernamePasswordCredentials

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;
}
Also used : CredentialsException(org.pac4j.core.exception.CredentialsException) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 19 with UsernamePasswordCredentials

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);
}
Also used : UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 20 with UsernamePasswordCredentials

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

Aggregations

UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)80 lombok.val (lombok.val)34 JEEContext (org.pac4j.core.context.JEEContext)24 CommonProfile (org.pac4j.core.profile.CommonProfile)22 Test (org.junit.Test)21 Test (org.junit.jupiter.api.Test)21 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)20 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)20 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)10 BasicAuthExtractor (org.pac4j.core.credentials.extractor.BasicAuthExtractor)9 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)6 Map (java.util.Map)5 MockWebContext (org.pac4j.core.context.MockWebContext)5 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)4 CredentialsException (org.pac4j.core.exception.CredentialsException)4 ArrayList (java.util.ArrayList)3 WebContext (org.pac4j.core.context.WebContext)3