Search in sources :

Example 41 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class WebUtilsTests method verifyOperation.

@Test
public void verifyOperation() {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    val flow = new Flow("mockFlow");
    val flowSession = new MockFlowSession(flow);
    flowSession.setParent(new MockFlowSession(flow));
    val mockExecutionContext = new MockFlowExecutionContext(flowSession);
    context.setFlowExecutionContext(mockExecutionContext);
    WebUtils.putLogoutRedirectUrl(context, URL);
    assertNotNull(WebUtils.getLogoutRedirectUrl(context, String.class));
    WebUtils.removeLogoutRedirectUrl(context);
    assertNull(WebUtils.getLogoutRedirectUrl(context, String.class));
    assertNull(WebUtils.getHttpServletRequestUserAgentFromRequestContext(context));
    assertNull(WebUtils.getHttpServletRequestUserAgentFromRequestContext(request));
    assertNull(WebUtils.getAuthenticationResult(context));
    assertNull(WebUtils.getHttpServletRequestGeoLocationFromRequestContext());
    assertNull(WebUtils.getAcceptableUsagePolicyTermsFromFlowScope(context, Object.class));
    assertFalse(WebUtils.hasSurrogateAuthenticationRequest(context));
    assertNotNull(WebUtils.produceUnauthorizedErrorView(new RuntimeException()));
    assertNotNull(WebUtils.produceErrorView(new IllegalArgumentException()));
    assertNotNull(WebUtils.produceErrorView("error-view", new IllegalArgumentException()));
    assertNotNull(WebUtils.getHttpRequestFullUrl(context));
    request.setQueryString("param=value");
    assertNotNull(WebUtils.getHttpRequestFullUrl(request));
    assertFalse(WebUtils.isGraphicalUserAuthenticationEnabled(context));
    assertTrue(WebUtils.getDelegatedAuthenticationProviderConfigurations(context).isEmpty());
    assertNull(WebUtils.getAvailableAuthenticationHandleNames(context));
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() {
            WebUtils.putYubiKeyMultipleDeviceRegistrationEnabled(context, true);
            WebUtils.putInitialHttpRequestPostParameters(context);
            WebUtils.putExistingSingleSignOnSessionAvailable(context, true);
            WebUtils.putExistingSingleSignOnSessionPrincipal(context, CoreAuthenticationTestUtils.getPrincipal());
            WebUtils.putAvailableAuthenticationHandleNames(context, List.of());
            WebUtils.putPasswordManagementEnabled(context, true);
            WebUtils.putRecaptchaPropertiesFlowScope(context, new GoogleRecaptchaProperties().setEnabled(true));
            WebUtils.putLogoutUrls(context, Map.of());
            val ac = OneTimeTokenAccount.builder().validationCode(123456).username("casuser").name("Example").build();
            WebUtils.putOneTimeTokenAccount(context, ac);
            assertNotNull(WebUtils.getOneTimeTokenAccount(context, OneTimeTokenAccount.class));
            WebUtils.putOneTimeTokenAccounts(context, List.of(ac));
            WebUtils.putWarnCookieIfRequestParameterPresent(null, context);
            WebUtils.putTicketGrantingTicketInScopes(context, "ticket-id");
        }
    });
    WebUtils.putCredential(context, new UsernamePasswordCredential("casuser", "password"));
    assertThrows(ClassCastException.class, () -> WebUtils.getCredential(context, OneTimeTokenCredential.class));
    WebUtils.putTicketGrantingTicketInScopes(context, StringUtils.EMPTY);
    WebUtils.putTicketGrantingTicketInScopes(context, (TicketGrantingTicket) null);
    WebUtils.putTicketGrantingTicketInScopes(context, (String) null);
    assertNull(WebUtils.getTicketGrantingTicket(context));
    assertThrows(IllegalArgumentException.class, () -> WebUtils.getPrincipalFromRequestContext(context, null));
    request.addParameter(WebUtils.PUBLIC_WORKSTATION_ATTRIBUTE, "true");
    WebUtils.putPublicWorkstationToFlowIfRequestParameterPresent(context);
    assertTrue(WebUtils.isAuthenticatingAtPublicWorkstation(context));
    val ticketRegistrySupport = mock(TicketRegistrySupport.class);
    WebUtils.putTicketGrantingTicketInScopes(context, "TGT-XYZ123");
    assertNull(WebUtils.getPrincipalFromRequestContext(context, ticketRegistrySupport));
    WebUtils.putLogoutPostUrl(context, URL);
    assertEquals(URL, WebUtils.getLogoutPostUrl(context));
    val data = new HashMap<String, Object>();
    data.put("SAMLResponse", "xxx");
    WebUtils.putLogoutPostData(context, data);
    assertEquals(data, WebUtils.getLogoutPostData(context));
}
Also used : lombok.val(lombok.val) MockFlowSession(org.springframework.webflow.test.MockFlowSession) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.apereo.cas.util.MockServletContext) OneTimeTokenCredential(org.apereo.cas.authentication.credential.OneTimeTokenCredential) Flow(org.springframework.webflow.engine.Flow) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) GoogleRecaptchaProperties(org.apereo.cas.configuration.model.support.captcha.GoogleRecaptchaProperties) Executable(org.junit.jupiter.api.function.Executable) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 42 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class BasicAuthenticationAction method constructCredentialsFromRequest.

