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;
21 import java.util.concurrent.ConcurrentHashMap;
23 import org.joda.time.DateTime;
27 * Login context created by a profile handler and interpreted
28 * by the authentication package.
30 * Two properties are tracked by default:
32 * <code>forceAuth</code> - Should user authentiation be forced.
33 * <code>passiveAuth</code> - Should user authentication not control the UI.
35 * A Map<String, Object> is provided to store other properties.
36 * Alternatively, a profile handler may create a subclass of LoginContext with
39 * LoginContexts should be created by a profile handler when authentication is needed.
40 * Once control has returned to the profile handler, it should remove the LoginContext
41 * from the HttpSession.
43 * The {@link AuthenticationManager} or an {@link AuthenticationHandler} should set the
44 * {@link LoginContext#setAuthenticationAttempted()}, {@link LoginContext#setAuthnOK(boolean)},
45 * {@link LoginContext#setAuthnFailure(String)}, {@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";
55 /** Should user authentication be forced */
56 protected boolean forceAuth = false;
58 /** Must authentication not interact with the UI */
59 protected boolean passiveAuth = false;
61 /** a catch-all map for other properties */
62 protected Map<String, Object> propsMap = new ConcurrentHashMap<String, Object>();;
64 /** The ProfileHandler URL */
65 protected String profileHandlerURL;
67 /** The AuthenticationManager's URL */
68 protected String authnManagerURL;
70 /** has authentication been attempted yet */
71 protected boolean authnAttempted = false;
73 /** The id of the authenticated user */
74 protected String userID;
76 /** Did authentication succceed? */
77 protected boolean authenticationOK;
79 /** Optional failure message */
80 protected String authnFailureMessage;
82 /** The instant of authentication */
83 protected DateTime authnInstant;
85 /** The duration of authentication */
86 protected long authnDuration;
88 /** The method used to authenticate the user */
89 protected String authnMethod;
92 protected String sessionID;
95 /** Creates a new instance of LoginContext */
96 public LoginContext() {
101 * Creates a new instance of LoginContext
103 * @param forceAuth if the authentication manager must reauth the user.
104 * @param passiveAuth if the authentication manager must not interact with the users UI.
106 public LoginContext(boolean forceAuth, boolean passiveAuth) {
108 forceAuth = forceAuth;
109 passiveAuth = passiveAuth;
114 * Returns if authentication must be forced.
116 * @return <code>true</code> if the authentication manager must reauth the user.
118 public boolean getForceAuth() {
124 * Returns if authentication must be passive.
126 * @return <code>true</code> if the authentication manager must not interact with the users UI.
128 public boolean getPassiveAuth() {
134 * Sets if authentication must be forced.
136 * @param forceAuth if the authentication manager must reauth the user.
138 public void setForceAuth(boolean forceAuth) {
139 this.forceAuth = forceAuth;
144 * Sets if authentication must be passive.
146 * @param passiveAuth if the authentication manager must not interact with the users UI.
148 public void setPassiveAuth(boolean passiveAuth) {
149 this.passiveAuth = passiveAuth;
154 * Get an optional property object.
156 * @param key The key in the properites Map.
158 * @return The object, or <code>null</code> is no object exists for the key.
160 public Object getProperty(String key) {
161 return propsMap.get(key);
166 * Sets an optional property object.
168 * If an object is already associated with key, it will be overwritten.
170 * @param key The key to set.
171 * @param obj The object to associate with key.
173 public void setProperty(String key, final Object obj) {
174 propsMap.put(key, obj);
179 * Sets if authentication succeeded.
181 * @param authnOK if authentication succeeded;
183 public void setAuthenticationOK(boolean authnOK) {
184 this.authenticationOK = authnOK;
189 * Returns if authentication succeeded.
191 * @return <code>true</code> is the user was successfully authenticated.
193 public boolean getAuthenticationOK() {
194 return authenticationOK;
198 /** Sets the optional authentication failure message.
200 * @param failureMessage A description of why authN failed.
202 public void setAuthenticationFailureMessage(String failureMessage) {
203 authnFailureMessage = failureMessage;
208 * Returns the optional authentication failure message.
210 * @return The failure message, or <code>null</code> is none was set.
212 public String getAuthenticationFailureMessage() {
213 return authnFailureMessage;
218 * Set if authentication has been attempted.
220 * This method should be called by an {@link AuthenticationHandler}
221 * while processing a request.
223 public void setAuthenticationAttempted() {
224 authnAttempted = true;
229 * Returns if authentication has been attempted for this user.
231 public boolean getAuthenticationAttempted() {
232 return authnAttempted;
237 * Sets the ID of the authenticated user.
239 * @param userID The userid.
241 public void setUserID(String userID) {
242 this.userID = userID;
247 * Returns the ID of the authenticated user.
249 * @return the ID of the user, or <code>null</code> if authentication failed.
251 public String getUserID() {
257 * Gets the ProfileHandler URL.
259 * @return the URL of the profile handler that is invoking the Authentication Manager.
261 public String getProfileHandlerURL() {
262 return profileHandlerURL;
267 * Sets the ProfileHandler URL.
269 * @param profileHandlerURL The URL of the profile handler that invoked the AuthenticationManager/
271 public void setProfileHandlerURL(String profileHandlerURL) {
272 this.profileHandlerURL = profileHandlerURL;
277 * Gets the AuthenticationManager URL.
279 * @return the URL of the AuthenticationManager.
281 public String getAuthenticationManagerURL() {
282 return authnManagerURL;
287 * Sets the AuthenticationManager's URL.
289 * @param authnManagerURL the URL of the AuthenticationManager.
291 public void setAuthenticationManagerURL(String authnManagerURL) {
292 this.authnManagerURL = authnManagerURL;
297 * Gets the authentication instant.
299 * @return The instant of authentication, or <code>null</code> if none was set.
301 public DateTime getAuthenticationInstant() {
307 * Sets the authentication instant.
309 * @param authnInstant The instant of authentication.
311 public void setAuthenticationInstant(final DateTime authnInstant) {
312 this.authnInstant = authnInstant;
317 * Gets the duration of authentication.
319 * @return The duration of authentication, or zero if none was set.
321 public long getAuthenticationDuration() {
322 return authnDuration;
327 * Sets the duration of authentication.
329 * @param authnDuration The duration of authentication.
331 public void setAuthenticationDuration(long authnDuration) {
332 this.authnDuration = authnDuration;
337 * Gets the method used to authenticate the user.
339 * @return The method used to authenticate the user.
341 public String getAuthenticationMethod() {
347 * Sets the method used to authenticate the user.
349 * @param authnMethod The method used to authenticate the user.
351 public void setAuthenticationMethod(String authnMethod) {
352 this.authnMethod = authnMethod;
357 * Gets the {@link Session} ID
359 * @return the Session id.
361 public String getSessionID() {
367 * Sets the {@link Session} ID
369 * @param sessionID the Session ID
371 public void setSessionID(String sessionID) {
372 this.sessionID = sessionID;
377 * Return the acceptable authentication handler URIs
378 * for authenticating this user. If no authentication
379 * methods are preferred, this method will return
382 * @return an array of URIs, or <code>null</code>.
384 public String[] getRequestedAuthenticationMethods() {