Search in sources :

Example 1 with SurrogateUsernamePasswordCredential

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

the class SurrogateInitialAuthenticationAction method deconvertFromSurrogatePrincipal.

private static void deconvertFromSurrogatePrincipal(final RequestContext context) {
    final Credential c = WebUtils.getCredential(context);
    if (c instanceof SurrogateUsernamePasswordCredential) {
        final SurrogateUsernamePasswordCredential sc = SurrogateUsernamePasswordCredential.class.cast(c);
        final UsernamePasswordCredential up = new UsernamePasswordCredential();
        up.setUsername(sc.getUsername());
        up.setPassword(sc.getPassword());
        WebUtils.putCredential(context, up);
    }
}
Also used : RememberMeCredential(org.apereo.cas.authentication.RememberMeCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Credential(org.apereo.cas.authentication.Credential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)

Example 2 with SurrogateUsernamePasswordCredential

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

the class SurrogateAuthenticationRestHttpRequestCredentialFactoryTests method verifyOperationByCredentialSeparator.

@Test
public void verifyOperationByCredentialSeparator() {
    val request = new MockHttpServletRequest();
    val requestBody = new LinkedMultiValueMap<String, String>();
    requestBody.add("username", "surrogate+test");
    requestBody.add("password", "password");
    val service = new SimpleSurrogateAuthenticationService(Map.of("test", List.of("surrogate")), mock(ServicesManager.class));
    val factory = new SurrogateAuthenticationRestHttpRequestCredentialFactory(service, casProperties.getAuthn().getSurrogate());
    val results = factory.fromRequest(request, requestBody);
    assertFalse(results.isEmpty());
    val credential = (SurrogateUsernamePasswordCredential) results.get(0);
    assertNotNull(credential);
    assertEquals("surrogate", credential.getSurrogateUsername());
    assertEquals("test", credential.getUsername());
}
Also used : lombok.val(lombok.val) ServicesManager(org.apereo.cas.services.ServicesManager) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SimpleSurrogateAuthenticationService(org.apereo.cas.authentication.surrogate.SimpleSurrogateAuthenticationService) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with SurrogateUsernamePasswordCredential

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

the class LoadSurrogatesListAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    try {
        if (WebUtils.hasSurrogateAuthenticationRequest(requestContext)) {
            WebUtils.removeSurrogateAuthenticationRequest(requestContext);
            LOGGER.trace("Attempting to load surrogates...");
            if (loadSurrogates(requestContext)) {
                return new Event(this, CasWebflowConstants.TRANSITION_ID_SURROGATE_VIEW);
            }
            return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_SKIP_SURROGATE);
        }
        val currentCredential = WebUtils.getCredential(requestContext);
        if (currentCredential instanceof SurrogateUsernamePasswordCredential) {
            val authenticationResultBuilder = WebUtils.getAuthenticationResultBuilder(requestContext);
            val credential = (SurrogateUsernamePasswordCredential) currentCredential;
            val registeredService = WebUtils.getRegisteredService(requestContext);
            val result = surrogatePrincipalBuilder.buildSurrogateAuthenticationResult(authenticationResultBuilder, currentCredential, credential.getSurrogateUsername(), registeredService);
            result.ifPresent(builder -> WebUtils.putAuthenticationResultBuilder(builder, requestContext));
        }
        return success();
    } catch (final Exception e) {
        requestContext.getMessageContext().addMessage(new MessageBuilder().error().source("surrogate").code("screen.surrogates.account.selection.error").defaultText("Unable to accept or authorize selection").build());
        LoggingUtils.error(LOGGER, e);
        return error(e);
    }
}
Also used : lombok.val(lombok.val) MessageBuilder(org.springframework.binding.message.MessageBuilder) Event(org.springframework.webflow.execution.Event) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)

Example 4 with SurrogateUsernamePasswordCredential

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

the class LoadSurrogatesListActionTests method verifySkipAuthenticate.

@Test
public void verifySkipAuthenticate() throws Exception {
    val context = new MockRequestContext();
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    WebUtils.putSurrogateAuthenticationRequest(context, Boolean.TRUE);
    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("someuser", 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("someuser");
    creds.setSurrogateUsername("others");
    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_SKIP_SURROGATE, 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 5 with SurrogateUsernamePasswordCredential

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

the class LoadSurrogatesListActionTests method verifyAuthenticate.

@Test
public void verifyAuthenticate() 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("cassurrogate");
    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_SUCCESS, 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)

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