use of org.apereo.cas.rest.BadRestRequestException 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.rest.BadRestRequestException 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.rest.BadRestRequestException 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");
}
Aggregations