Search in sources :

Example 81 with AuthenticationResult

use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.

the class OpenIdServiceTests method verifyGetResponse.

@Test
public void verifyGetResponse() {
    try {
        request.removeParameter(OpenIdProtocolConstants.OPENID_ASSOCHANDLE);
        request.addParameter(OpenIdProtocolConstants.OPENID_ASSOCHANDLE, association.getHandle());
        openIdService = openIdServiceFactory.createService(request);
        final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), openIdService);
        final String tgt = centralAuthenticationService.createTicketGrantingTicket(ctx).getId();
        final String st = centralAuthenticationService.grantServiceTicket(tgt, openIdService, ctx).getId();
        centralAuthenticationService.validateServiceTicket(st, openIdService);
        final Response response = new OpenIdServiceResponseBuilder(OPEN_ID_PREFIX_URL, serverManager, centralAuthenticationService, new DefaultServicesManager(mock(ServiceRegistry.class), mock(ApplicationEventPublisher.class))).build(openIdService, "something", CoreAuthenticationTestUtils.getAuthentication());
        assertNotNull(response);
        assertEquals(association.getHandle(), response.getAttributes().get(OpenIdProtocolConstants.OPENID_ASSOCHANDLE));
        assertEquals(RETURN_TO_URL, response.getAttributes().get(OpenIdProtocolConstants.OPENID_RETURNTO));
        assertEquals(OPEN_ID_PREFIX_URL, response.getAttributes().get(OpenIdProtocolConstants.OPENID_IDENTITY));
        final Response response2 = new OpenIdServiceResponseBuilder(OPEN_ID_PREFIX_URL, serverManager, centralAuthenticationService, new DefaultServicesManager(mock(ServiceRegistry.class), mock(ApplicationEventPublisher.class))).build(openIdService, null, CoreAuthenticationTestUtils.getAuthentication());
        assertEquals("cancel", response2.getAttributes().get(OpenIdProtocolConstants.OPENID_MODE));
    } catch (final Exception e) {
        LOGGER.debug("Exception during verification of service ticket", e);
    }
}
Also used : Response(org.apereo.cas.authentication.principal.Response) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ServiceRegistry(org.apereo.cas.services.ServiceRegistry) DefaultServicesManager(org.apereo.cas.services.DefaultServicesManager) IOException(java.io.IOException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) Test(org.junit.Test)

Example 82 with AuthenticationResult

use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.

the class RegisteredServiceResource method authenticateRequest.

private Authentication authenticateRequest(final HttpServletRequest request, final HttpServletResponse response) {
    final BasicAuthExtractor extractor = new BasicAuthExtractor();
    final WebContext webContext = new J2EContext(request, response);
    final UsernamePasswordCredentials credentials = extractor.extract(webContext);
    if (credentials != null) {
        LOGGER.debug("Received basic authentication request from credentials [{}]", credentials);
        final Credential c = new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
        final Service serviceRequest = this.serviceFactory.createService(request);
        final AuthenticationResult result = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(serviceRequest, c);
        return result.getAuthentication();
    }
    throw new BadRestRequestException("Could not authenticate request");
}
Also used : BasicAuthExtractor(org.pac4j.core.credentials.extractor.BasicAuthExtractor) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Credential(org.apereo.cas.authentication.Credential) WebContext(org.pac4j.core.context.WebContext) RegisteredService(org.apereo.cas.services.RegisteredService) Service(org.apereo.cas.authentication.principal.Service) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) J2EContext(org.pac4j.core.context.J2EContext) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Example 83 with AuthenticationResult

use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.

the class JWTTicketGrantingTicketResourceEntityResponseFactoryTests method verifyTicketGrantingTicketAsJwt.

