Search in sources :

Example 41 with MockTicketGrantingTicket

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

the class CreateTicketGrantingTicketActionTests method verifySkipTgt.

@Test
public void verifySkipTgt() throws Exception {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val tgt = new MockTicketGrantingTicket("casuser-new");
    getTicketRegistry().addTicket(tgt);
    WebUtils.putTicketGrantingTicketInScopes(context, tgt);
    WebUtils.putRegisteredService(context, RegisteredServiceTestUtils.getRegisteredService());
    prepareRequestContextForAuthentication(context, tgt.getAuthentication());
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, action.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) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 42 with MockTicketGrantingTicket

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

the class HazelcastTicketRegistryTests method verifyBadTicketInCatalog.

@RepeatedTest(1)
public void verifyBadTicketInCatalog() {
    val ticket = new MockTicketGrantingTicket("casuser");
    val instance = mock(HazelcastInstance.class);
    when(instance.getMap(anyString())).thenThrow(new RuntimeException());
    val catalog = mock(TicketCatalog.class);
    val defn = new DefaultTicketDefinition(ticket.getClass(), ticket.getPrefix(), 0);
    defn.getProperties().setStorageName("Tickets");
    when(catalog.find(any(Ticket.class))).thenReturn(defn);
    val registry = new HazelcastTicketRegistry(instance, catalog, 0);
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() throws Exception {
            registry.addTicket(ticket);
        }
    });
    assertNull(registry.getTicket(ticket.getId()));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) Ticket(org.apereo.cas.ticket.Ticket) DefaultTicketDefinition(org.apereo.cas.ticket.DefaultTicketDefinition) Executable(org.junit.jupiter.api.function.Executable) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 43 with MockTicketGrantingTicket

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

the class QRAuthenticationChannelControllerTests method verifyFails.

@Test
public void verifyFails() throws Exception {
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    val payload = JwtBuilder.JwtRequest.builder().subject("unknown-user").jwtId(tgt.getId()).issuer(casProperties.getServer().getPrefix()).serviceAudience("https://example.com/normal/").validUntilDate(DateTimeUtils.dateOf(LocalDate.now(Clock.systemUTC()).plusDays(1))).build();
    val jwt = jwtBuilder.build(payload);
    val message = mock(Message.class);
    val nativeHeaders = new LinkedMultiValueMap<>();
    nativeHeaders.put(QRAuthenticationConstants.QR_AUTHENTICATION_CHANNEL_ID, List.of(UUID.randomUUID().toString()));
    nativeHeaders.put(QRAuthenticationConstants.QR_AUTHENTICATION_DEVICE_ID, List.of(UUID.randomUUID().toString()));
    val headers = new MessageHeaders(Map.of("nativeHeaders", nativeHeaders));
    when(message.getHeaders()).thenReturn(headers);
    val token = String.format("{\"token\": \"%s\"}", jwt);
    when(message.getPayload()).thenReturn(token);
    assertFalse(qrAuthenticationChannelController.verify(message));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 44 with MockTicketGrantingTicket

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

the class DefaultQRAuthenticationTokenValidatorServiceTests method verifySuccess.

@Test
public void verifySuccess() throws Exception {
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    val deviceId = UUID.randomUUID().toString();
    qrAuthenticationDeviceRepository.authorizeDeviceFor(deviceId, tgt.getAuthentication().getPrincipal().getId());
    val payload = JwtBuilder.JwtRequest.builder().subject(tgt.getAuthentication().getPrincipal().getId()).jwtId(tgt.getId()).issuer(casProperties.getServer().getPrefix()).serviceAudience("https://example.com/normal/").validUntilDate(DateTimeUtils.dateOf(LocalDate.now(Clock.systemUTC()).plusDays(1))).attributes(Map.of(QRAuthenticationConstants.QR_AUTHENTICATION_DEVICE_ID, List.of(deviceId))).build();
    val jwt = jwtBuilder.build(payload);
    val request = QRAuthenticationTokenValidationRequest.builder().registeredService(Optional.empty()).token(jwt).deviceId(deviceId).build();
    val res = qrAuthenticationTokenValidatorService.validate(request);
    assertNotNull(res.getAuthentication());
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 45 with MockTicketGrantingTicket

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

the class DefaultQRAuthenticationTokenValidatorServiceTests method verifyBadDeviceId.

@Test
public void verifyBadDeviceId() throws Exception {
    val tgt = new MockTicketGrantingTicket("casuser");
    ticketRegistry.addTicket(tgt);
    val deviceId = UUID.randomUUID().toString();
    qrAuthenticationDeviceRepository.authorizeDeviceFor(deviceId, tgt.getAuthentication().getPrincipal().getId());
    val payload = JwtBuilder.JwtRequest.builder().subject(tgt.getAuthentication().getPrincipal().getId()).jwtId(tgt.getId()).issuer(casProperties.getServer().getPrefix()).serviceAudience("https://example.com/normal/").validUntilDate(DateTimeUtils.dateOf(LocalDate.now(Clock.systemUTC()).plusDays(1))).attributes(Map.of(QRAuthenticationConstants.QR_AUTHENTICATION_DEVICE_ID, List.of("mismatch"))).build();
    val jwt = jwtBuilder.build(payload);
    val request = QRAuthenticationTokenValidationRequest.builder().registeredService(Optional.empty()).token(jwt).deviceId(deviceId).build();
    assertThrows(AuthenticationException.class, () -> qrAuthenticationTokenValidatorService.validate(request));
}
Also used : lombok.val(lombok.val) MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) 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