org.apereo.cas.authentication.RegisteredServiceAuthenticationHandlerResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
c 0
b 0
f 0
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
F resolve(Set,AuthenticationTransaction) 0 33 9
A RegisteredServiceAuthenticationHandlerResolver(ServicesManager) 0 2 1
1
package org.apereo.cas.authentication;
2
3
import org.apereo.cas.services.RegisteredService;
4
import org.apereo.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler;
5
import org.apereo.cas.authentication.principal.Service;
6
import org.apereo.cas.services.ServicesManager;
7
import org.apereo.cas.services.UnauthorizedSsoServiceException;
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
11
import java.util.Iterator;
12
import java.util.LinkedHashSet;
13
import java.util.Set;
14
import java.util.stream.Collectors;
15
16
/**
17
 * This is {@link RegisteredServiceAuthenticationHandlerResolver}
18
 * that acts on the criteria presented by a registered service to
19
 * detect which handler(s) should be resolved for authentication.
20
 *
21
 * @author Misagh Moayyed
22
 * @since 5.0.0
23
 */
24
public class RegisteredServiceAuthenticationHandlerResolver implements AuthenticationHandlerResolver {
25
    private static final Logger LOGGER = LoggerFactory.getLogger(RegisteredServiceAuthenticationHandlerResolver.class);
26
    /**
27
     * The Services manager.
28
     */
29
    protected final ServicesManager servicesManager;
30
31
    /**
32
     * Instantiates a new Registered service authentication handler resolver.
33
     *
34
     * @param servicesManager the services manager
35
     */
36
    public RegisteredServiceAuthenticationHandlerResolver(final ServicesManager servicesManager) {
37
        this.servicesManager = servicesManager;
38
    }
39
40
    @Override
41
    public Set<AuthenticationHandler> resolve(final Set<AuthenticationHandler> candidateHandlers,
42
                                              final AuthenticationTransaction transaction) {
43
        final Service service = transaction.getService();
44
        if (service != null && this.servicesManager != null) {
45
            final RegisteredService registeredService = this.servicesManager.findServiceBy(service);
46
            if (registeredService == null || !registeredService.getAccessStrategy().isServiceAccessAllowed()) {
47
                LOGGER.warn("Service [{}] is not allowed to use SSO.", registeredService);
48
                throw new UnauthorizedSsoServiceException();
49
            }
50
            if (!registeredService.getRequiredHandlers().isEmpty()) {
51
                LOGGER.debug("Authentication transaction requires [{}] for service [{}]", registeredService.getRequiredHandlers(), service);
52
                final Set<AuthenticationHandler> handlerSet = new LinkedHashSet<>(candidateHandlers);
53
                LOGGER.info("Candidate authentication handlers examined this transaction are [{}]", handlerSet);
54
55
                final Iterator<AuthenticationHandler> it = handlerSet.iterator();
56
                while (it.hasNext()) {
57
                    final AuthenticationHandler handler = it.next();
58
                    if (!(handler instanceof HttpBasedServiceCredentialsAuthenticationHandler)
59
                            && !registeredService.getRequiredHandlers().contains(handler.getName())) {
60
                        LOGGER.debug("Authentication handler [{}] is not required for this transaction and is removed", handler.getName());
61
                        it.remove();
62
                    }
63
                }
64
                LOGGER.debug("Authentication handlers for this transaction are [{}]", handlerSet);
65
                return handlerSet;
66
            }
67
            LOGGER.debug("No specific authentication handlers are required for this transaction");
68
        }
69
70
        final String handlers = candidateHandlers.stream().map(AuthenticationHandler::getName).collect(Collectors.joining(","));
71
        LOGGER.debug("Authentication handlers used for this transaction are [{}]", handlers);
72
        return candidateHandlers;
73
    }
74
}
75