Search in sources :

Example 6 with SurrogateUsernamePasswordCredential

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

the class SurrogateInitialAuthenticationAction method convertToUsernamePasswordCredential.

private static void convertToUsernamePasswordCredential(final RequestContext context, final UsernamePasswordCredential up) throws Exception {
    if (up instanceof SurrogateUsernamePasswordCredential) {
        val sc = new UsernamePasswordCredential();
        BeanUtils.copyProperties(sc, up);
        WebUtils.putCredential(context, sc);
    }
}
Also used : lombok.val(lombok.val) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)

Example 7 with SurrogateUsernamePasswordCredential

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

the class SurrogateInitialAuthenticationAction method convertToSurrogateCredential.

private void convertToSurrogateCredential(final RequestContext context, final UsernamePasswordCredential up) {
    val tUsername = up.getUsername();
    val surrogateUsername = tUsername.substring(0, tUsername.indexOf(this.separator));
    val realUsername = tUsername.substring(tUsername.indexOf(this.separator) + this.separator.length());
    LOGGER.debug("Converting to surrogate credential for username [{}], surrogate username [{}]", realUsername, surrogateUsername);
    if (StringUtils.isBlank(surrogateUsername)) {
        up.setUsername(realUsername);
        WebUtils.putSurrogateAuthenticationRequest(context, Boolean.TRUE);
        WebUtils.putCredential(context, up);
        LOGGER.debug("No surrogate username is defined; Signal webflow to request for surrogate credentials");
        return;
    }
    val sc = new SurrogateUsernamePasswordCredential();
    sc.setUsername(realUsername);
    sc.setSurrogateUsername(surrogateUsername);
    sc.setPassword(up.getPassword());
    if (up instanceof RememberMeCredential) {
        sc.setRememberMe(((RememberMeCredential) up).isRememberMe());
    }
    WebUtils.putSurrogateAuthenticationRequest(context, Boolean.FALSE);
    LOGGER.debug("Converted credential to surrogate for username [{}] and assigned it to webflow", realUsername);
    WebUtils.putCredential(context, sc);
}
Also used : lombok.val(lombok.val) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) RememberMeCredential(org.apereo.cas.authentication.RememberMeCredential)

Example 8 with SurrogateUsernamePasswordCredential

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

the class SurrogateAuthenticationRestHttpRequestCredentialFactory method extractCredential.

/**
 * Extract credential surrogate username password.
 *
 * @param request     the request
 * @param credentials the credentials
 * @return the surrogate username password credential
 * @throws Exception the exception
 */
protected SurrogateUsernamePasswordCredential extractCredential(final HttpServletRequest request, final List<Credential> credentials) throws Exception {
    val sc = new SurrogateUsernamePasswordCredential();
    val credential = UsernamePasswordCredential.class.cast(credentials.get(0));
    BeanUtils.copyProperties(sc, credential);
    val surrogatePrincipal = request.getHeader(REQUEST_HEADER_SURROGATE_PRINCIPAL);
    if (StringUtils.isNotBlank(surrogatePrincipal)) {
        LOGGER.debug("Request surrogate principal [{}]", surrogatePrincipal);
        sc.setSurrogateUsername(surrogatePrincipal);
        return sc;
    }
    val username = credential.getUsername();
    if (username.contains(properties.getSeparator())) {
        val surrogateUsername = username.substring(0, username.indexOf(properties.getSeparator()));
        val realUsername = username.substring(username.indexOf(properties.getSeparator()) + properties.getSeparator().length());
        sc.setUsername(realUsername);
        sc.setSurrogateUsername(surrogateUsername);
        sc.setPassword(credential.getPassword());
        return sc;
    }
    return null;
}
Also used : lombok.val(lombok.val) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)

Example 9 with SurrogateUsernamePasswordCredential

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

the class SurrogateAuthenticationRestHttpRequestCredentialFactoryTests method verifyBasicUsernamePasswordOperationWithoutSurrogatePrincipal.

@Test
public void verifyBasicUsernamePasswordOperationWithoutSurrogatePrincipal() {
    val request = new MockHttpServletRequest();
    val requestBody = new LinkedMultiValueMap<String, String>();
    requestBody.add("username", "test");
    requestBody.add("password", "password");
    val service = new SimpleSurrogateAuthenticationService(Collections.emptyMap(), mock(ServicesManager.class));
    val factory = new SurrogateAuthenticationRestHttpRequestCredentialFactory(service, casProperties.getAuthn().getSurrogate());
    val results = factory.fromRequest(request, requestBody);
    assertFalse(results.isEmpty());
    assertFalse(results.get(0) instanceof SurrogateUsernamePasswordCredential);
    assertTrue(results.get(0) instanceof UsernamePasswordCredential);
    val credential = (UsernamePasswordCredential) results.get(0);
    assertNotNull(credential);
    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) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) SurrogateUsernamePasswordCredential(org.apereo.cas.authentication.SurrogateUsernamePasswordCredential) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 10 with SurrogateUsernamePasswordCredential

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

the class SurrogateAuthenticationRestHttpRequestCredentialFactoryTests method verifyOperationByHeader.

@Test
public void verifyOperationByHeader() {
    val request = new MockHttpServletRequest();
    val requestBody = new LinkedMultiValueMap<String, String>();
    request.addHeader(SurrogateAuthenticationRestHttpRequestCredentialFactory.REQUEST_HEADER_SURROGATE_PRINCIPAL, "surrogate");
    requestBody.add("username", "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());
    assertTrue(factory.getOrder() > 0);
    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)

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