Search in sources :

Example 1 with CasSimpleMultifactorAuthenticationTicketFactory

use of org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory in project cas by apereo.

the class CasSimpleMultifactorSendTokenAction method getOrCreateToken.

/**
 * Get or create a token.
 *
 * @param requestContext the request context
 * @param principal      the principal
 * @return the token
 */
protected CasSimpleMultifactorAuthenticationTicket getOrCreateToken(final RequestContext requestContext, final Principal principal) {
    val currentToken = WebUtils.getSimpleMultifactorAuthenticationToken(requestContext, CasSimpleMultifactorAuthenticationTicket.class);
    return Optional.ofNullable(currentToken).filter(token -> !token.isExpired()).orElseGet(() -> {
        WebUtils.removeSimpleMultifactorAuthenticationToken(requestContext);
        val service = WebUtils.getService(requestContext);
        val mfaFactory = (CasSimpleMultifactorAuthenticationTicketFactory) ticketFactory.get(CasSimpleMultifactorAuthenticationTicket.class);
        val token = mfaFactory.create(service, CollectionUtils.wrap(CasSimpleMultifactorAuthenticationConstants.PROPERTY_PRINCIPAL, principal));
        LOGGER.debug("Created multifactor authentication token [{}] for service [{}]", token.getId(), service);
        return token;
    });
}
Also used : lombok.val(lombok.val) BucketConsumer(org.apereo.cas.bucket4j.consumer.BucketConsumer) RequiredArgsConstructor(lombok.RequiredArgsConstructor) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) StringUtils(org.apache.commons.lang3.StringUtils) RequestContext(org.springframework.webflow.execution.RequestContext) CasSimpleMultifactorAuthenticationTicketFactory(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory) CommunicationsManager(org.apereo.cas.notifications.CommunicationsManager) CasSimpleMultifactorAuthenticationConstants(org.apereo.cas.mfa.simple.CasSimpleMultifactorAuthenticationConstants) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) AbstractMultifactorAuthenticationAction(org.apereo.cas.web.flow.actions.AbstractMultifactorAuthenticationAction) CasWebflowConstants(org.apereo.cas.web.flow.CasWebflowConstants) CasSimpleMultifactorAuthenticationTicket(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) TicketFactory(org.apereo.cas.ticket.TicketFactory) CasSimpleMultifactorTokenCommunicationStrategy(org.apereo.cas.mfa.simple.CasSimpleMultifactorTokenCommunicationStrategy) lombok.val(lombok.val) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Slf4j(lombok.extern.slf4j.Slf4j) CasSimpleMultifactorAuthenticationProperties(org.apereo.cas.configuration.model.support.mfa.simple.CasSimpleMultifactorAuthenticationProperties) CasSimpleMultifactorAuthenticationProvider(org.apereo.cas.mfa.simple.CasSimpleMultifactorAuthenticationProvider) EmailMessageBodyBuilder(org.apereo.cas.notifications.mail.EmailMessageBodyBuilder) Optional(java.util.Optional) Principal(org.apereo.cas.authentication.principal.Principal) WebUtils(org.apereo.cas.web.support.WebUtils) Event(org.springframework.webflow.execution.Event) Ticket(org.apereo.cas.ticket.Ticket) CasSimpleMultifactorAuthenticationTicket(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket) CasSimpleMultifactorAuthenticationTicketFactory(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory)

Example 2 with CasSimpleMultifactorAuthenticationTicketFactory

use of org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory in project cas by apereo.

the class CasSimpleMultifactorAuthenticationTicketFactoryTests method verifyExpirationPolicy.

