Search in sources :

Example 76 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class AbstractPrincipalAttributeAcceptableUsagePolicyRepository method verify.

@Override
public Pair<Boolean, Principal> verify(final RequestContext requestContext, final Credential credential) {
    final Principal principal = WebUtils.getPrincipalFromRequestContext(requestContext, this.ticketRegistrySupport);
    final Map<String, Object> attributes = principal.getAttributes();
    LOGGER.debug("Principal attributes found for [{}] are [{}]", principal.getId(), attributes);
    if (attributes != null && attributes.containsKey(this.aupAttributeName)) {
        final Object value = attributes.get(this.aupAttributeName);
        LOGGER.debug("Evaluating attribute value [{}] found for [{}]", value, this.aupAttributeName);
        if (value.toString().equalsIgnoreCase(Boolean.TRUE.toString())) {
            return Pair.of(true, principal);
        }
    }
    LOGGER.warn("Usage policy has not been accepted by [{}]", principal.getId());
    return Pair.of(false, principal);
}
Also used : Principal(org.apereo.cas.authentication.principal.Principal)

Example 77 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class PrincipalAttributeMultifactorAuthenticationPolicyEventResolver method resolveInternal.

@Override
public Set<Event> resolveInternal(final RequestContext context) {
    final RegisteredService service = resolveRegisteredServiceInRequestContext(context);
    final Authentication authentication = WebUtils.getAuthentication(context);
    if (service == null || authentication == null) {
        LOGGER.debug("No service or authentication is available to determine event for principal");
        return null;
    }
    final Principal principal = authentication.getPrincipal();
    if (attributeNames.isEmpty()) {
        LOGGER.debug("Attribute name to determine event is not configured for [{}]", principal.getId());
        return null;
    }
    final Map<String, MultifactorAuthenticationProvider> providerMap = WebUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    if (providerMap == null || providerMap.isEmpty()) {
        LOGGER.error("No multifactor authentication providers are available in the application context");
        return null;
    }
    final Collection<MultifactorAuthenticationProvider> providers = flattenProviders(providerMap.values());
    if (providers.size() == 1 && StringUtils.isNotBlank(globalPrincipalAttributeValueRegex)) {
        final MultifactorAuthenticationProvider provider = providers.iterator().next();
        LOGGER.debug("Found a single multifactor provider [{}] in the application context", provider);
        return resolveEventViaPrincipalAttribute(principal, attributeNames, service, context, providers, input -> input != null && input.matches(globalPrincipalAttributeValueRegex));
    }
    return resolveEventViaPrincipalAttribute(principal, attributeNames, service, context, providers, input -> providers.stream().filter(provider -> input != null && provider.matches(input)).count() > 0);
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) Logger(org.slf4j.Logger) Audit(org.apereo.inspektr.audit.annotation.Audit) Collection(java.util.Collection) MultifactorAuthenticationProviderSelector(org.apereo.cas.services.MultifactorAuthenticationProviderSelector) LoggerFactory(org.slf4j.LoggerFactory) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) AuthenticationServiceSelectionPlan(org.apereo.cas.authentication.AuthenticationServiceSelectionPlan) TicketRegistrySupport(org.apereo.cas.ticket.registry.TicketRegistrySupport) Set(java.util.Set) StringUtils(org.apache.commons.lang3.StringUtils) RequestContext(org.springframework.webflow.execution.RequestContext) RegisteredService(org.apereo.cas.services.RegisteredService) BaseMultifactorAuthenticationProviderEventResolver(org.apereo.cas.web.flow.authentication.BaseMultifactorAuthenticationProviderEventResolver) Authentication(org.apereo.cas.authentication.Authentication) StringUtils.commaDelimitedListToSet(org.springframework.util.StringUtils.commaDelimitedListToSet) Map(java.util.Map) AuthenticationSystemSupport(org.apereo.cas.authentication.AuthenticationSystemSupport) Principal(org.apereo.cas.authentication.principal.Principal) WebUtils(org.apereo.cas.web.support.WebUtils) CookieGenerator(org.springframework.web.util.CookieGenerator) Event(org.springframework.webflow.execution.Event) ServicesManager(org.apereo.cas.services.ServicesManager) RegisteredService(org.apereo.cas.services.RegisteredService) Authentication(org.apereo.cas.authentication.Authentication) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) Principal(org.apereo.cas.authentication.principal.Principal)

Example 78 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class RestEndpointMultifactorAuthenticationPolicyEventResolver method resolveInternal.

