use of org.apache.commons.lang3.StringUtils in project cas by apereo.
the class AbstractX509PrincipalResolver method getAlternatePrincipal.
/**
* Get alternate principal if alternate attribute configured.
*
* @param certificate X509 Certificate of user
* @return principal using alternate attribute or null if none configured
*/
protected String getAlternatePrincipal(final X509Certificate certificate) {
if (StringUtils.isBlank(alternatePrincipalAttribute)) {
return null;
}
val attributes = extractPersonAttributes(certificate);
val attribute = attributes.get(alternatePrincipalAttribute);
if (attribute == null) {
LOGGER.debug("Attempt to get alternate principal with attribute [{}] was unsuccessful.", alternatePrincipalAttribute);
return null;
}
val optionalAttribute = CollectionUtils.firstElement(attribute);
return optionalAttribute.map(Object::toString).filter(StringUtils::isNotEmpty).map(alternatePrincipal -> {
LOGGER.debug("Using alternate principal attribute [{}]", alternatePrincipal);
return alternatePrincipal;
}).orElseGet(() -> {
LOGGER.trace("Returning null principal id...");
return null;
});
}
use of org.apache.commons.lang3.StringUtils in project cas by apereo.
the class CoreAuthenticationUtils method buildPrincipalResolutionContext.
/**
* New PrincipalResolutionContext.
*
* @param principalFactory the principal factory
* @param attributeRepository the attribute repository
* @param attributeMerger the attribute merger
* @param personDirectory the person directory properties
* @return the resolver
*/
public static PrincipalResolutionContext buildPrincipalResolutionContext(final PrincipalFactory principalFactory, final IPersonAttributeDao attributeRepository, final IAttributeMerger attributeMerger, final PersonDirectoryPrincipalResolverProperties... personDirectory) {
val transformers = Arrays.stream(personDirectory).map(p -> PrincipalNameTransformerUtils.newPrincipalNameTransformer(p.getPrincipalTransformation())).collect(Collectors.toList());
val transformer = new ChainingPrincipalNameTransformer(transformers);
return PrincipalResolutionContext.builder().attributeRepository(attributeRepository).attributeMerger(attributeMerger).principalFactory(principalFactory).returnNullIfNoAttributes(Arrays.stream(personDirectory).filter(p -> p.getReturnNull() != TriStateBoolean.UNDEFINED).map(p -> p.getReturnNull().toBoolean()).findFirst().orElse(Boolean.FALSE)).principalAttributeNames(Arrays.stream(personDirectory).map(PersonDirectoryPrincipalResolverProperties::getPrincipalAttribute).filter(StringUtils::isNotBlank).findFirst().orElse(StringUtils.EMPTY)).principalNameTransformer(transformer).useCurrentPrincipalId(Arrays.stream(personDirectory).filter(p -> p.getUseExistingPrincipalId() != TriStateBoolean.UNDEFINED).map(p -> p.getUseExistingPrincipalId().toBoolean()).findFirst().orElse(Boolean.FALSE)).resolveAttributes(Arrays.stream(personDirectory).filter(p -> p.getAttributeResolutionEnabled() != TriStateBoolean.UNDEFINED).map(p -> p.getAttributeResolutionEnabled().toBoolean()).findFirst().orElse(Boolean.TRUE)).activeAttributeRepositoryIdentifiers(Arrays.stream(personDirectory).filter(p -> StringUtils.isNotBlank(p.getActiveAttributeRepositoryIds())).map(p -> org.springframework.util.StringUtils.commaDelimitedListToSet(p.getActiveAttributeRepositoryIds())).filter(p -> !p.isEmpty()).findFirst().orElse(Collections.EMPTY_SET)).build();
}
use of org.apache.commons.lang3.StringUtils in project cas by apereo.
the class ServiceValidationViewFactory method getValidationResponseType.
/**
* Gets validation response type.
*
* @param request the request
* @param service the service
* @return the validation response type
*/
private static ValidationResponseType getValidationResponseType(final HttpServletRequest request, final WebApplicationService service) {
val format = request.getParameter(CasProtocolConstants.PARAMETER_FORMAT);
final Function<String, ValidationResponseType> func = FunctionUtils.doIf(StringUtils::isNotBlank, t -> ValidationResponseType.valueOf(t.toUpperCase()), f -> service != null ? service.getFormat() : ValidationResponseType.XML);
return func.apply(format);
}
use of org.apache.commons.lang3.StringUtils in project cas by apereo.
the class MongoDbConnectionFactory method buildMongoDbClient.
/**
* Build mongo db client.
*
* @param mongo the mongo
* @return the mongo client
*/
public MongoClient buildMongoDbClient(final BaseMongoDbProperties mongo) {
val settingsBuilder = MongoClientSettings.builder();
if (StringUtils.isNotBlank(mongo.getClientUri())) {
LOGGER.debug("Using MongoDb client URI [{}] to connect to MongoDb instance", mongo.getClientUri());
settingsBuilder.applyConnectionString(new ConnectionString(mongo.getClientUri()));
} else {
val serverAddresses = mongo.getHost().split(",");
if (serverAddresses.length == 0) {
throw new BeanCreationException("Unable to build a MongoDb client without any hosts/servers defined");
}
val servers = new ArrayList<ServerAddress>(0);
if (serverAddresses.length > 1) {
LOGGER.debug("Multiple MongoDb server addresses are defined. Ignoring port [{}], " + "assuming ports are defined as part of the address", mongo.getPort());
Arrays.stream(serverAddresses).filter(StringUtils::isNotBlank).map(ServerAddress::new).forEach(servers::add);
} else {
val port = mongo.getPort() > 0 ? mongo.getPort() : DEFAULT_PORT;
LOGGER.debug("Found single MongoDb server address [{}] using port [{}]", mongo.getHost(), port);
val addr = new ServerAddress(mongo.getHost(), port);
servers.add(addr);
}
settingsBuilder.applyToClusterSettings(builder -> builder.hosts(servers));
val credential = buildMongoCredential(mongo);
settingsBuilder.credential(credential).writeConcern(WriteConcern.valueOf(mongo.getWriteConcern())).codecRegistry(CodecRegistries.fromRegistries(CodecRegistries.fromProviders(new BaseConverters.ZonedDateTimeCodecProvider()), MongoClientSettings.getDefaultCodecRegistry())).readConcern(new ReadConcern(ReadConcernLevel.valueOf(mongo.getReadConcern()))).applyToConnectionPoolSettings(builder -> {
val poolConfig = mongo.getPool();
val pool = ConnectionPoolSettings.builder().maxConnectionLifeTime(Beans.newDuration(poolConfig.getLifeTime()).toMillis(), TimeUnit.MILLISECONDS).maxConnectionIdleTime(Beans.newDuration(poolConfig.getIdleTime()).toMillis(), TimeUnit.MILLISECONDS).maxSize(poolConfig.getMaxSize()).minSize(poolConfig.getMinSize()).maxWaitTime(Beans.newDuration(poolConfig.getMaxWaitTime()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(pool);
}).applyToSocketSettings(builder -> {
val socket = SocketSettings.builder().connectTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).readTimeout((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(socket);
}).applyToSslSettings(builder -> {
val ssl = SslSettings.builder().enabled(mongo.isSslEnabled()).context(this.sslContext).build();
builder.applySettings(ssl);
}).applyToServerSettings(builder -> {
val server = ServerSettings.builder().heartbeatFrequency((int) Beans.newDuration(mongo.getTimeout()).toMillis(), TimeUnit.MILLISECONDS).build();
builder.applySettings(server);
}).retryWrites(mongo.isRetryWrites());
}
return MongoClients.create(settingsBuilder.build());
}
use of org.apache.commons.lang3.StringUtils in project cas by apereo.
the class OidcClientRegistrationUtils method getClientRegistrationResponse.
/**
* Gets client registration response.
*
* @param registeredService the registered service
* @param serverPrefix the server prefix
* @return the client registration response
*/
@SneakyThrows
public static OidcClientRegistrationResponse getClientRegistrationResponse(final OidcRegisteredService registeredService, final String serverPrefix) {
val clientResponse = new OidcClientRegistrationResponse();
clientResponse.setApplicationType(registeredService.getApplicationType());
clientResponse.setClientId(registeredService.getClientId());
clientResponse.setClientSecret(registeredService.getClientSecret());
clientResponse.setSubjectType(registeredService.getSubjectType());
clientResponse.setTokenEndpointAuthMethod(registeredService.getTokenEndpointAuthenticationMethod());
clientResponse.setClientName(registeredService.getName());
clientResponse.setRedirectUris(CollectionUtils.wrap(registeredService.getServiceId()));
clientResponse.setUserInfoSignedReponseAlg(registeredService.getUserInfoSigningAlg());
clientResponse.setUserInfoEncryptedReponseAlg(registeredService.getUserInfoEncryptedResponseAlg());
clientResponse.setUserInfoEncryptedReponseEncoding(registeredService.getUserInfoEncryptedResponseEncoding());
clientResponse.setContacts(registeredService.getContacts().stream().map(RegisteredServiceContact::getName).filter(StringUtils::isNotBlank).collect(Collectors.toList()));
clientResponse.setGrantTypes(Arrays.stream(OAuth20GrantTypes.values()).map(type -> type.getType().toLowerCase()).collect(Collectors.toList()));
clientResponse.setResponseTypes(Arrays.stream(OAuth20ResponseTypes.values()).map(type -> type.getType().toLowerCase()).collect(Collectors.toList()));
val validator = new SimpleUrlValidatorFactoryBean(false).getObject();
val keystore = SpringExpressionLanguageValueResolver.getInstance().resolve(registeredService.getJwks());
if (Objects.requireNonNull(validator).isValid(keystore)) {
clientResponse.setJwksUri(keystore);
} else if (ResourceUtils.doesResourceExist(keystore)) {
val res = ResourceUtils.getResourceFrom(keystore);
val json = IOUtils.toString(res.getInputStream(), StandardCharsets.UTF_8);
clientResponse.setJwks(new JsonWebKeySet(json).toJson());
} else if (StringUtils.isNotBlank(keystore)) {
val jwks = new JsonWebKeySet(keystore);
clientResponse.setJwks(jwks.toJson());
}
clientResponse.setLogo(registeredService.getLogo());
clientResponse.setPolicyUri(registeredService.getInformationUrl());
clientResponse.setTermsOfUseUri(registeredService.getPrivacyUrl());
clientResponse.setRedirectUris(CollectionUtils.wrapList(registeredService.getServiceId()));
val clientConfigUri = getClientConfigurationUri(registeredService, serverPrefix);
clientResponse.setRegistrationClientUri(clientConfigUri);
return clientResponse;
}
Aggregations