View Javadoc

1   /*
2    * Copyright 2006 University Corporation for Advanced Internet Development, Inc.
3    *
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
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  
17  package edu.internet2.middleware.shibboleth.idp.session.impl;
18  
19  import java.security.Principal;
20  
21  import javax.security.auth.Subject;
22  
23  import org.joda.time.DateTime;
24  import org.joda.time.chrono.ISOChronology;
25  
26  import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
27  
28  /** Information about an authentication method employed by a user. */
29  public class AuthenticationMethodInformationImpl implements AuthenticationMethodInformation {
30  
31      /** Serial version UID. */
32      private static final long serialVersionUID = -2108905664641155003L;
33  
34      /** Subject created by this authentication mechanism. */
35      private Subject authenticationSubject;
36  
37      /** Principal created by the authentication method. */
38      private Principal authenticationPrincipal;
39  
40      /** The authentication method (a URI). */
41      private String authenticationMethod;
42  
43      /** The timestamp at which authentication occurred. */
44      private long authenticationInstant;
45  
46      /** The lifetime of the authentication method. */
47      private long authenticationDuration;
48  
49      /** Time when this method expires. */
50      private long expirationInstant;
51  
52      /**
53       * Default constructor.  This constructor does NOT add the given principal to the given subject.
54       * 
55       * @param subject subject associated with the user's session
56       * @param principal principal created by the authentication method
57       * @param method The unique identifier for the authentication method
58       * @param instant The time the user authenticated with this member
59       * @param duration The duration of this authentication method
60       */
61      public AuthenticationMethodInformationImpl(Subject subject, Principal principal, String method, DateTime instant,
62              long duration) {
63  
64          if (method == null || instant == null || duration < 0) {
65              throw new IllegalArgumentException("Authentication method, instant, and duration may not be null");
66          }
67  
68          authenticationSubject = subject;
69          authenticationPrincipal = principal;
70          authenticationMethod = method;
71          authenticationInstant = instant.toDateTime(ISOChronology.getInstanceUTC()).getMillis();
72          authenticationDuration = duration;
73          expirationInstant = authenticationInstant + duration;
74      }
75  
76      /** {@inheritDoc} */
77      public synchronized Subject getAuthenticationSubject() {
78          return authenticationSubject;
79      }
80  
81      /** {@inheritDoc} */
82      public synchronized Principal getAuthenticationPrincipal() {
83          return authenticationPrincipal;
84      }
85  
86      /** {@inheritDoc} */
87      public synchronized String getAuthenticationMethod() {
88          return authenticationMethod;
89      }
90  
91      /** {@inheritDoc} */
92      public synchronized DateTime getAuthenticationInstant() {
93          return new DateTime(authenticationInstant, ISOChronology.getInstanceUTC());
94      }
95  
96      /** {@inheritDoc} */
97      public synchronized long getAuthenticationDuration() {
98          return authenticationDuration;
99      }
100 
101     /** {@inheritDoc} */
102     public synchronized boolean isExpired() {
103         return new DateTime(expirationInstant, ISOChronology.getInstanceUTC()).isBeforeNow();
104     }
105 
106     /** {@inheritDoc} */
107     public synchronized int hashCode() {
108         return authenticationMethod.hashCode();
109     }
110 
111     /** {@inheritDoc} */
112     public synchronized boolean equals(Object obj) {
113         if (obj == this) {
114             return true;
115         }
116 
117         if (!(obj instanceof AuthenticationMethodInformation)) {
118             return false;
119         }
120 
121         AuthenticationMethodInformation amInfo = (AuthenticationMethodInformation) obj;
122         return authenticationMethod.equals(amInfo.getAuthenticationMethod());
123     }
124 }