AuthenticationManagerImpl.java
7.59 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at the following location:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.cas.authentication;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.handler.AuthenticationHandler;
import org.jasig.cas.authentication.handler.BadCredentialsAuthenticationException;
import org.jasig.cas.authentication.handler.UnsupportedCredentialsException;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver;
import org.jasig.cas.authentication.principal.WeaverCredentialsToPrincipalResolver;
import org.jasig.cas.authentication.principal.Principal;
import weaver.loginsso.WeaverHttpAuthenticationHandler;
/**
* <p>
* Default implementation of the AuthenticationManager. The
* AuthenticationManager follows the following algorithm. The manager loops
* through the array of AuthenticationHandlers searching for one that can
* attempt to determine the validity of the credentials. If it finds one, it
* tries that one. If that handler returns true, it continues on. If it returns
* false, it looks for another handler. If it throws an exception, it aborts the
* whole process and rethrows the exception. Next, it looks for a
* CredentialsToPrincipalResolver that can handle the credentials in order to
* create a Principal. Finally, it attempts to populate the Authentication
* object's attributes map using AuthenticationAttributesPopulators
* <p>
* Behavior is determined by external beans attached through three configuration
* properties. The Credentials are opaque to the manager. They are passed to the
* external beans to see if any can process the actual type represented by the
* Credentials marker.
* <p>
* AuthenticationManagerImpl requires the following properties to be set:
* </p>
* <ul>
* <li> <code>authenticationHandlers</code> - The array of
* AuthenticationHandlers that know how to process the credentials provided.
* <li> <code>credentialsToPrincipalResolvers</code> - The array of
* CredentialsToPrincipal resolvers that know how to process the credentials
* provided.
* </ul>
*
* @author Scott Battaglia
* @version $Revision$ $Date$
* @since 3.0
* @see org.jasig.cas.authentication.handler.AuthenticationHandler
* @see org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver
* @see org.jasig.cas.authentication.AuthenticationMetaDataPopulator
*/
public final class AuthenticationManagerImpl extends AbstractAuthenticationManager {
/** An array of authentication handlers. */
@NotNull
@Size(min=1)
private List<AuthenticationHandler> authenticationHandlers;
/** An array of CredentialsToPrincipalResolvers. */
@NotNull
@Size(min=1)
private List<CredentialsToPrincipalResolver> credentialsToPrincipalResolvers;
@Override
protected Pair<AuthenticationHandler, Principal> authenticateAndObtainPrincipal(final Credentials credentials) throws AuthenticationException {
boolean foundSupported = false;
boolean authenticated = false;
AuthenticationHandler authenticatedClass = null;
String handlerName;
for ( AuthenticationHandler authenticationHandler : this.authenticationHandlers) {
if (authenticationHandler.supports(credentials)) {
foundSupported = true;
handlerName = authenticationHandler.getClass().getName();
try {
if (!authenticationHandler.authenticate(credentials)) {
log.info("{} failed to authenticate {}", handlerName, credentials);
} else {
log.info("{} successfully authenticated {}", handlerName, credentials);
authenticatedClass = authenticationHandler;
authenticated = true;
break;
}
} catch (final Exception e) {
handleError(handlerName, credentials, e);
}
}
}
if (!authenticated) {
if (foundSupported) {
throw BadCredentialsAuthenticationException.ERROR;
}
throw UnsupportedCredentialsException.ERROR;
}
foundSupported = false;
for (CredentialsToPrincipalResolver credentialsToPrincipalResolver : this.credentialsToPrincipalResolvers) {
if (credentialsToPrincipalResolver.supports(credentials)) {
if (credentialsToPrincipalResolver instanceof WeaverCredentialsToPrincipalResolver) {
credentialsToPrincipalResolver = (WeaverCredentialsToPrincipalResolver) credentialsToPrincipalResolver;
for (int i=0;i<this.authenticationHandlers.size();i++) {
AuthenticationHandler handler = this.authenticationHandlers.get(i);
if (handler instanceof WeaverHttpAuthenticationHandler) {
handler = (WeaverHttpAuthenticationHandler) handler;
((WeaverCredentialsToPrincipalResolver) credentialsToPrincipalResolver).setLoginid(((WeaverHttpAuthenticationHandler) handler).getLoginid());
((WeaverCredentialsToPrincipalResolver) credentialsToPrincipalResolver).setTs(((WeaverHttpAuthenticationHandler) handler).getTs());
}
}
}
Principal principal = credentialsToPrincipalResolver.resolvePrincipal(credentials);
log.info("Resolved principal " + principal);
//System.out.println(principal);
foundSupported = true;
if (principal != null) {
return new Pair<AuthenticationHandler,Principal>(authenticatedClass, principal);
}
}
}
if (foundSupported) {
if (log.isDebugEnabled()) {
log.debug("CredentialsToPrincipalResolver found but no principal returned.");
}
throw BadCredentialsAuthenticationException.ERROR;
}
log.error("CredentialsToPrincipalResolver not found for " + credentials.getClass().getName());
throw UnsupportedCredentialsException.ERROR;
}
/**
* @param authenticationHandlers The authenticationHandlers to set.
*/
public void setAuthenticationHandlers(
final List<AuthenticationHandler> authenticationHandlers) {
this.authenticationHandlers = authenticationHandlers;
}
/**
* @param credentialsToPrincipalResolvers The
* credentialsToPrincipalResolvers to set.
*/
public void setCredentialsToPrincipalResolvers(
final List<CredentialsToPrincipalResolver> credentialsToPrincipalResolvers) {
this.credentialsToPrincipalResolvers = credentialsToPrincipalResolvers;
}
}