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 authentiation 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 AuthenticationHandler} 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 AuthenticationManager's URL. */
71 private String authnManagerURL;
73 /** has authentication been attempted yet. */
74 private boolean authnAttempted;
76 /** The id of the authenticated user. */
77 private String principalName;
79 /** Did authentication succceed? */
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 /** Creates a new instance of LoginContext. */
98 public LoginContext() {
102 * Creates a new instance of LoginContext.
104 * @param force if the authentication manager must reauth the user.
105 * @param passive if the authentication manager must not interact with the users UI.
107 public LoginContext(boolean force, boolean passive) {
110 passiveAuth = passive;
114 * Gets the entity ID of the relying party.
116 * @return entity ID of the relying party
118 public String getRelyingPartyId() {
119 return relyingPartyId;
123 * Gets the entity ID of the relying party.
125 * @param id entity ID of the relying party
127 public void setRelyingParty(String id) {
132 * Returns if authentication must be forced.
134 * @return <code>true</code> if the authentication manager must reauth the user.
136 public boolean getForceAuth() {
141 * Returns if authentication must be passive.
143 * @return <code>true</code> if the authentication manager must not interact with the users UI.
145 public boolean getPassiveAuth() {
150 * Sets if authentication must be forced.
152 * @param force if the authentication manager must reauth the user.
154 public void setForceAuth(boolean force) {
159 * Sets if authentication must be passive.
161 * @param passicve if the authentication manager must not interact with the users UI.
163 public void setPassiveAuth(boolean passicve) {
164 passiveAuth = passicve;
168 * Get an optional property object.
170 * @param key The key in the properites Map.
172 * @return The object, or <code>null</code> is no object exists for the key.
174 public Object getProperty(String key) {
175 return propsMap.get(key);
179 * Sets an optional property object.
181 * If an object is already associated with key, it will be overwritten.
183 * @param key The key to set.
184 * @param obj The object to associate with key.
186 public void setProperty(String key, final Serializable obj) {
187 propsMap.put(key, obj);
191 * Sets if authentication succeeded.
193 * @param authnOK if authentication succeeded;
195 public void setPrincipalAuthenticated(boolean authnOK) {
196 this.principalAuthenticated = authnOK;
200 * Returns if authentication succeeded.
202 * @return <code>true</code> is the user was successfully authenticated.
204 public boolean isPrincipalAuthenticated() {
205 return principalAuthenticated;
209 * Sets the optional authentication failure message.
211 * @param failureMessage A description of why authN failed.
213 public void setAuthenticationFailureMessage(String failureMessage) {
214 authnFailureMessage = failureMessage;
218 * Returns the optional authentication failure message.
220 * @return The failure message, or <code>null</code> is none was set.
222 public String getAuthenticationFailureMessage() {
223 return authnFailureMessage;
227 * Set if authentication has been attempted.
229 * This method should be called by an {@link AuthenticationHandler} while processing a request.
231 public void setAuthenticationAttempted() {
232 authnAttempted = true;
236 * Returns if authentication has been attempted for this user.
238 * @return if authentication has been attempted for this user
240 public boolean getAuthenticationAttempted() {
241 return authnAttempted;
245 * Sets the ID of the authenticated user.
247 * @param id The userid.
249 public void setPrincipalName(String id) {
254 * Returns the ID of the authenticated user.
256 * @return the ID of the user, or <code>null</code> if authentication failed.
258 public String getPrincipalName() {
259 return principalName;
263 * Gets the ProfileHandler URL.
265 * @return the URL of the profile handler that is invoking the Authentication Manager.
267 public String getProfileHandlerURL() {
268 return profileHandlerURL;
272 * Sets the ProfileHandler URL.
274 * @param url The URL of the profile handler that invoked the AuthenticationManager/
276 public void setProfileHandlerURL(String url) {
277 profileHandlerURL = url;
281 * Gets the AuthenticationManager URL.
283 * @return the URL of the AuthenticationManager.
285 public String getAuthenticationManagerURL() {
286 return authnManagerURL;
290 * Sets the AuthenticationManager's URL.
292 * @param url the URL of the AuthenticationManager.
294 public void setAuthenticationManagerURL(String url) {
295 authnManagerURL = url;
299 * Gets the authentication instant.
301 * @return The instant of authentication, or <code>null</code> if none was set.
303 public DateTime getAuthenticationInstant() {
308 * Sets the authentication instant.
310 * @param instant The instant of authentication.
312 public void setAuthenticationInstant(final DateTime instant) {
313 authnInstant = instant;
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;
326 * Sets the duration of authentication.
328 * @param duration The duration of authentication.
330 public void setAuthenticationDuration(long duration) {
331 authnDuration = duration;
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.
346 * @param method The method used to authenticate the user.
348 public void setAuthenticationMethod(String method) {
349 authnMethod = method;
353 * Gets the {@link Session} ID.
355 * @return the Session id
357 public String getSessionID() {
362 * Sets the {@link Session} ID.
364 * @param id the Session ID
366 public void setSessionID(String id) {
371 * Return the acceptable authentication handler URIs, in preference order, for authenticating this user. If no
372 * authentication methods are preferred the resultant list will be empty.
374 * @return an list of authentication method identifiers
376 public List<String> getRequestedAuthenticationMethods() {
377 return new ArrayList<String>();