2 * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.idp.authn;
20 import java.util.concurrent.ConcurrentHashMap;
22 import org.joda.time.DateTime;
25 * Login context created by a profile handler and interpreted by the
26 * authentication package.
28 * Two properties are tracked by default:
30 * <code>forceAuth</code> - Should user authentiation be forced.
31 * <code>passiveAuth</code> - Should user authentication not control the UI.
33 * A Map<String, Object> is provided to store other properties.
34 * Alternatively, a profile handler may create a subclass of LoginContext with
37 * LoginContexts should be created by a profile handler when authentication is
38 * needed. Once control has returned to the profile handler, it should remove
39 * the LoginContext from the HttpSession.
41 * The {@link AuthenticationManager} or an {@link AuthenticationHandler} should
42 * set the {@link LoginContext#setAuthenticationAttempted()},
43 * {@link LoginContext#setAuthnOK(boolean)},
44 * {@link LoginContext#setAuthnFailure(String)},
45 * {@link LoginContext#{setAuthenticationDuration(long)}
46 * {@link LoginContext#setAuthenticationInstant(DateTime)} appropriately.
49 public class LoginContext {
51 /** the key in a HttpSession where login contexts are stored */
52 public static final String LOGIN_CONTEXT_KEY = "shib2.logincontext";
54 /** Should user authentication be forced */
55 protected boolean forceAuth = false;
57 /** Must authentication not interact with the UI */
58 protected boolean passiveAuth = false;
60 /** a catch-all map for other properties */
61 protected Map<String, Object> propsMap = new ConcurrentHashMap<String, Object>();;
63 /** The ProfileHandler URL */
64 protected String profileHandlerURL;
66 /** The AuthenticationManager's URL */
67 protected String authnManagerURL;
69 /** has authentication been attempted yet */
70 protected boolean authnAttempted = false;
72 /** The id of the authenticated user */
73 protected String userID;
75 /** Did authentication succceed? */
76 protected boolean authenticationOK;
78 /** Optional failure message */
79 protected String authnFailureMessage;
81 /** The instant of authentication */
82 protected DateTime authnInstant;
84 /** The duration of authentication */
85 protected long authnDuration;
87 /** The method used to authenticate the user */
88 protected String authnMethod;
91 protected String sessionID;
93 /** Creates a new instance of LoginContext */
94 public LoginContext() {
98 * Creates a new instance of LoginContext
101 * if the authentication manager must reauth the user.
103 * if the authentication manager must not interact with the users
106 public LoginContext(boolean forceAuth, boolean passiveAuth) {
108 forceAuth = forceAuth;
109 passiveAuth = passiveAuth;
113 * Returns if authentication must be forced.
115 * @return <code>true</code> if the authentication manager must reauth the
118 public boolean getForceAuth() {
123 * Returns if authentication must be passive.
125 * @return <code>true</code> if the authentication manager must not
126 * interact with the users UI.
128 public boolean getPassiveAuth() {
133 * Sets if authentication must be forced.
136 * if the authentication manager must reauth the user.
138 public void setForceAuth(boolean forceAuth) {
139 this.forceAuth = forceAuth;
143 * Sets if authentication must be passive.
146 * if the authentication manager must not interact with the users
149 public void setPassiveAuth(boolean passiveAuth) {
150 this.passiveAuth = passiveAuth;
154 * Get an optional property object.
157 * The key in the properites Map.
159 * @return The object, or <code>null</code> is no object exists for the
162 public Object getProperty(String key) {
163 return propsMap.get(key);
167 * Sets an optional property object.
169 * If an object is already associated with key, it will be overwritten.
174 * The object to associate with key.
176 public void setProperty(String key, final Object obj) {
177 propsMap.put(key, obj);
181 * Sets if authentication succeeded.
184 * if authentication succeeded;
186 public void setAuthenticationOK(boolean authnOK) {
187 this.authenticationOK = authnOK;
191 * Returns if authentication succeeded.
193 * @return <code>true</code> is the user was successfully authenticated.
195 public boolean getAuthenticationOK() {
196 return authenticationOK;
200 * Sets the optional authentication failure message.
202 * @param failureMessage
203 * A description of why authN failed.
205 public void setAuthenticationFailureMessage(String failureMessage) {
206 authnFailureMessage = failureMessage;
210 * Returns the optional authentication failure message.
212 * @return The failure message, or <code>null</code> is none was set.
214 public String getAuthenticationFailureMessage() {
215 return authnFailureMessage;
219 * Set if authentication has been attempted.
221 * This method should be called by an {@link AuthenticationHandler} while
222 * processing a request.
224 public void setAuthenticationAttempted() {
225 authnAttempted = true;
229 * Returns if authentication has been attempted for this user.
231 public boolean getAuthenticationAttempted() {
232 return authnAttempted;
236 * Sets the ID of the authenticated user.
241 public void setUserID(String userID) {
242 this.userID = userID;
246 * Returns the ID of the authenticated user.
248 * @return the ID of the user, or <code>null</code> if authentication
251 public String getUserID() {
256 * Gets the ProfileHandler URL.
258 * @return the URL of the profile handler that is invoking the
259 * Authentication Manager.
261 public String getProfileHandlerURL() {
262 return profileHandlerURL;
266 * Sets the ProfileHandler URL.
268 * @param profileHandlerURL
269 * The URL of the profile handler that invoked the
270 * AuthenticationManager/
272 public void setProfileHandlerURL(String profileHandlerURL) {
273 this.profileHandlerURL = profileHandlerURL;
277 * Gets the AuthenticationManager URL.
279 * @return the URL of the AuthenticationManager.
281 public String getAuthenticationManagerURL() {
282 return authnManagerURL;
286 * Sets the AuthenticationManager's URL.
288 * @param authnManagerURL
289 * the URL of the AuthenticationManager.
291 public void setAuthenticationManagerURL(String authnManagerURL) {
292 this.authnManagerURL = authnManagerURL;
296 * Gets the authentication instant.
298 * @return The instant of authentication, or <code>null</code> if none was
301 public DateTime getAuthenticationInstant() {
306 * Sets the authentication instant.
308 * @param authnInstant
309 * The instant of authentication.
311 public void setAuthenticationInstant(final DateTime authnInstant) {
312 this.authnInstant = authnInstant;
316 * Gets the duration of authentication.
318 * @return The duration of authentication, or zero if none was set.
320 public long getAuthenticationDuration() {
321 return authnDuration;
325 * Sets the duration of authentication.
327 * @param authnDuration
328 * The duration of authentication.
330 public void setAuthenticationDuration(long authnDuration) {
331 this.authnDuration = authnDuration;
335 * Gets the method used to authenticate the user.
337 * @return The method used to authenticate the user.
339 public String getAuthenticationMethod() {
344 * Sets the method used to authenticate the user.
347 * The method used to authenticate the user.
349 public void setAuthenticationMethod(String authnMethod) {
350 this.authnMethod = authnMethod;
354 * Gets the {@link Session} ID
356 * @return the Session id.
358 public String getSessionID() {
363 * Sets the {@link Session} ID
368 public void setSessionID(String sessionID) {
369 this.sessionID = sessionID;
373 * Return the acceptable authentication handler URIs for authenticating this
374 * user. If no authentication methods are preferred, this method will return
377 * @return an array of URIs, or <code>null</code>.
379 public String[] getRequestedAuthenticationMethods() {