@Override
protected Credential constructCredentialsFromRequest(final RequestContext requestContext) {
    try {
        val request = WebUtils.getHttpServletRequestFromExternalWebflowContext(requestContext);
        val response = WebUtils.getHttpServletResponseFromExternalWebflowContext(requestContext);
        val extractor = new BasicAuthExtractor();
        val webContext = new JEEContext(request, response);
        val credentialsResult = extractor.extract(webContext, JEESessionStore.INSTANCE);
        if (credentialsResult.isPresent()) {
            val credentials = (UsernamePasswordCredentials) credentialsResult.get();
            LOGGER.debug("Received basic authentication request from credentials [{}]", credentials);
            return new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
        }
    } catch (final Exception e) {
        LoggingUtils.warn(LOGGER, e);
    }
    return null;
}
Also used : lombok.val(lombok.val) BasicAuthExtractor(org.pac4j.core.credentials.extractor.BasicAuthExtractor) JEEContext(org.pac4j.core.context.JEEContext) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 43 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class CasKryoTranscoderTests method verifyEncodeDecodeTGTWithLinkedHashMap.

@Test
public void verifyEncodeDecodeTGTWithLinkedHashMap() {
    val userPassCredential = new UsernamePasswordCredential(USERNAME, PASSWORD);
    val expectedTGT = new MockTicketGrantingTicket(TGT_ID, userPassCredential, new LinkedHashMap<>(this.principalAttributes));
    expectedTGT.grantServiceTicket(ST_ID, null, null, false, true);
    val result = transcoder.encode(expectedTGT);
    assertEquals(expectedTGT, transcoder.decode(result));
    assertEquals(expectedTGT, transcoder.decode(result));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 44 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class CasKryoTranscoderTests method verifyEncodeDecodeTGTWithUnmodifiableMap.

@Test
public void verifyEncodeDecodeTGTWithUnmodifiableMap() {
    val userPassCredential = new UsernamePasswordCredential(USERNAME, PASSWORD);
    val expectedTGT = new MockTicketGrantingTicket(TGT_ID, userPassCredential, new HashMap<>(this.principalAttributes));
    expectedTGT.grantServiceTicket(ST_ID, null, null, false, true);
    val result = transcoder.encode(expectedTGT);
    assertEquals(expectedTGT, transcoder.decode(result));
    assertEquals(expectedTGT, transcoder.decode(result));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 45 with UsernamePasswordCredential

use of org.apereo.cas.authentication.credential.UsernamePasswordCredential in project cas by apereo.

the class CasKryoTranscoderTests method verifyEncodeDecodeTGTWithListOrderedMap.

@Test
public void verifyEncodeDecodeTGTWithListOrderedMap() {
    val userPassCredential = new UsernamePasswordCredential(USERNAME, PASSWORD);
    val expectedTGT = new MockTicketGrantingTicket(TGT_ID, userPassCredential, this.principalAttributes);
    expectedTGT.grantServiceTicket(ST_ID, null, null, false, true);
    val result = transcoder.encode(expectedTGT);
    assertEquals(expectedTGT, transcoder.decode(result));
    assertEquals(expectedTGT, transcoder.decode(result));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

lombok.val (lombok.val)111 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)111 Test (org.junit.jupiter.api.Test)74 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)12 HashMap (java.util.HashMap)8 Map (java.util.Map)8 BasicCredentialMetaData (org.apereo.cas.authentication.metadata.BasicCredentialMetaData)8 LinkedHashMap (java.util.LinkedHashMap)7 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)7 Executable (org.junit.jupiter.api.function.Executable)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)6 MockRequestContext (org.springframework.webflow.test.MockRequestContext)6 ArrayList (java.util.ArrayList)5 ClassPathResource (org.springframework.core.io.ClassPathResource)5 MockServletContext (org.springframework.mock.web.MockServletContext)5 FailedLoginException (javax.security.auth.login.FailedLoginException)4 SurrogateUsernamePasswordCredential (org.apereo.cas.authentication.SurrogateUsernamePasswordCredential)4