Search in sources :

Example 11 with SurrogateUsernamePasswordCredential

use of org.apereo.cas.authentication.SurrogateUsernamePasswordCredential in project cas by apereo.

the class LoadSurrogatesListActionTests method verifyAuthenticateNotAuthorized.

@Test
public void verifyAuthenticateNotAuthorized() throws Exception {
    val context = new MockRequestContext();
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    val attributes = new LinkedHashMap<String, List<Object>>();
    attributes.put(SurrogateAuthenticationService.AUTHENTICATION_ATTR_SURROGATE_ENABLED, List.of(true));
    attributes.putAll(CoreAuthenticationTestUtils.getAttributeRepository().getBackingMap());
    val p = CoreAuthenticationTestUtils.getPrincipal("casuser", attributes);
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(p), context);
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    val creds = new SurrogateUsernamePasswordCredential();
    creds.setPassword("Mellon");
    creds.setUsername("casuser");
    creds.setSurrogateUsername("unknown-user");
    WebUtils.putCredential(context, creds);
    val builder = mock(AuthenticationResultBuilder.class);
    when(builder.getInitialAuthentication()).thenReturn(Optional.of(CoreAuthenticationTestUtils.getAuthentication("casuser")));
    when(builder.collect(any(Authentication.class))).thenReturn(builder);
    WebUtils.putAuthenticationResultBuilder(builder, context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, loadSurrogatesListAction.execute(context).getId());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Authentication(org.apereo.cas.authentication.Authentication) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

Example 12 with SurrogateUsernamePasswordCredential

use of org.apereo.cas.authentication.SurrogateUsernamePasswordCredential in project cas by apereo.

the class SurrogateInitialAuthenticationActionTests method verifySurrogateCredentialsFound.

@Test
public void verifySurrogateCredentialsFound() {
    try {
        val context = new MockRequestContext();
        val c = new SurrogateUsernamePasswordCredential();
        c.setUsername("casuser");
        c.setPassword("Mellon");
        c.setSurrogateUsername("cassurrogate");
        WebUtils.putCredential(context, c);
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), new MockHttpServletRequest(), new MockHttpServletResponse()));
        assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, authenticationViaFormAction.execute(context).getId());
    } catch (final Exception e) {
        throw new AssertionError(e);
    }
}
Also used : lombok.val(lombok.val) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

Example 13 with SurrogateUsernamePasswordCredential

use of org.apereo.cas.authentication.SurrogateUsernamePasswordCredential in project cas by apereo.

the class SurrogateInitialAuthenticationActionTests method verifyUsernamePasswordCredentialsBadPasswordAndCancelled.

@Test
public void verifyUsernamePasswordCredentialsBadPasswordAndCancelled() {
    try {
        val context = new MockRequestContext();
        var credential = new UsernamePasswordCredential();
        credential.setUsername("cassurrogate+casuser");
        credential.setPassword("badpassword");
        WebUtils.putCredential(context, credential);
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), new MockHttpServletRequest(), new MockHttpServletResponse()));
        assertEquals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, authenticationViaFormAction.execute(context).getId());
        assertTrue(WebUtils.getCredential(context) instanceof SurrogateUsernamePasswordCredential);
        val sc = WebUtils.getCredential(context, SurrogateUsernamePasswordCredential.class);
        sc.setUsername("casuser");
        sc.setPassword("Mellon");
        WebUtils.putCredential(context, sc);
        assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, authenticationViaFormAction.execute(context).getId());
        assertTrue(WebUtils.getCredential(context) instanceof UsernamePasswordCredential);
    } catch (final Exception e) {
        throw new AssertionError(e);
    }
}
Also used : lombok.val(lombok.val) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockRequestContext(org.springframework.webflow.test.MockRequestContext) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) Test(org.junit.jupiter.api.Test)

Aggregations

SurrogateUsernamePasswordCredential (org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)13 lombok.val (lombok.val)12 Test (org.junit.jupiter.api.Test)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 MockServletContext (org.springframework.mock.web.MockServletContext)5 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)5 MockRequestContext (org.springframework.webflow.test.MockRequestContext)5 LinkedHashMap (java.util.LinkedHashMap)3 Authentication (org.apereo.cas.authentication.Authentication)3 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)3 SimpleSurrogateAuthenticationService (org.apereo.cas.authentication.surrogate.SimpleSurrogateAuthenticationService)3 ServicesManager (org.apereo.cas.services.ServicesManager)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 RememberMeCredential (org.apereo.cas.authentication.RememberMeCredential)2 Credential (org.apereo.cas.authentication.Credential)1 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)1 MessageBuilder (org.springframework.binding.message.MessageBuilder)1 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)1