use of org.apereo.cas.authentication.principal.Principal 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.apereo.cas.authentication.principal.Principal 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.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class MultifactorAuthenticationUtils method buildEventAttributeMap.
/**
* Build event attribute map map.
*
* @param principal the principal
* @param service the service
* @param provider the provider
* @return the map
*/
public static Map<String, Object> buildEventAttributeMap(final Principal principal, final Optional<RegisteredService> service, final MultifactorAuthenticationProvider provider) {
val map = new HashMap<String, Object>();
map.put(Principal.class.getName(), principal);
service.ifPresent(svc -> map.put(RegisteredService.class.getName(), svc));
map.put(MultifactorAuthenticationProvider.class.getName(), provider);
return map;
}
use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class AbstractRegisteredServiceAttributeReleasePolicy method resolveAttributesFromPrincipalAttributeRepository.
/**
* Resolve attributes from principal attribute repository.
*
* @param principal the principal
* @param registeredService the registered service
* @return the map
*/
protected Map<String, List<Object>> resolveAttributesFromPrincipalAttributeRepository(final Principal principal, final RegisteredService registeredService) {
val attributes = getRegisteredServicePrincipalAttributesRepository().map(repository -> {
LOGGER.debug("Using principal attribute repository [{}] to retrieve attributes", repository);
return repository.getAttributes(principal, registeredService);
}).orElseGet(principal::getAttributes);
LOGGER.debug("Attributes retrieved from principal attribute repository for [{}] are [{}]", principal.getId(), attributes);
return attributes;
}
use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.
the class RegisteredServicePrincipalAttributeMultifactorAuthenticationTrigger method isActivated.
@Override
public Optional<MultifactorAuthenticationProvider> isActivated(final Authentication authentication, final RegisteredService registeredService, final HttpServletRequest httpServletRequest, final HttpServletResponse response, final Service service) {
if (authentication == null || registeredService == null) {
LOGGER.debug("No authentication or service is available to determine event for principal");
return Optional.empty();
}
val policy = registeredService.getMultifactorPolicy();
if (policy == null || registeredService.getMultifactorPolicy().getMultifactorAuthenticationProviders().isEmpty()) {
LOGGER.trace("Authentication policy is absent or does not contain any multifactor authentication providers");
return Optional.empty();
}
if (StringUtils.isBlank(policy.getPrincipalAttributeNameTrigger()) || StringUtils.isBlank(policy.getPrincipalAttributeValueToMatch())) {
LOGGER.debug("Authentication policy does not define a principal attribute and/or value to trigger multifactor authentication");
return Optional.empty();
}
val principal = multifactorAuthenticationProviderResolver.resolvePrincipal(authentication.getPrincipal());
val providers = MultifactorAuthenticationUtils.getMultifactorAuthenticationProviderForService(registeredService, applicationContext);
if (providers.size() > 1) {
val resolvedProvider = multifactorAuthenticationProviderSelector.resolve(providers, registeredService, principal);
providers.clear();
providers.add(resolvedProvider);
}
LOGGER.debug("Resolved multifactor providers are [{}]", providers);
val result = multifactorAuthenticationProviderResolver.resolveEventViaPrincipalAttribute(principal, org.springframework.util.StringUtils.commaDelimitedListToSet(policy.getPrincipalAttributeNameTrigger()), registeredService, Optional.empty(), providers, (attributeValue, mfaProvider) -> attributeValue != null && RegexUtils.matches(Pattern.compile(policy.getPrincipalAttributeValueToMatch()), attributeValue));
if (result != null && !result.isEmpty()) {
return CollectionUtils.firstElement(result).map(value -> MultifactorAuthenticationUtils.getMultifactorAuthenticationProviderById(value.toString(), this.applicationContext)).orElseGet(() -> unmatchedMultifactorAuthenticationTrigger(principal, registeredService));
}
return unmatchedMultifactorAuthenticationTrigger(principal, registeredService);
}
Aggregations