Search in sources :

Example 11 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class AccessTokenAuthorizationCodeGrantRequestExtractorTests method verifyNoClientIdOrRedirectUri.

@Test
public void verifyNoClientIdOrRedirectUri() throws Exception {
    val request = new MockHttpServletRequest();
    request.addParameter(OAuth20Constants.GRANT_TYPE, OAuth20GrantTypes.AUTHORIZATION_CODE.getType());
    val service = getRegisteredService(REDIRECT_URI, CLIENT_ID, CLIENT_SECRET);
    val principal = RegisteredServiceTestUtils.getPrincipal();
    val code = addCode(principal, service);
    ticketRegistry.addTicket(code.getTicketGrantingTicket());
    request.addParameter(OAuth20Constants.CODE, code.getId());
    val response = new MockHttpServletResponse();
    val extractor = new AccessTokenAuthorizationCodeGrantRequestExtractor(oauth20ConfigurationContext);
    val context = new JEEContext(request, response);
    assertThrows(UnauthorizedServiceException.class, () -> extractor.extract(context));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 12 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class AccepttoMultifactorValidateChannelActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val httpRequest = new MockHttpServletRequest();
    httpRequest.setRemoteAddr("185.86.151.11");
    httpRequest.setLocalAddr("185.88.151.11");
    ClientInfoHolder.setClientInfo(new ClientInfo(httpRequest));
    val data = MAPPER.writeValueAsString(CollectionUtils.wrap("channel", "test-channel", "status", "approved", "device_id", "deviceid-123456"));
    try (val webServer = new MockWebServer(5001, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.OK)) {
        webServer.start();
        val action = new AccepttoMultifactorValidateChannelAction(mfaAccepttoDistributedSessionStore, authenticationSystemSupport);
        val context = new MockRequestContext();
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();
        val webContext = new JEEContext(request, response);
        context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
        val authn = CoreAuthenticationTestUtils.getAuthentication("casuser");
        WebUtils.putAuthentication(authn, context);
        AccepttoWebflowUtils.storeChannelInSessionStore("test-channel", webContext, mfaAccepttoDistributedSessionStore);
        AccepttoWebflowUtils.storeAuthenticationInSessionStore(authn, webContext, mfaAccepttoDistributedSessionStore);
        RequestContextHolder.setRequestContext(context);
        val result = action.doExecute(context);
        assertEquals(CasWebflowConstants.TRANSITION_ID_FINALIZE, result.getId());
    }
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockWebServer(org.apereo.cas.util.MockWebServer) JEEContext(org.pac4j.core.context.JEEContext) ClientInfo(org.apereo.inspektr.common.web.ClientInfo) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 13 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class AccepttoMultifactorValidateChannelAction method doExecute.

@Override
protected Event doExecute(final RequestContext requestContext) {
    val eventAttributes = new LocalAttributeMap<>();
    try {
        val request = WebUtils.getHttpServletRequestFromExternalWebflowContext(requestContext);
        val response = WebUtils.getHttpServletResponseFromExternalWebflowContext(requestContext);
        val webContext = new JEEContext(request, response);
        val channel = AccepttoWebflowUtils.getChannel(webContext, sessionStore);
        if (channel.isEmpty()) {
            LOGGER.debug("Unable to determine channel from session store; not a validation attempt");
            return null;
        }
        val authentication = AccepttoWebflowUtils.getAuthentication(webContext, sessionStore);
        if (authentication == null) {
            LOGGER.debug("Unable to determine the original authentication attempt the session store");
            throw new AuthenticationException("Unable to determine authentication from session store");
        }
        WebUtils.putAuthentication(authentication, requestContext);
        val credential = new AccepttoMultifactorTokenCredential(channel.toString());
        val service = WebUtils.getService(requestContext);
        LOGGER.debug("Cleaning up session store to remove [{}]", credential);
        AccepttoWebflowUtils.resetChannelAndAuthentication(webContext, sessionStore);
        AccepttoWebflowUtils.setChannel(requestContext, null);
        LOGGER.debug("Attempting to authenticate channel [{}] with authentication [{}] and service [{}]", credential, authentication, service);
        var resultBuilder = authenticationSystemSupport.establishAuthenticationContextFromInitial(authentication);
        resultBuilder = authenticationSystemSupport.handleAuthenticationTransaction(service, resultBuilder, credential);
        WebUtils.putAuthenticationResultBuilder(resultBuilder, requestContext);
        return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_FINALIZE);
    } catch (final Exception e) {
        eventAttributes.put("error", e);
        LoggingUtils.error(LOGGER, e);
    }
    return new EventFactorySupport().event(this, CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE, eventAttributes);
}
Also used : lombok.val(lombok.val) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) AuthenticationException(org.apereo.cas.authentication.AuthenticationException) JEEContext(org.pac4j.core.context.JEEContext) AccepttoMultifactorTokenCredential(org.apereo.cas.mfa.accepto.AccepttoMultifactorTokenCredential) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) AuthenticationException(org.apereo.cas.authentication.AuthenticationException)