@Test
public void verifyExpirationPolicy() {
    val factory = (CasSimpleMultifactorAuthenticationTicketFactory) this.ticketFactory.get(CasSimpleMultifactorAuthenticationTicket.class);
    val ticket = factory.create(RegisteredServiceTestUtils.getService("example"), new HashMap<>(0));
    assertNotNull(ticket);
    assertEquals(30, ticket.getExpirationPolicy().getTimeToLive());
}
Also used : lombok.val(lombok.val) CasSimpleMultifactorAuthenticationTicket(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket) CasSimpleMultifactorAuthenticationTicketFactory(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with CasSimpleMultifactorAuthenticationTicketFactory

use of org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory in project cas by apereo.

the class CasSimpleMultifactorAuthenticationHandlerTests method verifyFailsPrincipal.

@Test
public void verifyFailsPrincipal() 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());
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication(), context);
    val factory = (CasSimpleMultifactorAuthenticationTicketFactory) defaultTicketFactory.get(CasSimpleMultifactorAuthenticationTicket.class);
    val ticket = factory.create(RegisteredServiceTestUtils.getService(), Map.of());
    ticketRegistry.addTicket(ticket);
    val credential = new CasSimpleMultifactorTokenCredential(ticket.getId());
    assertThrows(FailedLoginException.class, () -> casSimpleMultifactorAuthenticationHandler.authenticate(credential));
    assertFalse(casSimpleMultifactorAuthenticationHandler.supports(new UsernamePasswordCredential()));
    assertFalse(casSimpleMultifactorAuthenticationHandler.supports(UsernamePasswordCredential.class));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) CasSimpleMultifactorAuthenticationTicket(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket) CasSimpleMultifactorAuthenticationTicketFactory(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory) MockRequestContext(org.springframework.webflow.test.MockRequestContext) UsernamePasswordCredential(org.apereo.cas.authentication.credential.UsernamePasswordCredential) 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 4 with CasSimpleMultifactorAuthenticationTicketFactory

use of org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory in project cas by apereo.

the class CasSimpleMultifactorAuthenticationHandlerTests method verifySuccessfulAuthenticationWithTokenWithoutPrefix.

@Test
public void verifySuccessfulAuthenticationWithTokenWithoutPrefix() 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 principal = RegisteredServiceTestUtils.getPrincipal();
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication(principal), context);
    val factory = (CasSimpleMultifactorAuthenticationTicketFactory) defaultTicketFactory.get(CasSimpleMultifactorAuthenticationTicket.class);
    val ticket = factory.create(RegisteredServiceTestUtils.getService(), Map.of(CasSimpleMultifactorAuthenticationConstants.PROPERTY_PRINCIPAL, principal));
    ticketRegistry.addTicket(ticket);
    val ticketIdWithoutPrefix = ticket.getId().substring(CasSimpleMultifactorAuthenticationTicket.PREFIX.length() + 1);
    val credential = new CasSimpleMultifactorTokenCredential(ticketIdWithoutPrefix);
    assertNotNull(casSimpleMultifactorAuthenticationHandler.authenticate(credential).getPrincipal());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) CasSimpleMultifactorAuthenticationTicket(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket) CasSimpleMultifactorAuthenticationTicketFactory(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory) 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 5 with CasSimpleMultifactorAuthenticationTicketFactory

use of org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory in project cas by apereo.

the class CasSimpleMultifactorAuthenticationHandlerTests method verifyFailsExpiredToken.

@Test
public void verifyFailsExpiredToken() 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 principal = RegisteredServiceTestUtils.getPrincipal();
    WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication(principal), context);
    val factory = (CasSimpleMultifactorAuthenticationTicketFactory) defaultTicketFactory.get(CasSimpleMultifactorAuthenticationTicket.class);
    val ticket = factory.create(RegisteredServiceTestUtils.getService(), Map.of(CasSimpleMultifactorAuthenticationConstants.PROPERTY_PRINCIPAL, principal));
    ticketRegistry.addTicket(ticket);
    val credential = new CasSimpleMultifactorTokenCredential(ticket.getId());
    ticket.markTicketExpired();
    val handler = new CasSimpleMultifactorAuthenticationHandler(getClass().getSimpleName(), servicesManager, PrincipalFactoryUtils.newPrincipalFactory(), centralAuthenticationService, 0);
    assertThrows(FailedLoginException.class, () -> handler.authenticate(credential));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) CasSimpleMultifactorAuthenticationTicket(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket) CasSimpleMultifactorAuthenticationTicketFactory(org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory) 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)

Aggregations

lombok.val (lombok.val)6 CasSimpleMultifactorAuthenticationTicket (org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicket)6 CasSimpleMultifactorAuthenticationTicketFactory (org.apereo.cas.mfa.simple.ticket.CasSimpleMultifactorAuthenticationTicketFactory)6 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 MockServletContext (org.springframework.mock.web.MockServletContext)3 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)3 MockRequestContext (org.springframework.webflow.test.MockRequestContext)3 Map (java.util.Map)1 Optional (java.util.Optional)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)1 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)1 Principal (org.apereo.cas.authentication.principal.Principal)1 BucketConsumer (org.apereo.cas.bucket4j.consumer.BucketConsumer)1 CasSimpleMultifactorAuthenticationProperties (org.apereo.cas.configuration.model.support.mfa.simple.CasSimpleMultifactorAuthenticationProperties)1