Search in sources :

Example 31 with AuthenticationResult

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

the class JWTTicketGrantingTicketResourceEntityResponseFactoryTests method verifyTicketGrantingTicketAsJwtWithHeader.

@Test
public void verifyTicketGrantingTicketAsJwtWithHeader() throws Exception {
    final AuthenticationResult result = CoreAuthenticationTestUtils.getAuthenticationResult(authenticationSystemSupport, CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser"));
    final TicketGrantingTicket tgt = centralAuthenticationService.createTicketGrantingTicket(result);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader(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 32 with AuthenticationResult

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

the class TicketGrantingTicketResource method createTicketGrantingTicketForRequest.

/**
 * Create ticket granting ticket for request ticket granting ticket.
 *
 * @param requestBody the request body
 * @param request     the request
 * @return the ticket granting ticket
 */
protected TicketGrantingTicket createTicketGrantingTicketForRequest(final MultiValueMap<String, String> requestBody, final HttpServletRequest request) {
    final Collection<Credential> credential = this.credentialFactory.fromRequestBody(requestBody);
    if (credential == null || credential.isEmpty()) {
        throw new BadRestRequestException("No credentials are provided or extracted to authenticate the REST request");
    }
    final Service service = this.serviceFactory.createService(request);
    final AuthenticationResult authenticationResult = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
    return centralAuthenticationService.createTicketGrantingTicket(authenticationResult);
}
Also used : Credential(org.apereo.cas.authentication.Credential) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) Service(org.apereo.cas.authentication.principal.Service) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Example 33 with AuthenticationResult

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

the class UserAuthenticationResource method createTicketGrantingTicket.

/**
 * Create new ticket granting ticket.
 *
 * @param requestBody username and password application/x-www-form-urlencoded values
 * @param request     raw HttpServletRequest used to call this method
 * @return ResponseEntity representing RESTful response
 */
@PostMapping(value = "/v1/users", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> createTicketGrantingTicket(@RequestBody final MultiValueMap<String, String> requestBody, final HttpServletRequest request) {
    try {
        final Collection<Credential> credential = this.credentialFactory.fromRequestBody(requestBody);
        if (credential == null || credential.isEmpty()) {
            throw new BadRestRequestException("No credentials are provided or extracted to authenticate the REST request");
        }
        final Service service = this.serviceFactory.createService(request);
        final AuthenticationResult authenticationResult = authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(service, credential);
        return this.userAuthenticationResourceEntityResponseFactory.build(authenticationResult, request);
    } catch (final AuthenticationException e) {
        return RestResourceUtils.createResponseEntityForAuthnFailure(e);
    } catch (final BadRestRequestException e) {
        LOGGER.error(e.getMessage(), e);
        return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : Credential(org.apereo.cas.authentication.Credential) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) Service(org.apereo.cas.authentication.principal.Service) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) BadRestRequestException(org.apereo.cas.rest.BadRestRequestException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 34 with AuthenticationResult

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

the class AbstractServiceValidateController method handleProxyGrantingTicketDelivery.

/**
 * Handle proxy granting ticket delivery.
 *
 * @param serviceTicketId the service ticket id
 * @param credential      the service credential
 * @return the ticket granting ticket
 */
private TicketGrantingTicket handleProxyGrantingTicketDelivery(final String serviceTicketId, final Credential credential) throws AuthenticationException, AbstractTicketException {
    final ServiceTicket serviceTicket = this.centralAuthenticationService.getTicket(serviceTicketId, ServiceTicket.class);
    final AuthenticationResult authenticationResult = this.authenticationSystemSupport.handleAndFinalizeSingleAuthenticationTransaction(serviceTicket.getService(), credential);
    final TicketGrantingTicket proxyGrantingTicketId = this.centralAuthenticationService.createProxyGrantingTicket(serviceTicketId, authenticationResult);
    LOGGER.debug("Generated proxy-granting ticket [{}] off of service ticket [{}] and credential [{}]", proxyGrantingTicketId.getId(), serviceTicketId, credential);
    return proxyGrantingTicketId;
}
Also used : TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult)

Example 35 with AuthenticationResult

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

the class AbstractServiceValidateControllerTests method getModelAndViewUponServiceValidationWithSecurePgtUrl.

/*
    Helper methods.
     */
protected ModelAndView getModelAndViewUponServiceValidationWithSecurePgtUrl() throws Exception {
    final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), SERVICE);
    final TicketGrantingTicket tId = getCentralAuthenticationService().createTicketGrantingTicket(ctx);
    final ServiceTicket sId = getCentralAuthenticationService().grantServiceTicket(tId.getId(), SERVICE, ctx);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, SERVICE.getId());
    request.addParameter(CasProtocolConstants.PARAMETER_TICKET, sId.getId());
    request.addParameter(CasProtocolConstants.PARAMETER_PROXY_GRANTING_TICKET_URL, GITHUB_URL);
    return this.serviceValidateController.handleRequestInternal(request, new MockHttpServletResponse());
}
Also used : TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServiceTicket(org.apereo.cas.ticket.ServiceTicket) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) 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