private static <T> void addToCollection(final Collection<T> list, final T[] source) { if (source != null) { Arrays.stream(source).forEach(s -> { val col = toCollection(s); list.addAll((Collection) col); }); } } }
@Override public boolean hasAttribute(final String name, final Predicate<Object> predicate) { if (this.attributes.containsKey(name)) { val value = this.attributes.get(name); val valueCol = CollectionUtils.toCollection(value); return valueCol.stream().anyMatch(predicate); } return false; }
/** * Converts the provided object into a collection * and return the first element, or empty. * * @param obj the obj * @return the optional */ public static Optional<Object> firstElement(final Object obj) { val object = CollectionUtils.toCollection(obj); if (object.isEmpty()) { return Optional.empty(); } return Optional.of(object.iterator().next()); }
@Override public Collection<String> render(final Map<String, Object> attributes) { val formattedAttributes = new ArrayList<String>(attributes.size()); attributes.forEach((key, value) -> { val attributeValues = CollectionUtils.toCollection(value).stream().map(Object::toString).collect(Collectors.joining(",")); formattedAttributes.add(key.concat("=").concat(attributeValues)); }); return formattedAttributes; } }
private Collection<Pair<Pattern, String>> createPatternsAndReturnValue(final String attributeName) { val patternDef = getPatterns().get(attributeName); val patternAndReturnVal = new ArrayList<Object>(CollectionUtils.toCollection(patternDef)); return patternAndReturnVal .stream() .map(this::mapPattern) .collect(Collectors.toList()); }
@Override public AuthenticationBuilder mergeAttribute(final String key, final Object value) { val currentValue = this.attributes.get(key); if (currentValue == null) { return addAttribute(key, value); } val collection = CollectionUtils.toCollection(currentValue); collection.addAll(CollectionUtils.toCollection(value)); return addAttribute(key, collection); }
/** * To collection t. * * @param <T> the type parameter * @param obj the obj * @param clazz the clazz * @return the t */ @SneakyThrows public static <T extends Collection> T toCollection(final Object obj, final Class<T> clazz) { val results = toCollection(obj); if (clazz.isInterface()) { throw new IllegalArgumentException("Cannot accept an interface " + clazz.getSimpleName() + " to create a new object instance"); } val col = clazz.getDeclaredConstructor().newInstance(); col.addAll(results); return col; }
@Override public Collection<String> render(final Map<String, Object> attributes) { val formattedAttributes = new ArrayList<String>(attributes.size()); LOGGER.debug("Beginning to format/render attributes for the response"); attributes.forEach((k, v) -> { val values = CollectionUtils.toCollection(v); values.forEach(value -> { val fmt = buildSingleAttributeDefinitionLine(k, value); LOGGER.debug("Formatted attribute for the response: [{}]", fmt); formattedAttributes.add(fmt); }); }); return formattedAttributes; }
private Collection<MultifactorAuthenticationProvider> getSatisfiedAuthenticationProviders(final Authentication authentication, final Collection<MultifactorAuthenticationProvider> providers) { val contexts = CollectionUtils.toCollection(authentication.getAttributes().get(this.authenticationContextAttribute)); if (contexts == null || contexts.isEmpty()) { LOGGER.debug("No authentication context could be determined based on authentication attribute [{}]", this.authenticationContextAttribute); return null; } return providers.stream() .filter(p -> contexts.contains(p.getId())) .collect(Collectors.toCollection(LinkedHashSet::new)); } }
private static void sanitizeAndTransformAttributeValues(final Map<String, Object> attributes, final RegisteredService registeredService) { LOGGER.trace("Sanitizing attribute values in preparation of the final validation response"); attributes.forEach((key, value) -> { val values = CollectionUtils.toCollection(value); values.stream() .filter(v -> getBinaryAttributeValuePredicate().test(v)) .forEach(v -> attributes.put(key, transformAttributeValueIfNecessary(v))); }); }
private boolean requiredAttributeFound(final String attributeName, final Map<String, Object> principalAttributes, final Map<String, Set<String>> requiredAttributes) { val values = requiredAttributes.get(attributeName); val availableValues = CollectionUtils.toCollection(principalAttributes.get(attributeName)); val pattern = RegexUtils.concatenate(values, this.caseInsensitive); LOGGER.debug("Checking [{}] against [{}] with pattern [{}] for attribute [{}]", values, availableValues, pattern, attributeName); if (pattern != RegexUtils.MATCH_NOTHING_PATTERN) { return availableValues.stream().map(Object::toString).anyMatch(pattern.asPredicate()); } return availableValues.stream().anyMatch(values::contains); } }
@Override public Map<String, List<Object>> getPersonAttributesFromMultivaluedAttributes(final Map<String, List<Object>> attributes) { if (attributes.containsKey("username")) { val username = attributes.get("username"); if (!username.isEmpty()) { val results = new HashMap<String, List<Object>>(); val attrs = getAttributesForUser(username.get(0).toString()); LOGGER.debug("Groovy-based attributes found are [{}]", attrs); attrs.forEach((k, v) -> { val values = new ArrayList<Object>(CollectionUtils.toCollection(v)); LOGGER.debug("Adding Groovy-based attribute [{}] with value(s) [{}]", k, values); results.put(k, values); }); return results; } } return new HashMap<>(0); }
/** * Merge attributes map. * * @param currentAttributes the current attributes * @param attributesToMerge the attributes to merge * @return the map */ public static Map<String, Object> mergeAttributes(final Map<String, Object> currentAttributes, final Map<String, Object> attributesToMerge) { val merger = new MultivaluedAttributeMerger(); val toModify = currentAttributes.entrySet() .stream() .map(entry -> Pair.of(entry.getKey(), CollectionUtils.toCollection(entry.getValue(), ArrayList.class))) .collect(Collectors.toMap(Pair::getKey, Pair::getValue)); val toMerge = attributesToMerge.entrySet() .stream() .map(entry -> Pair.of(entry.getKey(), CollectionUtils.toCollection(entry.getValue(), ArrayList.class))) .collect(Collectors.toMap(Pair::getKey, Pair::getValue)); LOGGER.trace("Merging current attributes [{}] with [{}]", toModify, toMerge); val results = merger.mergeAttributes((Map) toModify, (Map) toMerge); LOGGER.debug("Merged attributes with the final result as [{}]", results); return results; }
/** * Is usage policy accepted by user? * Looks into the attributes collected by the principal to find {@link #aupAttributeName}. * If the attribute contains {@code true}, then the policy is determined as accepted. * * @param principal the principal * @return true if accepted, false otherwise. */ protected boolean isUsagePolicyAcceptedBy(final Principal principal) { val attributes = principal.getAttributes(); LOGGER.debug("Principal attributes found for [{}] are [{}]", principal.getId(), attributes); if (attributes != null && attributes.containsKey(this.aupAttributeName)) { val value = CollectionUtils.toCollection(attributes.get(this.aupAttributeName)); LOGGER.debug("Evaluating attribute value [{}] found for [{}]", value, this.aupAttributeName); return value.stream().anyMatch(v -> v.toString().equalsIgnoreCase(Boolean.TRUE.toString())); } return false; } }
@Override protected InterruptResponse inquireInternal(final Authentication authentication, final RegisteredService registeredService, final Service service, final Credential credential, final RequestContext requestContext) { val attributes = new HashMap<String, Object>(authentication.getAttributes()); attributes.putAll(authentication.getPrincipal().getAttributes()); LOGGER.debug("Looking for [{}] in attributes [{}]", this.interruptAttributeName, attributes); val result = attributes.entrySet() .stream() .filter(entry -> entry.getKey().matches(this.interruptAttributeName)) .filter(entry -> { val values = CollectionUtils.toCollection(entry.getValue()); LOGGER.debug("Located attribute [{}] with values [{}]. Checking for match against [{}]", this.interruptAttributeName, values, this.interruptAttributeValue); return values.stream().anyMatch(value -> value.toString().matches(this.interruptAttributeValue)); }) .findAny(); if (result.isPresent()) { return InterruptResponse.interrupt(); } return InterruptResponse.none(); } }
@Override public Map<String, Object> filter(final Map<String, Object> givenAttributes) { val attributesToRelease = new HashMap<String, Object>(); givenAttributes.entrySet().stream().filter(filterProvidedGivenAttributes()).forEach(entry -> { val attributeName = entry.getKey(); if (patterns.containsKey(attributeName)) { val attributeValues = CollectionUtils.toCollection(entry.getValue()); LOGGER.debug("Found attribute [{}] in pattern definitions with value(s) [{}]", attributeName, attributeValues); val attributePatterns = createPatternForMappedAttribute(attributeName); attributePatterns.forEach(pattern -> { LOGGER.debug("Found attribute [{}] in the pattern definitions. Processing pattern [{}]", attributeName, pattern.pattern()); val filteredValues = filterAttributeValuesByPattern(attributeValues, pattern); LOGGER.debug("Filtered attribute values for [{}] are [{}]", attributeName, filteredValues); if (filteredValues.isEmpty()) { LOGGER.debug("Attribute [{}] has no values remaining and shall be excluded", attributeName); } else { collectAttributeWithFilteredValues(attributesToRelease, attributeName, filteredValues); } }); } else { handleUnmappedAttribute(attributesToRelease, entry.getKey(), entry.getValue()); } }); LOGGER.debug("Received [{}] attributes. Filtered and released [{}]", givenAttributes.size(), attributesToRelease.size()); return attributesToRelease; }
val attributeName = entry.getKey(); if (getPatterns().containsKey(attributeName)) { val attributeValues = CollectionUtils.toCollection(entry.getValue()); LOGGER.debug("Found attribute [{}] in pattern definitions with value(s) [{}]", attributeName, attributeValues); val patterns = createPatternsAndReturnValue(attributeName);
@Override public void authorize(final HttpServletRequest request, final Service service, final Assertion assertion) { val registeredService = this.servicesManager.findServiceBy(service); RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(service, registeredService); if (registeredService.getRequiredHandlers() != null && !registeredService.getRequiredHandlers().isEmpty()) { LOGGER.debug("Evaluating service [{}] to ensure required authentication handlers can satisfy assertion", service); val attributes = assertion.getPrimaryAuthentication().getAttributes(); if (attributes.containsKey(AuthenticationHandler.SUCCESSFUL_AUTHENTICATION_HANDLERS)) { val assertedHandlers = CollectionUtils.toCollection( attributes.get(AuthenticationHandler.SUCCESSFUL_AUTHENTICATION_HANDLERS)); val matchesAll = assertedHandlers.containsAll(registeredService.getRequiredHandlers()); if (!matchesAll) { throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, StringUtils.EMPTY); } } } } }
final Collection<Object> authnMethods = CollectionUtils.toCollection(authentication.getAttributes() .get(SamlAuthenticationMetaDataPopulator.ATTRIBUTE_AUTHENTICATION_METHOD)); LOGGER.debug("Authentication methods found are [{}]", authnMethods);
public ConfigurationMetadataSearchResult(final ConfigurationMetadataProperty prop, final CasConfigurationMetadataRepository repository) { try { setDefaultValue(prop.getDefaultValue()); setDeprecation(prop.getDeprecation()); setDescription(cleanUpDescription(prop.getDescription())); setShortDescription(cleanUpDescription(prop.getShortDescription())); setId(prop.getId()); setName(prop.getName()); setType(prop.getType()); setGroup(CasConfigurationMetadataRepository.getPropertyGroupId(prop)); setOrder(CasConfigurationMetadataRepository.isCasProperty(prop) ? Ordered.HIGHEST_PRECEDENCE : Ordered.LOWEST_PRECEDENCE); val valueHints = prop.getHints().getValueHints(); valueHints.forEach(hint -> { val values = CollectionUtils.toCollection(hint.getValue()); if (values.contains(RequiresModule.class.getName())) { setRequiredModule(hint.getDescription()); setRequiredModuleAutomated(values.contains(Boolean.TRUE)); } if (values.contains(RequiredProperty.class.getName())) { setRequiredProperty(true); } }); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } }