Search in sources :

Example 71 with ServletRequestAttributes

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);
}
Also used : lombok.val(lombok.val) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) URIBuilder(org.jasig.cas.client.util.URIBuilder) Test(org.junit.jupiter.api.Test)

Example 72 with ServletRequestAttributes

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()));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 73 with ServletRequestAttributes

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());
}
Also used : lombok.val(lombok.val) ServicesManager(org.apereo.cas.services.ServicesManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 74 with ServletRequestAttributes

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());
}
Also used : lombok.val(lombok.val) ServicesManager(org.apereo.cas.services.ServicesManager) UserProfile(org.pac4j.core.profile.UserProfile) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) BaseClient(org.pac4j.core.client.BaseClient) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 75 with ServletRequestAttributes

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);
    }
}
Also used : lombok.val(lombok.val) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MockWebServer(org.apereo.cas.util.MockWebServer) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)204 HttpServletRequest (javax.servlet.http.HttpServletRequest)92 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)62 RequestAttributes (org.springframework.web.context.request.RequestAttributes)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 Test (org.junit.jupiter.api.Test)28 lombok.val (lombok.val)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 Test (org.junit.Test)18 AuthChecker (com.code.server.login.anotation.AuthChecker)17 HttpSession (javax.servlet.http.HttpSession)12 Before (org.junit.Before)12 HashMap (java.util.HashMap)11 Method (java.lang.reflect.Method)8 Date (java.util.Date)7 AbstractTracingSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan)7 TraceSegment (org.apache.skywalking.apm.agent.core.context.trace.TraceSegment)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)6 Map (java.util.Map)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6