Search in sources :

Example 51 with MockTicketGrantingTicket

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

the class TicketGrantingTicketCheckActionTests method verifyInvalidTicket.

@Test
public void verifyInvalidTicket() throws Exception {
    val ctx = new MockRequestContext();
    val tgt = new MockTicketGrantingTicket("user");
    WebUtils.putTicketGrantingTicketInScopes(ctx, tgt);
    val action = new TicketGrantingTicketCheckAction(getCentralAuthenticationService());
    val event = action.execute(ctx);
    assertEquals(CasWebflowConstants.TRANSITION_ID_TICKET_GRANTING_TICKET_INVALID, event.getId());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockRequestContext(org.springframework.webflow.test.MockRequestContext) TicketGrantingTicketCheckAction(org.apereo.cas.web.flow.login.TicketGrantingTicketCheckAction) Test(org.junit.jupiter.api.Test)

Example 52 with MockTicketGrantingTicket

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

the class OidcPushedAuthorizationRequestUriResponseBuilderTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    assertEquals(0, oidcPushedAuthorizationRequestResponseBuilder.getOrder());
    val registeredService = getOidcRegisteredService();
    val profile = new CommonProfile();
    profile.setId("casuser");
    val holder = AccessTokenRequestContext.builder().clientId(registeredService.getClientId()).service(RegisteredServiceTestUtils.getService()).authentication(RegisteredServiceTestUtils.getAuthentication()).registeredService(registeredService).grantType(OAuth20GrantTypes.AUTHORIZATION_CODE).responseType(OAuth20ResponseTypes.CODE).userProfile(profile).build();
    var authzRequest = OAuth20AuthorizationRequest.builder().clientId(registeredService.getClientId()).url(OidcConstants.PUSHED_AUTHORIZE_URL).grantType(OAuth20GrantTypes.AUTHORIZATION_CODE.getType()).responseType(OAuth20ResponseTypes.CODE.getType()).build();
    assertTrue(oidcPushedAuthorizationRequestResponseBuilder.supports(authzRequest));
    val mv = oidcPushedAuthorizationRequestResponseBuilder.build(holder);
    assertTrue(mv.getModel().containsKey(OidcConstants.EXPIRES_IN));
    val uri = mv.getModel().get(OidcConstants.REQUEST_URI).toString();
    var ticket = ticketRegistry.getTicket(uri, OidcPushedAuthorizationRequest.class);
    assertNotNull(ticket);
    val request = new MockHttpServletRequest();
    request.setRequestURI('/' + OidcConstants.PUSHED_AUTHORIZE_URL);
    val response = new MockHttpServletResponse();
    var context = new JEEContext(request, response);
    authzRequest = oidcPushedAuthorizationRequestResponseBuilder.toAuthorizationRequest(context, holder.getAuthentication(), holder.getService(), holder.getRegisteredService()).get().build();
    assertNotNull(authzRequest);
    assertFalse(authzRequest.isSingleSignOnSessionRequired());
    request.addParameter(OidcConstants.REQUEST_URI, uri);
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    val c = ticketGrantingTicketCookieGenerator.addCookie(request, response, tgt.getId());
    request.setCookies(c);
    context = new JEEContext(request, response);
    authzRequest = oidcPushedAuthorizationRequestResponseBuilder.toAuthorizationRequest(context, holder.getAuthentication(), holder.getService(), holder.getRegisteredService()).get().build();
    val accessTokenRequest = authzRequest.getAccessTokenRequest();
    assertTrue(authzRequest.isSingleSignOnSessionRequired());
    assertNotNull(accessTokenRequest.getAuthentication());
    assertNotNull(accessTokenRequest.getRegisteredService());
    assertNotNull(accessTokenRequest.getResponseType());
    assertNotNull(accessTokenRequest.getGrantType());
    assertNotNull(accessTokenRequest.getTicketGrantingTicket());
    ticket = ticketRegistry.getTicket(uri, OidcPushedAuthorizationRequest.class);
    assertNull(ticket);
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) 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 53 with MockTicketGrantingTicket

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

the class AbstractOidcTests method addCode.

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

Example 54 with MockTicketGrantingTicket

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

the class OidcProfileScopeToAttributesFilterTests method verifyOperationFilterWithOpenId.

@Test
public void verifyOperationFilterWithOpenId() {
    val service = getOidcRegisteredService();
    val accessToken = mock(OAuth20AccessToken.class);
    when(accessToken.getTicketGrantingTicket()).thenReturn(new MockTicketGrantingTicket("casuser"));
    when(accessToken.getScopes()).thenReturn(CollectionUtils.wrapSet(OidcConstants.StandardScopes.OPENID.getScope(), OidcConstants.StandardScopes.PHONE.getScope(), OidcConstants.StandardScopes.PROFILE.getScope(), OidcConstants.StandardScopes.ADDRESS.getScope(), OidcConstants.StandardScopes.EMAIL.getScope()));
    service.getScopes().add(OidcConstants.StandardScopes.EMAIL.getScope());
    service.getScopes().add(OidcConstants.StandardScopes.ADDRESS.getScope());
    service.getScopes().add(OidcConstants.StandardScopes.PHONE.getScope());
    service.getScopes().add(OidcConstants.StandardScopes.PROFILE.getScope());
    val original = CoreAuthenticationTestUtils.getPrincipal(CollectionUtils.wrap("email", "casuser@example.org", "address", "1234 Main Street", "phone", "123445677", "name", "CAS", "gender", "male"));
    val principal = profileScopeToAttributesFilter.filter(CoreAuthenticationTestUtils.getService(), original, service, accessToken);
    assertTrue(principal.getAttributes().containsKey("name"));
    assertTrue(principal.getAttributes().containsKey("address"));
    assertTrue(principal.getAttributes().containsKey("gender"));
    assertTrue(principal.getAttributes().containsKey("email"));
    assertEquals(4, principal.getAttributes().size());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) Test(org.junit.jupiter.api.Test)

Example 55 with MockTicketGrantingTicket

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

the class DelegatedClientAuthenticationActionTests method verifySsoAuthenticationWithInvalidTicketFails.

@Test
public void verifySsoAuthenticationWithInvalidTicketFails() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    request.addParameter("error_message", "Auth+failed");
    val response = new MockHttpServletResponse();
    val client = builtClients.findClient("FacebookClient").get();
    val webContext = new JEEContext(request, new MockHttpServletResponse());
    val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
    request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
    request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
    val service = CoreAuthenticationTestUtils.getService("https://delegated2.example.org");
    servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
    request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    setRequestContext(context);
    setExternalContext(context.getExternalContext());
    val tgt = new MockTicketGrantingTicket("casuser");
    centralAuthenticationService.addTicket(tgt);
    WebUtils.putTicketGrantingTicketInScopes(context, new MockTicketGrantingTicket("otheruser"));
    assertEquals(CasWebflowConstants.TRANSITION_ID_STOP, delegatedAuthenticationAction.execute(context).getId());
}
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) JEEContext(org.pac4j.core.context.JEEContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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