use of org.apereo.cas.ticket.ServiceTicketFactory in project cas by apereo.
the class DefaultCentralAuthenticationService method grantServiceTicket.
@Audit(action = "SERVICE_TICKET", actionResolverName = "GRANT_SERVICE_TICKET_RESOLVER", resourceResolverName = "GRANT_SERVICE_TICKET_RESOURCE_RESOLVER")
@Timed(name = "GRANT_SERVICE_TICKET_TIMER")
@Metered(name = "GRANT_SERVICE_TICKET_METER")
@Counted(name = "GRANT_SERVICE_TICKET_COUNTER", monotonic = true)
@Override
public ServiceTicket grantServiceTicket(final String ticketGrantingTicketId, final Service service, final AuthenticationResult authenticationResult) throws AuthenticationException, AbstractTicketException {
final boolean credentialProvided = authenticationResult != null && authenticationResult.isCredentialProvided();
final TicketGrantingTicket ticketGrantingTicket = getTicket(ticketGrantingTicketId, TicketGrantingTicket.class);
final Service selectedService = resolveServiceFromAuthenticationRequest(service);
final RegisteredService registeredService = this.servicesManager.findServiceBy(selectedService);
final AuditableContext audit = AuditableContext.builder().service(selectedService).ticketGrantingTicket(ticketGrantingTicket).registeredService(registeredService).retrievePrincipalAttributesFromReleasePolicy(Boolean.FALSE).build();
final AuditableExecutionResult accessResult = this.registeredServiceAccessStrategyEnforcer.execute(audit);
accessResult.throwExceptionIfNeeded();
final Authentication currentAuthentication = evaluatePossibilityOfMixedPrincipals(authenticationResult, ticketGrantingTicket);
RegisteredServiceAccessStrategyUtils.ensureServiceSsoAccessIsAllowed(registeredService, selectedService, ticketGrantingTicket, credentialProvided);
evaluateProxiedServiceIfNeeded(selectedService, ticketGrantingTicket, registeredService);
// Perform security policy check by getting the authentication that satisfies the configured policy
getAuthenticationSatisfiedByPolicy(currentAuthentication, new ServiceContext(selectedService, registeredService));
final Authentication latestAuthentication = ticketGrantingTicket.getRoot().getAuthentication();
AuthenticationCredentialsThreadLocalBinder.bindCurrent(latestAuthentication);
final Principal principal = latestAuthentication.getPrincipal();
final ServiceTicketFactory factory = (ServiceTicketFactory) this.ticketFactory.get(ServiceTicket.class);
final ServiceTicket serviceTicket = factory.create(ticketGrantingTicket, service, credentialProvided, ServiceTicket.class);
this.ticketRegistry.updateTicket(ticketGrantingTicket);
this.ticketRegistry.addTicket(serviceTicket);
LOGGER.info("Granted ticket [{}] for service [{}] and principal [{}]", serviceTicket.getId(), DigestUtils.abbreviate(service.getId()), principal.getId());
doPublishEvent(new CasServiceTicketGrantedEvent(this, ticketGrantingTicket, serviceTicket));
return serviceTicket;
}
use of org.apereo.cas.ticket.ServiceTicketFactory in project cas by apereo.
the class JpaTicketRegistryCleanerTests method verifyOperation.
@Test
@Order(10)
public void verifyOperation() throws Exception {
val tgtFactory = (TicketGrantingTicketFactory) ticketFactory.get(TicketGrantingTicket.class);
val tgt = tgtFactory.create(RegisteredServiceTestUtils.getAuthentication(), RegisteredServiceTestUtils.getService(), TicketGrantingTicket.class);
ticketRegistry.addTicket(tgt);
val stFactory = (ServiceTicketFactory) ticketFactory.get(ServiceTicket.class);
val st = stFactory.create(tgt, RegisteredServiceTestUtils.getService(), true, ServiceTicket.class);
ticketRegistry.addTicket(st);
ticketRegistry.updateTicket(tgt);
assertEquals(1, ticketRegistry.sessionCount());
assertEquals(1, ticketRegistry.serviceTicketCount());
st.markTicketExpired();
tgt.markTicketExpired();
ticketRegistry.updateTicket(st);
ticketRegistry.updateTicket(tgt);
assertTrue(ticketRegistryCleaner.clean() > 0);
assertEquals(0, ticketRegistry.sessionCount());
assertEquals(0, ticketRegistry.serviceTicketCount());
}
use of org.apereo.cas.ticket.ServiceTicketFactory in project cas by apereo.
the class AbstractSamlIdPProfileHandlerController method buildResponseBasedSingleSignOnSession.
/**
* Build response based single sign on session.
* The http response before encoding the SAML response is reset
* to ensure a clean slate from previous attempts, specially
* when requests/responses are produced rapidly.
*
* @param context the pair
* @param ticketGrantingTicket the authentication
* @param request the request
* @param response the response
* @throws Exception the exception
*/
protected void buildResponseBasedSingleSignOnSession(final Pair<? extends RequestAbstractType, MessageContext> context, final TicketGrantingTicket ticketGrantingTicket, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
val authnRequest = (AuthnRequest) context.getLeft();
val id = SamlIdPUtils.getIssuerFromSamlObject(authnRequest);
val service = configurationContext.getWebApplicationServiceFactory().createService(id);
service.getAttributes().put(SamlProtocolConstants.PARAMETER_ENTITY_ID, CollectionUtils.wrapList(id));
val registeredService = configurationContext.getServicesManager().findServiceBy(service, SamlRegisteredService.class);
val audit = AuditableContext.builder().service(service).authentication(ticketGrantingTicket.getAuthentication()).registeredService(registeredService).httpRequest(request).httpResponse(response).build();
val accessResult = configurationContext.getRegisteredServiceAccessStrategyEnforcer().execute(audit);
accessResult.throwExceptionIfNeeded();
val assertion = buildCasAssertion(ticketGrantingTicket.getAuthentication(), service, registeredService, Map.of());
val authenticationContext = buildAuthenticationContextPair(request, response, context);
val binding = determineProfileBinding(authenticationContext);
val messageContext = authenticationContext.getRight();
val relayState = SAMLBindingSupport.getRelayState(messageContext);
SAMLBindingSupport.setRelayState(authenticationContext.getRight(), relayState);
response.reset();
val factory = (ServiceTicketFactory) getConfigurationContext().getTicketFactory().get(ServiceTicket.class);
val st = factory.create(ticketGrantingTicket, service, false, ServiceTicket.class);
getConfigurationContext().getTicketRegistry().addTicket(st);
getConfigurationContext().getTicketRegistry().updateTicket(ticketGrantingTicket);
buildSamlResponse(response, request, authenticationContext, assertion, binding);
}
use of org.apereo.cas.ticket.ServiceTicketFactory in project cas by apereo.
the class DefaultServiceTicketFactoryTests method verifyBadType.
@Test
public void verifyBadType() {
val factory = (ServiceTicketFactory) this.ticketFactory.get(ServiceTicket.class);
assertThrows(ClassCastException.class, () -> factory.create(new MockTicketGrantingTicket("casuser"), RegisteredServiceTestUtils.getService("customExpirationPolicy"), true, BaseMockTicketServiceTicket.class));
}
use of org.apereo.cas.ticket.ServiceTicketFactory in project cas by apereo.
the class DefaultServiceTicketFactoryTests method verifyCustomExpirationPolicy.
@Test
public void verifyCustomExpirationPolicy() {
val svc = RegisteredServiceTestUtils.getRegisteredService("customExpirationPolicy", RegexRegisteredService.class);
svc.setServiceTicketExpirationPolicy(new DefaultRegisteredServiceServiceTicketExpirationPolicy(10, "666"));
servicesManager.save(svc);
val factory = (ServiceTicketFactory) this.ticketFactory.get(ServiceTicket.class);
val serviceTicket = factory.create(new MockTicketGrantingTicket("casuser"), RegisteredServiceTestUtils.getService("customExpirationPolicy"), true, ServiceTicket.class);
assertNotNull(serviceTicket);
assertEquals(666, serviceTicket.getExpirationPolicy().getTimeToLive());
}
Aggregations