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;
});
}
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());
}
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));
}
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());
}
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));
}
Aggregations