@Test
public void verifyTicketGrantingTicketAsJwt() throws Exception {
    final AuthenticationResult result = CoreAuthenticationTestUtils.getAuthenticationResult(authenticationSystemSupport, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser"));
    final TicketGrantingTicket tgt = centralAuthenticationService.createTicketGrantingTicket(result);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(TokenConstants.PARAMETER_NAME_TOKEN, Boolean.TRUE.toString());
    final ResponseEntity<String> response = ticketGrantingTicketResourceEntityResponseFactory.build(tgt, request);
    assertNotNull(response);
    assertEquals(HttpStatus.CREATED, response.getStatusCode());
    final Object jwt = this.tokenCipherExecutor.decode(response.getBody());
    final JWTClaimsSet claims = JWTClaimsSet.parse(jwt.toString());
    assertEquals(claims.getSubject(), tgt.getAuthentication().getPrincipal().getId());
}
Also used : TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 84 with AuthenticationResult

use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.

the class ServiceTicketResource method createServiceTicket.

/**
 * Create new service ticket.
 *
 * @param httpServletRequest http request
 * @param tgtId       ticket granting ticket id URI path param
 * @return {@link ResponseEntity} representing RESTful response
 */
@PostMapping(value = "/v1/tickets/{tgtId:.+}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> createServiceTicket(final HttpServletRequest httpServletRequest, @PathVariable("tgtId") final String tgtId) {
    try {
        final Authentication authn = this.ticketRegistrySupport.getAuthenticationFrom(tgtId);
        AuthenticationCredentialsThreadLocalBinder.bindCurrent(authn);
        if (authn == null) {
            throw new InvalidTicketException(tgtId);
        }
        final AuthenticationResultBuilder builder = new DefaultAuthenticationResultBuilder(this.authenticationSystemSupport.getPrincipalElectionStrategy());
        final Service service = this.argumentExtractor.extractService(httpServletRequest);
        if (service == null) {
            throw new IllegalArgumentException("Target service/application is unspecified or unrecognized in the request");
        }
        final AuthenticationResult authenticationResult = builder.collect(authn).build(service);
        return this.serviceTicketResourceEntityResponseFactory.build(tgtId, service, authenticationResult);
    } catch (final InvalidTicketException e) {
        return new ResponseEntity<>(tgtId + " could not be found or is considered invalid", HttpStatus.NOT_FOUND);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    } finally {
        AuthenticationCredentialsThreadLocalBinder.clear();
    }
}
Also used : Authentication(org.apereo.cas.authentication.Authentication) InvalidTicketException(org.apereo.cas.ticket.InvalidTicketException) Service(org.apereo.cas.authentication.principal.Service) DefaultAuthenticationResultBuilder(org.apereo.cas.authentication.DefaultAuthenticationResultBuilder) AuthenticationResultBuilder(org.apereo.cas.authentication.AuthenticationResultBuilder) DefaultAuthenticationResultBuilder(org.apereo.cas.authentication.DefaultAuthenticationResultBuilder) InvalidTicketException(org.apereo.cas.ticket.InvalidTicketException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 85 with AuthenticationResult

use of org.apereo.cas.authentication.AuthenticationResult in project cas by apereo.

the class ECPProfileHandlerController method authenticateEcpRequest.

/**
 * Authenticate ecp request.
 *
 * @param credential   the credential
 * @param authnRequest the authn request
 * @return the authentication
 */
protected Authentication authenticateEcpRequest(final Credential credential, final Pair<AuthnRequest, MessageContext> authnRequest) {
    final String issuer = SamlIdPUtils.getIssuerFromSamlRequest(authnRequest.getKey());
    LOGGER.debug("Located issuer [{}] from request prior to authenticating [{}]", issuer, credential.getId());
    final Service service = webApplicationServiceFactory.createService(issuer);
    LOGGER.debug("Executing authentication request for service [{}] on behalf of credential id [{}]", service, credential.getId());
    final AuthenticationResult authenticationResult = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
    return authenticationResult.getAuthentication();
}
Also used : WebApplicationService(org.apereo.cas.authentication.principal.WebApplicationService) SamlRegisteredService(org.apereo.cas.support.saml.services.SamlRegisteredService) Service(org.apereo.cas.authentication.principal.Service) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Aggregations

AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)92 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)66 Test (org.junit.Test)66 ServiceTicket (org.apereo.cas.ticket.ServiceTicket)47 Service (org.apereo.cas.authentication.principal.Service)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)21 Authentication (org.apereo.cas.authentication.Authentication)17 AbstractWebApplicationService (org.apereo.cas.authentication.principal.AbstractWebApplicationService)16 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)15 Credential (org.apereo.cas.authentication.Credential)13 Assertion (org.apereo.cas.validation.Assertion)12 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)9 RegisteredService (org.apereo.cas.services.RegisteredService)8 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 AuthenticationResultBuilder (org.apereo.cas.authentication.AuthenticationResultBuilder)5 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4