use of org.apereo.cas.services.UnauthorizedServiceException in project cas by apereo.
the class ServiceAuthorizationCheck method doExecute.
@Override
protected Event doExecute(final RequestContext context) throws Exception {
final Service service = WebUtils.getService(context);
//No service == plain /login request. Return success indicating transition to the login form
if (service == null) {
return success();
}
if (this.servicesManager.getAllServices().isEmpty()) {
final String msg = String.format("No service definitions are found in the service manager. " + "Service [%s] will not be automatically authorized to request authentication.", service.getId());
LOGGER.warn(msg);
throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_EMPTY_SVC_MGMR, msg);
}
final RegisteredService registeredService = this.servicesManager.findServiceBy(service);
if (registeredService == null) {
final String msg = String.format("Service Management: missing service. " + "Service [%s] is not found in service registry.", service.getId());
LOGGER.warn(msg);
throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, msg);
}
if (!registeredService.getAccessStrategy().isServiceAccessAllowed()) {
final String msg = String.format("Service Management: Unauthorized Service Access. " + "Service [%s] is not allowed access via the service registry.", service.getId());
LOGGER.warn(msg);
WebUtils.putUnauthorizedRedirectUrlIntoFlowScope(context, registeredService.getAccessStrategy().getUnauthorizedRedirectUrl());
throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, msg);
}
return success();
}
use of org.apereo.cas.services.UnauthorizedServiceException in project cas by apereo.
the class BaseWSFederationRequestController method findAndValidateFederationRequestForRegisteredService.
/**
* Gets ws federation registered service.
*
* @param response the response
* @param request the request
* @param fedRequest the fed request
* @return the ws federation registered service
*/
protected WSFederationRegisteredService findAndValidateFederationRequestForRegisteredService(final HttpServletResponse response, final HttpServletRequest request, final WSFederationRequest fedRequest) {
final String serviceUrl = constructServiceUrl(request, response, fedRequest);
final Service targetService = this.serviceSelectionStrategy.resolveServiceFrom(this.webApplicationServiceFactory.createService(serviceUrl));
final WSFederationRegisteredService svc = getWsFederationRegisteredService(targetService);
final WsFederationProperties.IdentityProvider idp = casProperties.getAuthn().getWsfedIdP().getIdp();
if (StringUtils.isBlank(fedRequest.getWtrealm()) || !StringUtils.equals(fedRequest.getWtrealm(), svc.getRealm())) {
LOGGER.warn("Realm [{}] is not authorized for matching service [{}]", fedRequest.getWtrealm(), svc);
throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, StringUtils.EMPTY);
}
if (!StringUtils.equals(idp.getRealm(), svc.getRealm())) {
LOGGER.warn("Realm [{}] is not authorized for the identity provider realm [{}]", fedRequest.getWtrealm(), idp.getRealm());
throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, StringUtils.EMPTY);
}
return svc;
}
use of org.apereo.cas.services.UnauthorizedServiceException in project cas by apereo.
the class ProxyController method handleRequestInternal.
/**
* Handle request internal.
*
* @param request the request
* @param response the response
* @return ModelAndView containing a view name of either
* {@code casProxyFailureView} or {@code casProxySuccessView}
*/
@GetMapping(path = "/proxy")
protected ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) {
final String proxyGrantingTicket = request.getParameter(CasProtocolConstants.PARAMETER_PROXY_GRANTING_TICKET);
final Service targetService = getTargetService(request);
if (!StringUtils.hasText(proxyGrantingTicket) || targetService == null) {
return generateErrorView(CasProtocolConstants.ERROR_CODE_INVALID_REQUEST_PROXY, null, request);
}
try {
final ProxyTicket proxyTicket = this.centralAuthenticationService.grantProxyTicket(proxyGrantingTicket, targetService);
return new ModelAndView(CONST_PROXY_SUCCESS, MODEL_SERVICE_TICKET, proxyTicket);
} catch (final AbstractTicketException e) {
return generateErrorView(e.getCode(), new Object[] { proxyGrantingTicket }, request);
} catch (final UnauthorizedServiceException e) {
return generateErrorView(CasProtocolConstants.ERROR_CODE_UNAUTHORIZED_SERVICE_PROXY, new Object[] { targetService }, request);
}
}
use of org.apereo.cas.services.UnauthorizedServiceException in project cas by apereo.
the class WSFederationValidateRequestCallbackController method validateSecurityTokenInAssertion.
private SecurityToken validateSecurityTokenInAssertion(final Assertion assertion, final HttpServletRequest request, final HttpServletResponse response) {
LOGGER.debug("Validating security token in CAS assertion...");
final AttributePrincipal principal = assertion.getPrincipal();
if (!principal.getAttributes().containsKey(WSFederationConstants.SECURITY_TOKEN_ATTRIBUTE)) {
throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE);
}
final String token = (String) principal.getAttributes().get(WSFederationConstants.SECURITY_TOKEN_ATTRIBUTE);
final byte[] securityTokenBin = EncodingUtils.decodeBase64(token);
return SerializationUtils.deserialize(securityTokenBin);
}
Aggregations