Search in sources :

Example 41 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project cas by apereo.

the class OidcIntrospectionEndpointController method handlePostRequest.

/**
 * Handle post request.
 *
 * @param request  the request
 * @param response the response
 * @return the response entity
 */
@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, value = { '/' + OidcConstants.BASE_OIDC_URL + '/' + OidcConstants.INTROSPECTION_URL })
public ResponseEntity<OidcIntrospectionAccessTokenResponse> handlePostRequest(final HttpServletRequest request, final HttpServletResponse response) {
    try {
        final CredentialsExtractor<UsernamePasswordCredentials> authExtractor = new BasicAuthExtractor();
        final UsernamePasswordCredentials credentials = authExtractor.extract(Pac4jUtils.getPac4jJ2EContext(request, response));
        if (credentials == null) {
            throw new IllegalArgumentException("No credentials are provided to verify introspection on the access token");
        }
        final OAuthRegisteredService service = OAuth20Utils.getRegisteredOAuthServiceByClientId(this.servicesManager, credentials.getUsername());
        if (validateIntrospectionRequest(service, credentials, request)) {
            final String accessToken = StringUtils.defaultIfBlank(request.getParameter(OAuth20Constants.ACCESS_TOKEN), request.getParameter(OAuth20Constants.TOKEN));
            LOGGER.debug("Located access token [{}] in the request", accessToken);
            final AccessToken ticket = this.centralAuthenticationService.getTicket(accessToken, AccessToken.class);
            if (ticket != null) {
                return createIntrospectionResponse(service, ticket);
            }
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : BasicAuthExtractor(org.pac4j.core.credentials.extractor.BasicAuthExtractor) ResponseEntity(org.springframework.http.ResponseEntity) OAuthRegisteredService(org.apereo.cas.support.oauth.services.OAuthRegisteredService) AccessToken(org.apereo.cas.ticket.accesstoken.AccessToken) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 42 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials 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 43 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials 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)

Example 44 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class DirectFormClientTests method testGetGoodCredentials.

@Test
public void testGetGoodCredentials() {
    final DirectFormClient formClient = getFormClient();
    final UsernamePasswordCredentials credentials = formClient.getCredentials(MockWebContext.create().addRequestParameter(formClient.getUsernameParameter(), USERNAME).addRequestParameter(formClient.getPasswordParameter(), USERNAME));
    assertEquals(USERNAME, credentials.getUsername());
    assertEquals(USERNAME, credentials.getPassword());
}
Also used : UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 45 with UsernamePasswordCredentials

use of org.pac4j.core.credentials.UsernamePasswordCredentials in project pac4j by pac4j.

the class DirectFormClientTests method testMissingProfileCreator.

@Test
public void testMissingProfileCreator() {
    final DirectFormClient formClient = new DirectFormClient(new SimpleTestUsernamePasswordAuthenticator(), null);
    TestsHelper.expectException(() -> formClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, PASSWORD), MockWebContext.create()), TechnicalException.class, "profileCreator cannot be null");
}
Also used : SimpleTestUsernamePasswordAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

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