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  import java.util.Map;
    21  import java.util.Set;
    22  import java.util.concurrent.ConcurrentHashMap;
    23
    24  import javax.security.auth.Subject;
    25
    26  import edu.internet2.middleware.shibboleth.common.session.impl.AbstractSession;
    27  import edu.internet2.middleware.shibboleth.idp.authn.UsernamePrincipal;
    28  import edu.internet2.middleware.shibboleth.idp.session.AuthenticationMethodInformation;
    29  import edu.internet2.middleware.shibboleth.idp.session.ServiceInformation;
    30  import edu.internet2.middleware.shibboleth.idp.session.Session;
    31
    32  /** Session information for user logged into the IdP. */
    33  public class SessionImpl extends AbstractSession implements Session {
    34
    35      /** Serial version UID. */
    36      private static final long serialVersionUID = 2927868242208211623L;
    37
    38      /** Secret key associated with the session. */
    39      private byte[] sessionSecret;
    40
    41      /** The list of methods used to authenticate the user. */
    42      private Map<String, AuthenticationMethodInformation> authnMethods;
    43
    44      /** The list of services to which the user has logged in. */
    45      private Map<String, ServiceInformation> servicesInformation;
    46
    47      /**
    48       * Constructor.
    49       *
    50       * @param sessionId ID of the session
    51       * @param secret a secret to associate with the session
    52       * @param timeout inactivity timeout for the session in milliseconds
    53       */
    54      public SessionImpl(String sessionId, byte[] secret, long timeout) {
    55          super(sessionId, timeout);
    56
    57          sessionSecret = secret;
    58          authnMethods = new ConcurrentHashMap<String, AuthenticationMethodInformation>(2);
    59          servicesInformation = new ConcurrentHashMap<String, ServiceInformation>(2);
    60      }
    61
    62      /** {@inheritDoc} */
    63      public synchronized byte[] getSessionSecret() {
    64          return sessionSecret;
    65      }
    66
    67      /** {@inheritDoc} */
    68      public synchronized Map<String, AuthenticationMethodInformation> getAuthenticationMethods() {
    69          return authnMethods;
    70      }
    71
    72      /** {@inheritDoc} */
    73      public synchronized Map<String, ServiceInformation> getServicesInformation() {
    74          return servicesInformation;
    75      }
    76
    77      /**
    78       * Gets the service information for the given entity ID.
    79       *
    80       * @param entityId entity ID to retrieve the service information for
    81       *
    82       * @return the service information or null
    83       */
    84      public synchronized ServiceInformation getServiceInformation(String entityId) {
    85          return servicesInformation.get(entityId);
    86      }
    87
    88      /**
    89       * This method will return the first, in an unordered list of principal names registered with the {@link Subject} of
    90       * the session. If one or more {@link UsernamePrincipal} principals is registered with the subject the returned
    91       * value will be the string form of one of those.
    92       *
    93       * {@inheritDoc}
    94       */
    95      public synchronized String getPrincipalName() {
    96          Subject subject = getSubject();
    97
    98          Set<? extends Principal> principals = subject.getPrincipals(UsernamePrincipal.class);
    99          if (principals == null || principals.isEmpty()) {
    100             principals = subject.getPrincipals();
    101         }
    102
    103         if (principals == null || principals.isEmpty()) {
    104             return null;
    105         }
    106
    107         return principals.iterator().next().getName();
    108     }
    109 }