View Javadoc

    1   /*
    2    * Licensed to the University Corporation for Advanced Internet Development,
    3    * Inc. (UCAID) under one or more contributor license agreements.  See the
    4    * NOTICE file distributed with this work for additional information regarding
    5    * copyright ownership. The UCAID licenses this file to You under the Apache
    6    * License, Version 2.0 (the "License"); you may not use this file except in
    7    * compliance with the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package edu.internet2.middleware.shibboleth.idp.session.impl;
    19
    20  import java.security.Principal;
    21
    22  import javax.security.auth.Subject;
    23
    24  import org.joda.time.DateTime;
    25  import org.joda.time.chrono.ISOChronology;
    26
    27  import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
    28
    29  /** Information about an authentication method employed by a user. */
    30  public class AuthenticationMethodInformationImpl implements AuthenticationMethodInformation {
    31
    32      /** Serial version UID. */
    33      private static final long serialVersionUID = -2108905664641155003L;
    34
    35      /** Subject created by this authentication mechanism. */
    36      private Subject authenticationSubject;
    37
    38      /** Principal created by the authentication method. */
    39      private Principal authenticationPrincipal;
    40
    41      /** The authentication method (a URI). */
    42      private String authenticationMethod;
    43
    44      /** The timestamp at which authentication occurred. */
    45      private long authenticationInstant;
    46
    47      /** The lifetime of the authentication method. */
    48      private long authenticationDuration;
    49
    50      /** Time when this method expires. */
    51      private long expirationInstant;
    52
    53      /**
    54       * Default constructor.  This constructor does NOT add the given principal to the given subject.
    55       *
    56       * @param subject subject associated with the user's session
    57       * @param principal principal created by the authentication method
    58       * @param method The unique identifier for the authentication method
    59       * @param instant The time the user authenticated with this member
    60       * @param duration The duration of this authentication method
    61       */
    62      public AuthenticationMethodInformationImpl(Subject subject, Principal principal, String method, DateTime instant,
    63              long duration) {
    64
    65          if (method == null || instant == null || duration < 0) {
    66              throw new IllegalArgumentException("Authentication method, instant, and duration may not be null");
    67          }
    68
    69          authenticationSubject = subject;
    70          authenticationPrincipal = principal;
    71          authenticationMethod = method;
    72          authenticationInstant = instant.toDateTime(ISOChronology.getInstanceUTC()).getMillis();
    73          authenticationDuration = duration;
    74          expirationInstant = authenticationInstant + duration;
    75      }
    76
    77      /** {@inheritDoc} */
    78      public synchronized Subject getAuthenticationSubject() {
    79          return authenticationSubject;
    80      }
    81
    82      /** {@inheritDoc} */
    83      public synchronized Principal getAuthenticationPrincipal() {
    84          return authenticationPrincipal;
    85      }
    86
    87      /** {@inheritDoc} */
    88      public synchronized String getAuthenticationMethod() {
    89          return authenticationMethod;
    90      }
    91
    92      /** {@inheritDoc} */
    93      public synchronized DateTime getAuthenticationInstant() {
    94          return new DateTime(authenticationInstant, ISOChronology.getInstanceUTC());
    95      }
    96
    97      /** {@inheritDoc} */
    98      public synchronized long getAuthenticationDuration() {
    99          return authenticationDuration;
    100     }
    101
    102     /** {@inheritDoc} */
    103     public synchronized boolean isExpired() {
    104         return new DateTime(expirationInstant, ISOChronology.getInstanceUTC()).isBeforeNow();
    105     }
    106
    107     /** {@inheritDoc} */
    108     public synchronized int hashCode() {
    109         return authenticationMethod.hashCode();
    110     }
    111
    112     /** {@inheritDoc} */
    113     public synchronized boolean equals(Object obj) {
    114         if (obj == this) {
    115             return true;
    116         }
    117
    118         if (!(obj instanceof AuthenticationMethodInformation)) {
    119             return false;
    120         }
    121
    122         AuthenticationMethodInformation amInfo = (AuthenticationMethodInformation) obj;
    123         return authenticationMethod.equals(amInfo.getAuthenticationMethod());
    124     }
    125 }