Search in sources :

Example 1 with BadRestRequestException

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);
}
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 2 with BadRestRequestException

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);
    }
}
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 3 with BadRestRequestException

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");
}
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)

Aggregations

AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)3 Credential (org.apereo.cas.authentication.Credential)3 Service (org.apereo.cas.authentication.principal.Service)3 BadRestRequestException (org.apereo.cas.rest.BadRestRequestException)3 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)1 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)1 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)1 RegisteredService (org.apereo.cas.services.RegisteredService)1 J2EContext (org.pac4j.core.context.J2EContext)1 WebContext (org.pac4j.core.context.WebContext)1 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)1 BasicAuthExtractor (org.pac4j.core.credentials.extractor.BasicAuthExtractor)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1