use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifyFinishAuthenticationAuthzFailure.
@Test
public void verifyFinishAuthenticationAuthzFailure() throws Exception {
val request = new MockHttpServletRequest();
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
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());
assertThrows(UnauthorizedServiceException.class, () -> delegatedAuthenticationAction.execute(context));
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifySsoAuthenticationWithUnauthorizedSso.
@Test
public void verifySsoAuthenticationWithUnauthorizedSso() throws Exception {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
val client = builtClients.findClient("FacebookClient").get();
val webContext = new JEEContext(request, new MockHttpServletResponse());
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
val service = CoreAuthenticationTestUtils.getService("https://delegated2-authn-policy.example.org");
val registeredService = RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of());
val authenticationPolicy = new DefaultRegisteredServiceAuthenticationPolicy();
authenticationPolicy.setRequiredAuthenticationHandlers(Set.of("DelegatedClientAuthenticationHandler"));
authenticationPolicy.setCriteria(new AllAuthenticationHandlersRegisteredServiceAuthenticationPolicyCriteria());
registeredService.setAuthenticationPolicy(authenticationPolicy);
servicesManager.save(registeredService);
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val tgt = new MockTicketGrantingTicket("casuser");
centralAuthenticationService.addTicket(tgt);
WebUtils.putTicketGrantingTicketInScopes(context, tgt);
assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, delegatedAuthenticationAction.execute(context).getId());
assertThrows(InvalidTicketException.class, () -> centralAuthenticationService.getTicket(tgt.getId()));
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method assertStartAuthentication.
@SneakyThrows
private void assertStartAuthentication(final Service service) {
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
val flow = new Flow("mockFlow");
flow.addVariable(new FlowVariable("credential", new BeanFactoryVariableValueFactory(UsernamePasswordCredential.class, applicationContext.getAutowireCapableBeanFactory())));
val locale = Locale.ENGLISH.getLanguage();
request.setParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, "theme");
LOGGER.debug("Setting locale [{}] for request parameter as [{}]", locale, request.getParameterMap());
request.setParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, locale);
request.setParameter(CasProtocolConstants.PARAMETER_METHOD, HttpMethod.POST.name());
LOGGER.debug("Set request parameters as [{}]", request.getParameterMap());
val requestContext = new MockRequestContext();
requestContext.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
RequestContextHolder.setRequestContext(requestContext);
ExternalContextHolder.setExternalContext(requestContext.getExternalContext());
val mockExecutionContext = new MockFlowExecutionContext(new MockFlowSession(flow));
requestContext.setFlowExecutionContext(mockExecutionContext);
if (service != null) {
WebUtils.putServiceIntoFlowScope(requestContext, service);
}
val client = builtClients.findClient("SAML2Client").get();
val webContext = new JEEContext(request, response);
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
LOGGER.debug("Initializing action with request parameters [{}]", webContext.getRequestParameters());
val event = delegatedAuthenticationAction.execute(requestContext);
assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, event.getId());
delegatedClientAuthenticationWebflowManager.retrieve(requestContext, webContext, client);
assertEquals("theme", request.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(locale, request.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(HttpMethod.POST.name(), request.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
val urls = (Set<DelegatedClientIdentityProviderConfiguration>) WebUtils.getDelegatedAuthenticationProviderConfigurations(requestContext);
assertFalse(urls.isEmpty());
assertSame(4, urls.size());
urls.stream().map(url -> {
LOGGER.debug("Redirect URL [{}]", url.getRedirectUrl());
return UriComponentsBuilder.fromUriString(url.getRedirectUrl()).build();
}).forEach(uriComponents -> {
assertEquals(DelegatedClientIdentityProviderConfigurationFactory.ENDPOINT_URL_REDIRECT, uriComponents.getPath());
val clientName = uriComponents.getQueryParams().get(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER);
assertEquals(1, clientName.size());
val serviceName = uriComponents.getQueryParams().get(CasProtocolConstants.PARAMETER_SERVICE);
if (service != null) {
assertEquals(1, serviceName.size());
assertTrue(serviceName.contains(EncodingUtils.urlEncode(RegisteredServiceTestUtils.CONST_TEST_URL)));
} else {
assertNull(serviceName);
}
val method = uriComponents.getQueryParams().get(CasProtocolConstants.PARAMETER_METHOD);
assertEquals(1, method.size());
assertTrue(method.contains(HttpMethod.POST.toString()));
val theme = uriComponents.getQueryParams().get(ThemeChangeInterceptor.DEFAULT_PARAM_NAME);
assertEquals(1, theme.size());
assertTrue(theme.contains("theme"));
val testLocale = uriComponents.getQueryParams().get(LocaleChangeInterceptor.DEFAULT_PARAM_NAME);
assertEquals(1, testLocale.size());
assertTrue(testLocale.contains(locale));
});
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifyExecutionFailureWithUnauthzResponse.
@Test
public void verifyExecutionFailureWithUnauthzResponse() throws Exception {
val request = new MockHttpServletRequest();
val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId()));
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
val client = builtClients.findClient("FacebookClient").get();
var response = new MockHttpServletResponse();
response.setStatus(HttpStatus.UNAUTHORIZED.value());
val webContext = new JEEContext(request, response);
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
val context = new MockRequestContext();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val event = delegatedAuthenticationAction.execute(context);
assertEquals(CasWebflowConstants.TRANSITION_ID_STOP, event.getId());
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifySsoAuthentication.
@Test
public void verifySsoAuthentication() throws Exception {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
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());
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
val service = CoreAuthenticationTestUtils.getService("https://delegated2.example.org");
servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val tgt = new MockTicketGrantingTicket("casuser");
centralAuthenticationService.addTicket(tgt);
WebUtils.putTicketGrantingTicketInScopes(context, tgt);
assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, delegatedAuthenticationAction.execute(context).getId());
}
Aggregations