View Javadoc

1   /*
2    * Copyright [2007] [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.common.attribute.resolver.provider.principalConnector;
18  
19  import java.sql.SQLException;
20  
21  import org.opensaml.saml1.core.NameIdentifier;
22  import org.opensaml.saml2.core.NameID;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
27  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
28  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.StoredIDDataConnector;
29  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.StoredIDStore;
30  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector.StoredIDStore.PersistentIdEntry;
31  import edu.internet2.middleware.shibboleth.common.profile.provider.SAMLProfileRequestContext;
32  
33  /**
34   * A principal connector that resolved ID created by {@link StoredIDPrincipalConnector}s into principals.
35   */
36  public class StoredIDPrincipalConnector extends BasePrincipalConnector {
37  
38      /** Class logger. */
39      private final Logger log = LoggerFactory.getLogger(StoredIDPrincipalConnector.class);
40  
41      /** ID store that manages the stored IDs. */
42      private StoredIDStore pidStore;
43  
44      /**
45       * Constructor.
46       * 
47       * @param idProducer data connector that produced the stored ID.
48       */
49      public StoredIDPrincipalConnector(StoredIDDataConnector idProducer) {
50          if (idProducer == null) {
51              throw new IllegalArgumentException("ID producing data connector may not be null");
52          }
53          pidStore = idProducer.getStoredIDStore();
54  
55      }
56  
57      /** {@inheritDoc} */
58      public String resolve(ShibbolethResolutionContext resolutionContext) throws AttributeResolutionException {
59          SAMLProfileRequestContext requestContext = resolutionContext.getAttributeRequestContext();
60  
61          String persistentId;
62          if (requestContext.getSubjectNameIdentifier() instanceof NameIdentifier) {
63              persistentId = ((NameIdentifier) requestContext.getSubjectNameIdentifier()).getNameIdentifier();
64          } else if (requestContext.getSubjectNameIdentifier() instanceof NameID) {
65              persistentId = ((NameID) requestContext.getSubjectNameIdentifier()).getValue();
66          } else {
67              throw new AttributeResolutionException("Subject name identifier is not of a supported type");
68          }
69  
70          try {
71              PersistentIdEntry pidEntry = pidStore.getActivePersistentIdEntry(persistentId);
72              if(pidEntry != null){
73                  return pidEntry.getPrincipalName();
74              }else{
75                  return null;
76              }
77          } catch (SQLException e) {
78              log.error("Error retrieving persistent ID from database", e);
79              throw new AttributeResolutionException("Error retrieving persistent ID from database", e);
80          }
81      }
82  
83      /** {@inheritDoc} */
84      public void validate() throws AttributeResolutionException {
85          if(pidStore == null){
86              throw new AttributeResolutionException("Persistent ID store was null");
87          }
88          
89          try{
90              pidStore.getPersistentIdEntry("test", false);
91          }catch(SQLException e){
92              throw new AttributeResolutionException("Persistent ID store can not perform persistent ID search", e);
93          }
94      }
95  }