use of org.apereo.cas.configuration.model.support.saml.sps.AbstractSamlSPProperties in project cas by apereo.
the class SamlSPUtils method newSamlServiceProviderService.
/**
* New saml service provider registration.
*
* @param sp the properties
* @param resolver the resolver
* @return the saml registered service
*/
public static SamlRegisteredService newSamlServiceProviderService(final AbstractSamlSPProperties sp, final SamlRegisteredServiceCachingMetadataResolver resolver) {
if (StringUtils.isBlank(sp.getMetadata())) {
LOGGER.debug("Skipped registration of [{}] since no metadata location is found", sp.getName());
return null;
}
try {
final SamlRegisteredService service = new SamlRegisteredService();
service.setName(sp.getName());
service.setDescription(sp.getDescription());
service.setEvaluationOrder(Integer.MIN_VALUE);
service.setMetadataLocation(sp.getMetadata());
final List<String> attributesToRelease = new ArrayList<>(sp.getAttributes());
if (StringUtils.isNotBlank(sp.getNameIdAttribute())) {
attributesToRelease.add(sp.getNameIdAttribute());
service.setUsernameAttributeProvider(new PrincipalAttributeRegisteredServiceUsernameProvider(sp.getNameIdAttribute()));
}
if (StringUtils.isNotBlank(sp.getNameIdFormat())) {
service.setRequiredNameIdFormat(sp.getNameIdFormat());
}
final Map<String, String> attributes = Beans.transformPrincipalAttributesListIntoMap(attributesToRelease);
service.setAttributeReleasePolicy(new ReturnMappedAttributeReleasePolicy(attributes));
service.setMetadataCriteriaRoles(SPSSODescriptor.DEFAULT_ELEMENT_NAME.getLocalPart());
service.setMetadataCriteriaRemoveEmptyEntitiesDescriptors(true);
service.setMetadataCriteriaRemoveRolelessEntityDescriptors(true);
if (StringUtils.isNotBlank(sp.getSignatureLocation())) {
service.setMetadataSignatureLocation(sp.getSignatureLocation());
}
final List<String> entityIDList = sp.getEntityIds();
if (entityIDList.isEmpty()) {
final ChainingMetadataResolver chainingResolver = resolver.resolve(service);
if (chainingResolver.getResolvers().isEmpty()) {
LOGGER.warn("Skipped registration of [{}] since no metadata resolver could be constructed", sp.getName());
return null;
}
chainingResolver.getResolvers().forEach(r -> {
if (r instanceof AbstractBatchMetadataResolver) {
final Iterator<EntityDescriptor> it = ((AbstractBatchMetadataResolver) r).iterator();
final Optional<EntityDescriptor> descriptor = StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false).filter(e -> e.getSPSSODescriptor(SAMLConstants.SAML20P_NS) != null).findFirst();
if (descriptor.isPresent()) {
entityIDList.add(descriptor.get().getEntityID());
} else {
LOGGER.warn("Skipped registration of [{}] since no entity id could be found", sp.getName());
}
}
});
}
if (entityIDList.isEmpty()) {
LOGGER.warn("Skipped registration of [{}] since no metadata entity ids could be found", sp.getName());
return null;
}
final String entityIds = org.springframework.util.StringUtils.collectionToDelimitedString(entityIDList, "|");
service.setMetadataCriteriaDirection(PredicateFilter.Direction.INCLUDE.name());
service.setMetadataCriteriaPattern(entityIds);
LOGGER.debug("Registering saml service [{}] by entity id [{}]", sp.getName(), entityIds);
service.setServiceId(entityIds);
return service;
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
Aggregations