Search in sources :

Example 56 with UsernamePasswordCredentials

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

Example 57 with UsernamePasswordCredentials

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

Example 58 with UsernamePasswordCredentials

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

Example 59 with UsernamePasswordCredentials

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

Example 60 with UsernamePasswordCredentials

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