Example 14 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OidcAccessTokenResponseGeneratorTests method verifyAccessTokenResponseForDeviceCode.

@Test
public void verifyAccessTokenResponseForDeviceCode() {
    val devCode = deviceTokenFactory.createDeviceCode(RegisteredServiceTestUtils.getService());
    val token = OAuth20TokenGeneratedResult.builder().registeredService(getOidcRegisteredService()).responseType(OAuth20ResponseTypes.DEVICE_CODE).deviceCode(devCode.getId()).userCode(deviceUserCodeFactory.createDeviceUserCode(devCode).getId()).build();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val context = new JEEContext(request, response);
    val manager = new ProfileManager(context, JEESessionStore.INSTANCE);
    val profile = new CommonProfile();
    profile.setClientName(Authenticators.CAS_OAUTH_CLIENT_BASIC_AUTHN);
    profile.setId("casuser");
    manager.save(true, profile, false);
    val result = OAuth20AccessTokenResponseResult.builder().service(RegisteredServiceTestUtils.getService()).registeredService(getOidcRegisteredService()).casProperties(casProperties).generatedToken(token).responseType(OAuth20ResponseTypes.DEVICE_CODE).userProfile(profile).build();
    val mv = oidcAccessTokenResponseGenerator.generate(result);
    assertNotNull(mv);
    val modelMap = mv.getModelMap();
    assertTrue(modelMap.containsKey(OAuth20Constants.DEVICE_VERIFICATION_URI));
    assertTrue(modelMap.containsKey(OAuth20Constants.DEVICE_USER_CODE));
    assertTrue(modelMap.containsKey(OAuth20Constants.DEVICE_CODE));
    assertTrue(modelMap.containsKey(OAuth20Constants.DEVICE_INTERVAL));
}
Also used : lombok.val(lombok.val) ProfileManager(org.pac4j.core.profile.ProfileManager) CommonProfile(org.pac4j.core.profile.CommonProfile) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 15 with JEEContext

use of org.pac4j.core.context.JEEContext in project cas by apereo.

the class OidcAuthenticationAuthorizeSecurityLogicTests method verifyMaxAgeOperation.

@Test
public void verifyMaxAgeOperation() {
    val request = new MockHttpServletRequest();
    request.addParameter(OidcConstants.MAX_AGE, "5");
    val response = new MockHttpServletResponse();
    when(ticketGrantingTicketCookieGenerator.retrieveCookieValue(request)).thenReturn(ticketGrantingTicket.getId());
    val context = new JEEContext(request, response);
    val profileManager = new ProfileManager(context, JEESessionStore.INSTANCE);
    var profile = new BasicUserProfile();
    profile.addAuthenticationAttribute(CasProtocolConstants.VALIDATION_CAS_MODEL_ATTRIBUTE_NAME_AUTHENTICATION_DATE, ZonedDateTime.now(Clock.systemUTC()).minusSeconds(30));
    profileManager.save(true, profile, false);
    val logic = new OidcAuthenticationAuthorizeSecurityLogic(ticketGrantingTicketCookieGenerator, ticketRegistry, centralAuthenticationService);
    assertTrue(logic.loadProfiles(profileManager, context, JEESessionStore.INSTANCE, List.of()).isEmpty());
}
Also used : lombok.val(lombok.val) ProfileManager(org.pac4j.core.profile.ProfileManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) BasicUserProfile(org.pac4j.core.profile.BasicUserProfile) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

JEEContext (org.pac4j.core.context.JEEContext)224 lombok.val (lombok.val)215 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)158 Test (org.junit.jupiter.api.Test)157 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)155 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)34 ProfileManager (org.pac4j.core.profile.ProfileManager)28 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)24 CommonProfile (org.pac4j.core.profile.CommonProfile)22 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)16 HashMap (java.util.HashMap)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 RedirectView (org.springframework.web.servlet.view.RedirectView)14 CasProfile (org.pac4j.cas.profile.CasProfile)13 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)13 MockRequestContext (org.springframework.webflow.test.MockRequestContext)13 GetMapping (org.springframework.web.bind.annotation.GetMapping)11 Map (java.util.Map)10 Slf4j (lombok.extern.slf4j.Slf4j)10