-
Notifications
You must be signed in to change notification settings - Fork 188
/
MidpointProviderManager.java
89 lines (73 loc) · 3.07 KB
/
MidpointProviderManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.security;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.security.provider.MidPointAbstractAuthenticationProvider;
import org.springframework.security.authentication.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
public class MidpointProviderManager implements MidpointAuthenticationManager {
private static final Trace LOGGER = TraceManager.getTrace(MidpointProviderManager.class);
private AuthenticationManager parent;
private List<AuthenticationProvider> providers = new ArrayList<AuthenticationProvider>();
public MidpointProviderManager(List<AuthenticationProvider> providers) {
Assert.notNull(providers, "providers list cannot be null");
this.parent = parent;
}
public List<AuthenticationProvider> getProviders() {
return providers;
}
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
Class<? extends Authentication> toTest = authentication.getClass();
Authentication result = null;
boolean debug = LOGGER.isDebugEnabled();
for (AuthenticationProvider provider : getProviders()) {
if (provider instanceof MidPointAbstractAuthenticationProvider) {
if (! ((MidPointAbstractAuthenticationProvider)provider).supports(toTest, authentication)) {
continue;
}
} else if (!provider.supports(toTest)) {
continue;
}
if (debug) {
LOGGER.debug("Authentication attempt using "
+ provider.getClass().getName());
}
try {
result = provider.authenticate(authentication);
if (result != null) {
copyDetails(authentication, result);
break;
}
}
catch (AccountStatusException e) {
throw e;
}
catch (InternalAuthenticationServiceException e) {
throw e;
}
catch (AuthenticationException e) {
throw e;
}
}
if (result != null) {
return result;
}
throw new ProviderNotFoundException("No AuthenticationProvider found for " + toTest.getName());
}
private void copyDetails(Authentication source, Authentication dest) {
if ((dest instanceof AbstractAuthenticationToken) && (dest.getDetails() == null)) {
AbstractAuthenticationToken token = (AbstractAuthenticationToken) dest;
token.setDetails(source.getDetails());
}
}
}