use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.
the class SamlIdPServicesManagerRegisteredServiceLocatorTests method verifyWithSelectionStrategy.
@Test
public void verifyWithSelectionStrategy() {
val prefix = "http://localhost:8443/cas";
val callbackUrl = prefix + SamlIdPConstants.ENDPOINT_SAML2_SSO_PROFILE_CALLBACK;
val service0 = RegisteredServiceTestUtils.getRegisteredService(callbackUrl + ".*");
service0.setEvaluationOrder(0);
val service1 = getSamlRegisteredServiceFor("https://sp.testshib.org/shibboleth-sp");
service1.setEvaluationOrder(100);
val service2 = getSamlRegisteredServiceFor(".+");
service2.setMetadataLocation("https://example.org");
service2.setEvaluationOrder(1000);
val candidateServices = CollectionUtils.wrapList(service0, service1, service2);
servicesManager.save(candidateServices.toArray(new RegisteredService[0]));
Collections.sort(candidateServices);
val url = new URIBuilder(callbackUrl + "?entityId=https%3A%2F%2Fsp.testshib.org%2Fshibboleth-sp");
val request = new MockHttpServletRequest();
request.setRequestURI(callbackUrl);
url.getQueryParams().forEach(param -> request.addParameter(param.getName(), param.getValue()));
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
val service = webApplicationServiceFactory.createService(url.toString());
assertFalse(service.getAttributes().isEmpty());
val selectionStrategy = new SamlIdPEntityIdAuthenticationServiceSelectionStrategy(servicesManager, webApplicationServiceFactory, prefix);
val extracted = selectionStrategy.resolveServiceFrom(service);
assertNotNull(extracted);
assertFalse(extracted.getAttributes().isEmpty());
val result = servicesManager.findServiceBy(extracted);
assertEquals(service1, result);
}
use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.
the class BaseEndpointLdapAuthenticationProviderTests method init.
@BeforeEach
public void init() {
val request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
}
use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.
the class UsernamePasswordWrapperAuthenticationHandlerTests method verifyAuthWithWithPrincipalAttr.
@Test
public void verifyAuthWithWithPrincipalAttr() throws Exception {
val request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
val handler = new UsernamePasswordWrapperAuthenticationHandler("Handler1", mock(ServicesManager.class), PrincipalFactoryUtils.newPrincipalFactory(), 0, JEESessionStore.INSTANCE);
handler.setProfileCreator((credentials, webContext, sessionStore) -> {
val profile = credentials.getUserProfile();
profile.addAttribute("givenName", List.of("cas-person"));
return Optional.of(profile);
});
handler.setPrincipalAttributeId("givenName");
val result = handler.authenticate(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser"));
assertEquals("cas-person", result.getPrincipal().getId());
}
use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.
the class UsernamePasswordWrapperAuthenticationHandlerTests method verifyAuthWithWithClientPrincipalAttrTyped.
@Test
public void verifyAuthWithWithClientPrincipalAttrTyped() throws Exception {
val request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
val handler = new UsernamePasswordWrapperAuthenticationHandler("Handler1", mock(ServicesManager.class), PrincipalFactoryUtils.newPrincipalFactory(), 0, JEESessionStore.INSTANCE) {
@Override
protected String determinePrincipalIdFrom(final UserProfile profile, final BaseClient client) {
val mockClient = mock(BaseClient.class);
when(mockClient.getCustomProperties()).thenReturn(Map.of(ClientCustomPropertyConstants.CLIENT_CUSTOM_PROPERTY_PRINCIPAL_ATTRIBUTE_ID, "givenName"));
return super.determinePrincipalIdFrom(profile, mockClient);
}
};
handler.setProfileCreator((credentials, webContext, sessionStore) -> {
val profile = credentials.getUserProfile();
profile.addAttribute("givenName", List.of("cas-person"));
return Optional.of(profile);
});
handler.setTypedIdUsed(true);
val result = handler.authenticate(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser"));
assertEquals("org.pac4j.core.profile.CommonProfile#cas-person", result.getPrincipal().getId());
}
use of org.springframework.web.context.request.ServletRequestAttributes in project cas by apereo.
the class RestfulUrlTemplateResolverTests method verifyUnknownErrorAction.
@Test
public void verifyUnknownErrorAction() {
val request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, new MockHttpServletResponse()));
try (val webServer = new MockWebServer(9302, new ByteArrayResource("template".getBytes(StandardCharsets.UTF_8), "REST Output"), HttpStatus.NO_CONTENT)) {
webServer.start();
val props = new CasConfigurationProperties();
props.getView().getRest().setUrl("http://localhost:9302");
val r = new RestfulUrlTemplateResolver(props, new FixedThemeResolver());
val res = r.resolveTemplate(mock(IEngineConfiguration.class), "cas", "template", new LinkedHashMap<>());
assertNotNull(res);
}
}
Aggregations