use of org.apache.commons.lang3.StringUtils.isNotBlank 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);
}
}
use of org.apache.commons.lang3.StringUtils.isNotBlank in project cas by apereo.
the class Pac4jAuthenticationEventExecutionPlanConfiguration method configureSamlClient.
private void configureSamlClient(final Collection<BaseClient> properties) {
final AtomicInteger index = new AtomicInteger();
casProperties.getAuthn().getPac4j().getSaml().stream().filter(saml -> StringUtils.isNotBlank(saml.getKeystorePath()) && StringUtils.isNotBlank(saml.getIdentityProviderMetadataPath())).forEach(saml -> {
final SAML2ClientConfiguration cfg = new SAML2ClientConfiguration(saml.getKeystorePath(), saml.getKeystorePassword(), saml.getPrivateKeyPassword(), saml.getIdentityProviderMetadataPath());
cfg.setMaximumAuthenticationLifetime(saml.getMaximumAuthenticationLifetime());
cfg.setServiceProviderEntityId(saml.getServiceProviderEntityId());
cfg.setServiceProviderMetadataPath(saml.getServiceProviderMetadataPath());
cfg.setDestinationBindingType(SAMLConstants.SAML2_REDIRECT_BINDING_URI);
final SAML2Client client = new SAML2Client(cfg);
client.setName(client.getClass().getSimpleName() + index.incrementAndGet());
properties.add(client);
});
}
use of org.apache.commons.lang3.StringUtils.isNotBlank in project cas by apereo.
the class ClickatellSmsSender method send.
@Override
public boolean send(final String from, final String to, final String message) {
try {
final MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization", this.token);
headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
final Map<String, Object> map = new HashMap<>();
map.put("content", message);
map.put("to", Arrays.asList(to));
map.put("from", from);
final StringWriter stringify = new StringWriter();
mapper.writeValue(stringify, map);
final HttpEntity<String> request = new HttpEntity<>(stringify.toString(), headers);
final ResponseEntity<Map> response = restTemplate.postForEntity(new URI(this.serverUrl), request, Map.class);
if (response.hasBody()) {
final List<Map> messages = (List<Map>) response.getBody().get("messages");
final String error = (String) response.getBody().get("error");
if (StringUtils.isNotBlank(error)) {
LOGGER.error(error);
return false;
}
final List<String> errors = messages.stream().filter(m -> m.containsKey("accepted") && !Boolean.valueOf(m.get("accepted").toString()) && m.containsKey("error")).map(m -> (String) m.get("error")).collect(Collectors.toList());
if (errors.isEmpty()) {
return true;
}
errors.forEach(LOGGER::error);
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return false;
}
use of org.apache.commons.lang3.StringUtils.isNotBlank in project cas by apereo.
the class CasCoreAuthenticationHandlersConfiguration method getParsedUsers.
private Map<String, String> getParsedUsers() {
final Pattern pattern = Pattern.compile("::");
final String usersProperty = casProperties.getAuthn().getAccept().getUsers();
if (StringUtils.isNotBlank(usersProperty) && usersProperty.contains(pattern.pattern())) {
return Stream.of(usersProperty.split(",")).map(pattern::split).collect(Collectors.toMap(userAndPassword -> userAndPassword[0], userAndPassword -> userAndPassword[1]));
}
return Collections.emptyMap();
}
use of org.apache.commons.lang3.StringUtils.isNotBlank in project cas by apereo.
the class OidcJwksEndpointController method handleRequestInternal.
/**
* Handle request for jwk set.
*
* @param request the request
* @param response the response
* @param model the model
* @return the jwk set
* @throws Exception the exception
*/
@GetMapping(value = '/' + OidcConstants.BASE_OIDC_URL + '/' + OidcConstants.JWKS_URL, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response, final Model model) throws Exception {
Assert.notNull(this.jwksFile, "JWKS file cannot be undefined or null.");
try {
final String jsonJwks = IOUtils.toString(this.jwksFile.getInputStream(), StandardCharsets.UTF_8);
final JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(jsonJwks);
getServicesManager().getAllServices().stream().filter(s -> s instanceof OidcRegisteredService && StringUtils.isNotBlank(((OidcRegisteredService) s).getJwks())).forEach(Unchecked.consumer(s -> {
final OidcRegisteredService service = (OidcRegisteredService) s;
final Resource resource = this.resourceLoader.getResource(service.getJwks());
final JsonWebKeySet set = new JsonWebKeySet(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8));
set.getJsonWebKeys().forEach(jsonWebKeySet::addJsonWebKey);
}));
final String body = jsonWebKeySet.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<>(body, HttpStatus.OK);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
Aggregations