Search in sources :

Example 66 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class WSFederationValidateRequestControllerTests method verifyLoginRenewWithToken.

@Test
public void verifyLoginRenewWithToken() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val registeredService = getWsFederationRegisteredService();
    request.addParameter(WSFederationConstants.WTREALM, registeredService.getRealm());
    request.addParameter(WSFederationConstants.WREPLY, registeredService.getServiceId());
    request.addParameter(WSFederationConstants.WREFRESH, "1");
    request.addParameter(WSFederationConstants.WA, WSFederationConstants.WSIGNIN10);
    val token = mock(SecurityToken.class);
    when(token.isExpired()).thenReturn(Boolean.FALSE);
    when(token.getCreated()).thenReturn(Instant.now(Clock.systemUTC()).minusSeconds(300));
    val id = UUID.randomUUID().toString();
    val sts = mock(SecurityTokenTicket.class);
    when(sts.getPrefix()).thenReturn(SecurityTokenTicket.PREFIX);
    when(sts.getId()).thenReturn(SecurityTokenTicket.PREFIX + '-' + id);
    when(sts.isExpired()).thenReturn(Boolean.FALSE);
    when(sts.getSecurityToken()).thenReturn(token);
    ticketRegistry.addTicket(sts);
    val tgt = new MockTicketGrantingTicket("casuser");
    tgt.getDescendantTickets().add(sts.getId());
    ticketRegistry.addTicket(tgt);
    ticketGrantingTicketCookieGenerator.addCookie(response, tgt.getId());
    request.setCookies(response.getCookies());
    assertDoesNotThrow(() -> {
        federationValidateRequestController.handleFederationRequest(response, request);
        return null;
    });
    assertEquals(HttpStatus.SC_MOVED_TEMPORARILY, response.getStatus());
    val builder = new URIBuilder(response.getHeader("Location"));
    assertTrue(builder.getQueryParams().stream().anyMatch(p -> p.getName().equals(CasProtocolConstants.PARAMETER_SERVICE)));
    assertTrue(builder.getQueryParams().stream().anyMatch(p -> p.getName().equals(CasProtocolConstants.PARAMETER_RENEW)));
    assertTrue(builder.getQueryParams().stream().anyMatch(p -> p.getName().equals(WSFederationConstants.WTREALM)));
    assertTrue(builder.getQueryParams().stream().anyMatch(p -> p.getName().equals(WSFederationConstants.WREPLY)));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) HttpStatus(org.apache.http.HttpStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SecurityTokenTicket(org.apereo.cas.ticket.SecurityTokenTicket) TicketRegistry(org.apereo.cas.ticket.registry.TicketRegistry) CasCookieBuilder(org.apereo.cas.web.cookie.CasCookieBuilder) WSFederationRegisteredService(org.apereo.cas.ws.idp.services.WSFederationRegisteredService) RegisteredServiceTestUtils(org.apereo.cas.services.RegisteredServiceTestUtils) Qualifier(org.springframework.beans.factory.annotation.Qualifier) BaseCoreWsSecurityIdentityProviderConfigurationTests(org.apereo.cas.BaseCoreWsSecurityIdentityProviderConfigurationTests) Tag(org.junit.jupiter.api.Tag) ServicesManager(org.apereo.cas.services.ServicesManager) CasProtocolConstants(org.apereo.cas.CasProtocolConstants) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) URIBuilder(org.apache.http.client.utils.URIBuilder) WSFederationConstants(org.apereo.cas.ws.idp.WSFederationConstants) lombok.val(lombok.val) TestPropertySource(org.springframework.test.context.TestPropertySource) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UUID(java.util.UUID) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) Mockito(org.mockito.Mockito) UnauthorizedAuthenticationException(org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException) Assertions(org.junit.jupiter.api.Assertions) Clock(java.time.Clock) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) URIBuilder(org.apache.http.client.utils.URIBuilder) Test(org.junit.jupiter.api.Test)

Example 67 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class GroovyAcceptableUsagePolicyRepositoryTests method verifyPolicyTermsFails.

