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;
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
23 import java.util.concurrent.ConcurrentHashMap;
25 import org.joda.time.DateTime;
28 * Login context created by a profile handler and interpreted by the authentication package.
30 * Two properties are tracked by default:
32 * <code>forceAuth</code> - Should user authentication be forced. <code>passiveAuth</code> - Should user
33 * authentication not control the UI.
35 * A Map<String, Object> is provided to store other properties. Alternatively, a profile handler may create a
36 * subclass of LoginContext with extra fields.
38 * LoginContexts should be created by a profile handler when authentication is needed. Once control has returned to the
39 * profile handler, it should remove the LoginContext from the HttpSession.
41 * The {@link AuthenticationEngine} or an {@link LoginHandler} should set the
42 * {@link LoginContext#setAuthenticationAttempted()}, {@link LoginContext#setPrincipalAuthenticated(boolean)},
43 * {@link LoginContext#setAuthnFailure(String)}, {@link LoginContext#{setAuthenticationDuration(long)}
44 * {@link LoginContext#setAuthenticationInstant(DateTime)} appropriately.
47 public class LoginContext implements Serializable {
49 /** the key in a HttpSession where login contexts are stored. */
50 public static final String LOGIN_CONTEXT_KEY = "shib2.logincontext";
52 /** Serial version UID. */
53 private static final long serialVersionUID = -8764003758734956911L;
55 /** Entity ID of the relying party. */
56 private String relyingPartyId;
58 /** Should user authentication be forced. */
59 private boolean forceAuth;
61 /** Must authentication not interact with the UI. */
62 private boolean passiveAuth;
64 /** a catch-all map for other properties. */
65 private Map<String, Serializable> propsMap = new ConcurrentHashMap<String, Serializable>();
67 /** The ProfileHandler URL. */
68 private String profileHandlerURL;
70 /** The authentication engine's URL. */
71 private String authnEngineURL;
73 /** has authentication been attempted yet. */
74 private boolean authnAttempted;
76 /** The id of the authenticated user. */
77 private String principalName;
79 /** Did authentication succeed? */
80 private boolean principalAuthenticated;
82 /** Optional failure message. */
83 private String authnFailureMessage;
85 /** The instant of authentication. */
86 private DateTime authnInstant;
88 /** The duration of authentication. */
89 private long authnDuration;
91 /** The method used to authenticate the user. */
92 private String authnMethod;
94 /** The session id. */
95 private String sessionID;
97 /** List of request authentication methods. */
98 private ArrayList<String> requestAuthenticationMethods;
100 /** Creates a new instance of LoginContext. */
101 public LoginContext() {
102 requestAuthenticationMethods = new ArrayList<String>();
106 * Creates a new instance of LoginContext.
108 * @param force if the authentication manager must re-authenticate the user.
109 * @param passive if the authentication manager must not interact with the users UI.
111 public LoginContext(boolean force, boolean passive) {
113 passiveAuth = passive;
114 requestAuthenticationMethods = new ArrayList<String>();
118 * Gets the entity ID of the relying party.
120 * @return entity ID of the relying party
122 public String getRelyingPartyId() {
123 return relyingPartyId;
127 * Gets the entity ID of the relying party.
129 * @param id entity ID of the relying party
131 public void setRelyingParty(String id) {
136 * Returns if authentication must be forced.
138 * @return <code>true</code> if the authentication manager must re-authenticate the user.
140 public boolean getForceAuth() {
145 * Returns if authentication must be passive.
147 * @return <code>true</code> if the authentication manager must not interact with the users UI.
149 public boolean getPassiveAuth() {
154 * Sets if authentication must be forced.
156 * @param force if the authentication manager must re-authenticate the user.
158 public void setForceAuth(boolean force) {
163 * Sets if authentication must be passive.
165 * @param passive if the authentication manager must not interact with the users UI.
167 public void setPassiveAuth(boolean passive) {
168 passiveAuth = passive;
172 * Get an optional property object.
174 * @param key The key in the properties Map.
176 * @return The object, or <code>null</code> is no object exists for the key.
178 public Object getProperty(String key) {
179 return propsMap.get(key);
183 * Sets an optional property object.
185 * If an object is already associated with key, it will be overwritten.
187 * @param key The key to set.
188 * @param obj The object to associate with key.
190 public void setProperty(String key, final Serializable obj) {
191 propsMap.put(key, obj);
195 * Sets if authentication succeeded.
197 * @param authnOK if authentication succeeded;
199 public void setPrincipalAuthenticated(boolean authnOK) {
200 this.principalAuthenticated = authnOK;
204 * Returns if authentication succeeded.
206 * @return <code>true</code> is the user was successfully authenticated.
208 public boolean isPrincipalAuthenticated() {
209 return principalAuthenticated;
213 * Sets the optional authentication failure message.
215 * @param failureMessage A description of why authN failed.
217 public void setAuthenticationFailureMessage(String failureMessage) {
218 authnFailureMessage = failureMessage;
222 * Returns the optional authentication failure message.
224 * @return The failure message, or <code>null</code> is none was set.
226 public String getAuthenticationFailureMessage() {
227 return authnFailureMessage;
231 * Set if authentication has been attempted.
233 * This method should be called by an {@link LoginHandler} while processing a request.
235 public void setAuthenticationAttempted() {
236 authnAttempted = true;
240 * Returns if authentication has been attempted for this user.
242 * @return if authentication has been attempted for this user
244 public boolean getAuthenticationAttempted() {
245 return authnAttempted;
249 * Sets the ID of the authenticated user.
251 * @param id The userid.
253 public void setPrincipalName(String id) {
258 * Returns the ID of the authenticated user.
260 * @return the ID of the user, or <code>null</code> if authentication failed.
262 public String getPrincipalName() {
263 return principalName;
267 * Gets the ProfileHandler URL.
269 * @return the URL of the profile handler that is invoking the Authentication Manager.
271 public String getProfileHandlerURL() {
272 return profileHandlerURL;
276 * Sets the ProfileHandler URL.
278 * @param url The URL of the profile handler that invoked the AuthenticationManager/
280 public void setProfileHandlerURL(String url) {
281 profileHandlerURL = url;
285 * Gets the authentication engine's URL.
287 * @return the URL of the authentication engine
289 public String getAuthenticationEngineURL() {
290 return authnEngineURL;
294 * Sets the authentication engine's URL.
296 * @param url the URL of the authentication engine
298 public void setAuthenticationEngineURL(String url) {
299 authnEngineURL = url;
303 * Gets the authentication instant.
305 * @return The instant of authentication, or <code>null</code> if none was set.
307 public DateTime getAuthenticationInstant() {
312 * Sets the authentication instant.
314 * @param instant The instant of authentication.
316 public void setAuthenticationInstant(final DateTime instant) {
317 authnInstant = instant;
321 * Gets the duration of authentication.
323 * @return The duration of authentication, or zero if none was set.
325 public long getAuthenticationDuration() {
326 return authnDuration;
330 * Sets the duration of authentication.
332 * @param duration The duration of authentication.
334 public void setAuthenticationDuration(long duration) {
335 authnDuration = duration;
339 * Gets the method used to authenticate the user.
341 * @return The method used to authenticate the user.
343 public String getAuthenticationMethod() {
348 * Sets the method used to authenticate the user.
350 * @param method The method used to authenticate the user.
352 public void setAuthenticationMethod(String method) {
353 authnMethod = method;
357 * Gets the {@link Session} ID.
359 * @return the Session id
361 public String getSessionID() {
366 * Sets the {@link Session} ID.
368 * @param id the Session ID
370 public void setSessionID(String id) {
375 * Return the acceptable authentication handler URIs, in preference order, for authenticating this user. If no
376 * authentication methods are preferred the resultant list will be empty.
378 * @return an list of authentication method identifiers
380 public List<String> getRequestedAuthenticationMethods() {
381 return requestAuthenticationMethods;