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