@Override
public Set<Event> resolveInternal(final RequestContext context) {
    final RegisteredService service = resolveRegisteredServiceInRequestContext(context);
    final Authentication authentication = WebUtils.getAuthentication(context);
    final String restEndpoint = this.restEndpoint;
    if (service == null || authentication == null) {
        LOGGER.debug("No service or authentication is available to determine event for principal");
        return null;
    }
    final Principal principal = authentication.getPrincipal();
    if (StringUtils.isBlank(restEndpoint)) {
        LOGGER.debug("Rest endpoint to determine event is not configured for [{}]", principal.getId());
        return null;
    }
    final Map<String, MultifactorAuthenticationProvider> providerMap = WebUtils.getAvailableMultifactorAuthenticationProviders(this.applicationContext);
    if (providerMap == null || providerMap.isEmpty()) {
        LOGGER.error("No multifactor authentication providers are available in the application context");
        return null;
    }
    final Collection<MultifactorAuthenticationProvider> flattenedProviders = flattenProviders(providerMap.values());
    LOGGER.debug("Contacting [{}] to inquire about [{}]", restEndpoint, principal.getId());
    final RestTemplate restTemplate = new RestTemplate();
    final ResponseEntity<String> responseEntity = restTemplate.postForEntity(restEndpoint, principal.getId(), String.class);
    if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
        final String results = responseEntity.getBody();
        if (StringUtils.isNotBlank(results)) {
            LOGGER.debug("Result returned from the rest endpoint is [{}]", results);
            final MultifactorAuthenticationProvider restProvider = flattenedProviders.stream().filter(p -> p.matches(results)).findFirst().orElse(null);
            if (restProvider != null) {
                LOGGER.debug("Found multifactor authentication provider [{}]", restProvider.getId());
                return Collections.singleton(new Event(this, restProvider.getId()));
            }
            LOGGER.debug("No multifactor authentication provider could be matched against [{}]", results);
            return Collections.emptySet();
        }
    }
    LOGGER.debug("No providers are available to match rest endpoint results");
    return Collections.emptySet();
}
Also used : RegisteredService(org.apereo.cas.services.RegisteredService) Authentication(org.apereo.cas.authentication.Authentication) RestTemplate(org.springframework.web.client.RestTemplate) Event(org.springframework.webflow.execution.Event) MultifactorAuthenticationProvider(org.apereo.cas.services.MultifactorAuthenticationProvider) Principal(org.apereo.cas.authentication.principal.Principal)

Example 79 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class DefaultAuthenticationResultBuilder method buildAuthentication.

private Authentication buildAuthentication() {
    if (isEmpty()) {
        LOGGER.warn("No authentication event has been recorded; CAS cannot finalize the authentication result");
        return null;
    }
    final Map<String, Object> authenticationAttributes = new HashMap<>();
    final Map<String, Object> principalAttributes = new HashMap<>();
    final AuthenticationBuilder authenticationBuilder = DefaultAuthenticationBuilder.newInstance();
    buildAuthenticationHistory(this.authentications, authenticationAttributes, principalAttributes, authenticationBuilder);
    final Principal primaryPrincipal = getPrimaryPrincipal(this.authentications, principalAttributes);
    authenticationBuilder.setPrincipal(primaryPrincipal);
    LOGGER.debug("Determined primary authentication principal to be [{}]", primaryPrincipal);
    authenticationBuilder.setAttributes(authenticationAttributes);
    LOGGER.debug("Collected authentication attributes for this result are [{}]", authenticationAttributes);
    authenticationBuilder.setAuthenticationDate(ZonedDateTime.now());
    final Authentication auth = authenticationBuilder.build();
    LOGGER.debug("Authentication result commenced at [{}]", auth.getAuthenticationDate());
    return auth;
}
Also used : HashMap(java.util.HashMap) Principal(org.apereo.cas.authentication.principal.Principal)

Example 80 with Principal

use of org.apereo.cas.authentication.principal.Principal in project cas by apereo.

the class PolicyBasedAuthenticationManager method authenticate.

@Override
@Audit(action = "AUTHENTICATION", actionResolverName = "AUTHENTICATION_RESOLVER", resourceResolverName = "AUTHENTICATION_RESOURCE_RESOLVER")
@Timed(name = "AUTHENTICATE_TIMER")
@Metered(name = "AUTHENTICATE_METER")
@Counted(name = "AUTHENTICATE_COUNT", monotonic = true)
public Authentication authenticate(final AuthenticationTransaction transaction) throws AuthenticationException {
    AuthenticationCredentialsThreadLocalBinder.bindCurrent(transaction.getCredentials());
    final AuthenticationBuilder builder = authenticateInternal(transaction);
    AuthenticationCredentialsThreadLocalBinder.bindCurrent(builder);
    final Authentication authentication = builder.build();
    addAuthenticationMethodAttribute(builder, authentication);
    populateAuthenticationMetadataAttributes(builder, transaction);
    invokeAuthenticationPostProcessors(builder, transaction);
    final Authentication auth = builder.build();
    final Principal principal = auth.getPrincipal();
    if (principal instanceof NullPrincipal) {
        throw new UnresolvedPrincipalException(auth);
    }
    LOGGER.info("Authenticated principal [{}] with attributes [{}] via credentials [{}].", principal.getId(), principal.getAttributes(), transaction.getCredentials());
    AuthenticationCredentialsThreadLocalBinder.bindCurrent(auth);
    return auth;
}
Also used : NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) UnresolvedPrincipalException(org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal) Audit(org.apereo.inspektr.audit.annotation.Audit) Counted(com.codahale.metrics.annotation.Counted) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Principal (org.apereo.cas.authentication.principal.Principal)114 HashMap (java.util.HashMap)33 RegisteredService (org.apereo.cas.services.RegisteredService)31 Test (org.junit.Test)29 Authentication (org.apereo.cas.authentication.Authentication)26 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)26 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)26 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)25 Map (java.util.Map)23 Slf4j (lombok.extern.slf4j.Slf4j)23 lombok.val (lombok.val)19 List (java.util.List)15 StringUtils (org.apache.commons.lang3.StringUtils)15 OAuthCode (org.apereo.cas.ticket.code.OAuthCode)15 CollectionUtils (org.apereo.cas.util.CollectionUtils)15 ArrayList (java.util.ArrayList)14 Optional (java.util.Optional)14 Service (org.apereo.cas.authentication.principal.Service)14 Collection (java.util.Collection)11 Collectors (java.util.stream.Collectors)10