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 javolution.util.FastMap;
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)} appropriately.
48 public class LoginContext {
50 /** the key in a HttpSession where login contexts are stored */
51 public static final String LOGIN_CONTEXT_KEY = "shib2.logincontext";
54 /** Should user authentication be forced */
55 private boolean forceAuth = false;
57 /** Must authentication not interact with the UI */
58 private boolean passiveAuth = false;
60 /** a catch-all map for other properties */
61 private Map<String, Object> propsMap = new FastMap<String, Object>();
63 /** The ProfileHandler URL */
64 private String profileHandlerURL;
66 /** The AuthenticationManager's URL */
67 private String authnManagerURL;
69 /** has authentication been attempted yet */
70 private boolean authnAttempted = false;
72 /** The id of the authenticated user */
73 private String userID;
75 /** Did authentication succceed? */
76 private boolean authenticationOK;
78 /** Optional failure message */
79 private String authnFailureMessage;
81 /** The instant of authentication */
82 private DateTime authnInstant;
84 /** The duration of authentication */
85 private long authnDuration;
87 /** The method used to authenticate the user */
88 private String authnMethod;
91 private String sessionID;
94 /** Creates a new instance of LoginContext */
95 public LoginContext() {
100 * Creates a new instance of LoginContext
102 * @param forceAuth if the authentication manager must reauth the user.
103 * @param passiveAuth if the authentication manager must not interact with the users UI.
105 public LoginContext(boolean forceAuth, boolean passiveAuth) {
107 this.forceAuth = forceAuth;
108 this.passiveAuth = passiveAuth;
113 * Returns if authentication must be forced.
115 * @return <code>true</code> if the authentication manager must reauth the user.
117 public boolean getForceAuth() {
118 return this.forceAuth;
123 * Returns if authentication must be passive.
125 * @return <code>true</code> if the authentication manager must not interact with the users UI.
127 public boolean getPassiveAuth() {
128 return this.passiveAuth;
133 * Sets if authentication must be forced.
135 * @param forceAuth if the authentication manager must reauth the user.
137 public void setForceAuth(boolean forceAuth) {
138 this.forceAuth = forceAuth;
143 * Sets if authentication must be passive.
145 * @param passiveAuth if the authentication manager must not interact with the users UI.
147 public void setPassiveAuth(boolean passiveAuth) {
148 this.passiveAuth = passiveAuth;
153 * Get an optional property object.
155 * @param key The key in the properites Map.
157 * @return The object, or <code>null</code> is no object exists for the key.
159 public Object getProperty(String key) {
160 return this.propsMap.get(key);
165 * Sets an optional property object.
167 * If an object is already associated with key, it will be overwritten.
169 * @param key The key to set.
170 * @param obj The object to associate with key.
172 public void setProperty(String key, final Object obj) {
173 this.propsMap.put(key, obj);
178 * Sets if authentication succeeded.
180 * @param authnOK if authentication succeeded;
182 public void setAuthnOK(boolean authnOK) {
183 this.authenticationOK = authnOK;
188 * Returns if authentication succeeded.
190 * @return <code>true</code> is the user was successfully authenticated.
192 public boolean getAuthnOK() {
193 return this.authenticationOK;
197 /** Sets the optional authentication failure message.
199 * @param failureMessage A description of why authN failed.
201 public void setAuthnFailureMessage(String failureMessage) {
202 this.authnFailureMessage = failureMessage;
207 * Returns the optional authentication failure message.
209 * @return The failure message, or <code>null</code> is none was set.
211 public String getAuthnFailureMessage() {
212 return this.authnFailureMessage;
217 * Set if authentication has been attempted.
219 * This method should be called by an {@link AuthenticationHandler}
220 * while processing a request.
222 public void setAuthenticationAttempted() {
223 this.authnAttempted = true;
228 * Returns if authentication has been attempted for this user.
230 public boolean getAuthenticationAttempted() {
231 return this.authnAttempted;
236 * Sets the ID of the authenticated user.
238 * @param userID The userid.
240 public void setUserID(String userID) {
241 this.userID = userID;
246 * Returns the ID of the authenticated user.
248 * @return the ID of the user, or <code>null</code> if authentication failed.
250 public String getUserID() {
256 * Gets the ProfileHandler URL.
258 * @return the URL of the profile handler that is invoking the Authentication Manager.
260 public String getProfileHandlerURL() {
261 return this.profileHandlerURL;
266 * Sets the ProfileHandler URL.
268 * @param profileHandlerURL The URL of the profile handler that invoked the AuthenticationManager/
270 public void setProfileHandlerURL(String profileHandlerURL) {
271 this.profileHandlerURL = profileHandlerURL;
276 * Gets the AuthenticationManager URL.
278 * @return the URL of the AuthenticationManager.
280 public String getAuthnManagerURL() {
281 return this.authnManagerURL;
286 * Sets the AuthenticationManager's URL.
288 * @param authnManagerURL the URL of the AuthenticationManager.
290 public void setAuthnManagerURL(String authnManagerURL) {
291 this.authnManagerURL = authnManagerURL;
296 * Gets the authentication instant.
298 * @return The instant of authentication, or <code>null</code> if none was set.
300 public DateTime getAuthenticationInstant() {
301 return this.authnInstant;
306 * Sets the authentication instant.
308 * @param authnInstant The instant of authentication.
310 public void setAuthenticationInstant(final DateTime authnInstant) {
311 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 this.authnDuration;
326 * Sets the duration of authentication.
328 * @param authnDuration The duration of authentication.
330 public void setAuthenticationDuration(long authnDuration) {
331 this.authnDuration = authnDuration;
336 * Gets the method used to authenticate the user.
338 * @return The method used to authenticate the user.
340 public String getAuthenticationMethod() {
341 return this.authnMethod;
346 * Sets the method used to authenticate the user.
348 * @param authnMethod The method used to authenticate the user.
350 public void setAuthenticationMethod(String authnMethod) {
351 this.authnMethod = authnMethod;
356 * Gets the {@link Session} ID
358 * @return the Session id.
360 public String getSessionID() {
361 return this.sessionID;
366 * Sets the {@link Session} ID
368 * @param sessionID the Session ID
370 public void setSessionID(String sessionID) {
371 this.sessionID = sessionID;