@Test
public void verifyPolicyTermsFails() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
    val credential = CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword("casuser");
    val tgt = new MockTicketGrantingTicket(credential.getId(), credential, Map.of());
    ticketRegistry.addTicket(tgt);
    assertFalse(acceptableUsagePolicyRepository.fetchPolicy(context).isPresent());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 68 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class AbstractOAuth20Tests method generateAccessTokenResponseAndGetModelAndView.

@SneakyThrows
protected ModelAndView generateAccessTokenResponseAndGetModelAndView(final OAuthRegisteredService registeredService, final Authentication authentication, final OAuth20GrantTypes grantType, final HttpServletRequest mockRequest) {
    val mockResponse = new MockHttpServletResponse();
    val service = RegisteredServiceTestUtils.getService(SERVICE_URL);
    val holder = AccessTokenRequestContext.builder().clientId(registeredService.getClientId()).service(service).authentication(authentication).registeredService(registeredService).grantType(grantType).responseType(OAuth20ResponseTypes.CODE).ticketGrantingTicket(new MockTicketGrantingTicket("casuser")).claims(OAuth20Utils.parseRequestClaims(new JEEContext(mockRequest, mockResponse))).build();
    val generatedToken = oauthTokenGenerator.generate(holder);
    val builder = OAuth20AccessTokenResponseResult.builder();
    val result = builder.registeredService(registeredService).responseType(OAuth20ResponseTypes.CODE).service(service).generatedToken(generatedToken).build();
    return accessTokenResponseGenerator.generate(result);
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) JEEContext(org.pac4j.core.context.JEEContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) SneakyThrows(lombok.SneakyThrows)

Example 69 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class AbstractOAuth20Tests method addRefreshToken.

protected OAuth20RefreshToken addRefreshToken(final Principal principal, final OAuthRegisteredService registeredService) throws Exception {
    val authentication = getAuthentication(principal);
    val factory = new WebApplicationServiceFactory();
    val service = factory.createService(registeredService.getServiceId());
    val refreshToken = oAuthRefreshTokenFactory.create(service, authentication, new MockTicketGrantingTicket("casuser"), new ArrayList<>(), registeredService.getClientId(), StringUtils.EMPTY, new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE);
    this.ticketRegistry.addTicket(refreshToken);
    return refreshToken;
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory)

Example 70 with MockTicketGrantingTicket

use of org.apereo.cas.mock.MockTicketGrantingTicket in project cas by apereo.

the class AbstractOAuth20Tests method addAccessToken.

protected OAuth20AccessToken addAccessToken(final Principal principal, final OAuthRegisteredService registeredService) throws Exception {
    val code = addCode(principal, registeredService);
    val authentication = getAuthentication(principal);
    val factory = new WebApplicationServiceFactory();
    val service = factory.createService(registeredService.getServiceId());
    val accessToken = defaultAccessTokenFactory.create(service, authentication, new MockTicketGrantingTicket("casuser"), new ArrayList<>(), code.getId(), registeredService.getClientId(), new HashMap<>(), OAuth20ResponseTypes.CODE, OAuth20GrantTypes.AUTHORIZATION_CODE);
    this.ticketRegistry.addTicket(accessToken);
    return accessToken;
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory)

Aggregations

MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)224 lombok.val (lombok.val)199 Test (org.junit.jupiter.api.Test)164 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)93 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)92 MockRequestContext (org.springframework.webflow.test.MockRequestContext)42 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)39 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)36 JEEContext (org.pac4j.core.context.JEEContext)33 MockServletContext (org.springframework.mock.web.MockServletContext)33 HashMap (java.util.HashMap)31 MockServiceTicket (org.apereo.cas.mock.MockServiceTicket)18 Test (org.junit.Test)18 WebApplicationServiceFactory (org.apereo.cas.authentication.principal.WebApplicationServiceFactory)13 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)12 CasProfile (org.pac4j.cas.profile.CasProfile)11 Authentication (org.apereo.cas.authentication.Authentication)10 HardTimeoutExpirationPolicy (org.apereo.cas.ticket.expiration.HardTimeoutExpirationPolicy)10 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)10 RedirectView (org.springframework.web.servlet.view.RedirectView)10