use of org.pac4j.core.context.JEEContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifyFinishAuthentication.
@Test
public void verifyFinishAuthentication() throws Exception {
val request = new MockHttpServletRequest();
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
request.addParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, "theme");
request.addParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, Locale.getDefault().getCountry());
request.addParameter(CasProtocolConstants.PARAMETER_METHOD, HttpMethod.POST.name());
val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
val client = builtClients.findClient("FacebookClient").get();
val webContext = new JEEContext(request, new MockHttpServletResponse());
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
val context = new MockRequestContext();
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val event = delegatedAuthenticationAction.execute(context);
assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, event.getId());
assertEquals("theme", request.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(Locale.getDefault().getCountry(), request.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(HttpMethod.POST.name(), request.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
assertEquals(service.getId(), request.getAttribute(CasProtocolConstants.PARAMETER_SERVICE));
val flowScope = context.getFlowScope();
assertEquals(service.getId(), ((Service) flowScope.get(CasProtocolConstants.PARAMETER_SERVICE)).getId());
val credential = flowScope.get(CasWebflowConstants.VAR_ID_CREDENTIAL, ClientCredential.class);
assertNotNull(credential);
assertEquals(credential.getId(), "casuser");
}
use of org.pac4j.core.context.JEEContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifySsoAuthenticationUnauthz.
@Test
public void verifySsoAuthenticationUnauthz() throws Exception {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
val service = CoreAuthenticationTestUtils.getService("https://delegated3.example.org");
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
val client = builtClients.findClient("FacebookClient").get();
val webContext = new JEEContext(request, new MockHttpServletResponse());
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
val accessStrategy = new DefaultRegisteredServiceAccessStrategy();
accessStrategy.setEnabled(false);
val registeredService = RegisteredServiceTestUtils.getRegisteredService(service.getId());
registeredService.setAccessStrategy(accessStrategy);
servicesManager.save(registeredService);
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val tgt = new MockTicketGrantingTicket("casuser", Map.of(), Map.of(ClientCredential.AUTHENTICATION_ATTRIBUTE_CLIENT_NAME, List.of("FacebookClient")));
centralAuthenticationService.addTicket(tgt);
WebUtils.putTicketGrantingTicketInScopes(context, tgt);
assertThrows(UnauthorizedServiceException.class, () -> delegatedAuthenticationAction.execute(context).getId());
assertThrows(InvalidTicketException.class, () -> centralAuthenticationService.getTicket(tgt.getId()));
}
use of org.pac4j.core.context.JEEContext in project cas by apereo.
the class DefaultDelegatedClientAuthenticationWebflowManagerTests method setup.
@BeforeEach
public void setup() {
val service = RegisteredServiceTestUtils.getService();
httpServletRequest = new MockHttpServletRequest();
httpServletRequest.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
context = new JEEContext(httpServletRequest, new MockHttpServletResponse());
requestContext = new MockRequestContext();
requestContext.setExternalContext(new ServletExternalContext(new MockServletContext(), context.getNativeRequest(), context.getNativeResponse()));
RequestContextHolder.setRequestContext(requestContext);
ExternalContextHolder.setExternalContext(requestContext.getExternalContext());
}
use of org.pac4j.core.context.JEEContext in project cas by apereo.
the class AbstractSamlIdPProfileHandlerController method retrieveAuthenticationRequest.
/**
* Retrieve authentication request.
*
* @param response the response
* @param request the request
* @return the authn request
*/
@Synchronized
protected final Pair<? extends RequestAbstractType, MessageContext> retrieveAuthenticationRequest(final HttpServletResponse response, final HttpServletRequest request) {
LOGGER.info("Received SAML callback profile request [{}]", request.getRequestURI());
val webContext = new JEEContext(request, response);
return SamlIdPUtils.retrieveSamlRequest(webContext, configurationContext.getSessionStore(), configurationContext.getOpenSamlConfigBean(), AuthnRequest.class).orElseThrow(() -> new IllegalArgumentException("SAML request or context could not be determined from session store"));
}
use of org.pac4j.core.context.JEEContext in project cas by apereo.
the class AbstractSamlIdPProfileHandlerController method issueAuthenticationRequestRedirect.
/**
* Redirect request for authentication.
*
* @param pair the pair
* @param request the request
* @param response the response
* @return the model and view
* @throws Exception the exception
*/
protected ModelAndView issueAuthenticationRequestRedirect(final Pair<? extends SignableSAMLObject, MessageContext> pair, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
val authnRequest = (AuthnRequest) pair.getLeft();
val serviceUrl = constructServiceUrl(request, response, pair);
LOGGER.debug("Created service url [{}]", DigestUtils.abbreviate(serviceUrl));
val properties = configurationContext.getCasProperties();
val urlToRedirectTo = CommonUtils.constructRedirectUrl(properties.getServer().getLoginUrl(), CasProtocolConstants.PARAMETER_SERVICE, serviceUrl, authnRequest.isForceAuthn(), authnRequest.isPassive());
LOGGER.debug("Redirecting SAML authN request to [{}]", urlToRedirectTo);
val type = properties.getAuthn().getSamlIdp().getCore().getSessionStorageType();
if (type == SamlIdPCoreProperties.SessionStorageTypes.BROWSER_SESSION_STORAGE) {
val context = new JEEContext(request, response);
val sessionStorage = configurationContext.getSessionStore().getTrackableSession(context).map(BrowserSessionStorage.class::cast).orElseThrow(() -> new IllegalStateException("Unable to determine trackable session for storage"));
sessionStorage.setDestinationUrl(urlToRedirectTo);
return new ModelAndView(CasWebflowConstants.VIEW_ID_SESSION_STORAGE_WRITE, BrowserSessionStorage.KEY_SESSION_STORAGE, sessionStorage);
}
LOGGER.debug("Redirecting SAML authN request to [{}]", urlToRedirectTo);
val mv = new ModelAndView(new RedirectView(urlToRedirectTo));
mv.setStatus(HttpStatus.FOUND);
return mv;
}
Aggregations