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());
}
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);
}
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);
}
}
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;
}
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());
}
Aggregations