View Javadoc

1   /*
2    * Copyright 2008 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.attributeDefinition;
18  
19  import java.util.Collection;
20  
21  import org.opensaml.Configuration;
22  import org.opensaml.common.SAMLObjectBuilder;
23  import org.opensaml.saml1.core.NameIdentifier;
24  
25  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
26  import edu.internet2.middleware.shibboleth.common.attribute.provider.BasicAttribute;
27  import edu.internet2.middleware.shibboleth.common.attribute.resolver.AttributeResolutionException;
28  import edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.ShibbolethResolutionContext;
29  
30  /**
31   * An attribute definition the creates attributes whose values are {@link NameIdentifier}.
32   * 
33   * When building the NameIdentifier the textual content of the NameIdentifier is the value of the source attribute. If a
34   * {@link #nameIdQualifier} is provided that value is used as the NameIdentifier's name qualifier otherwise the
35   * attribute issuer's entity ID is used. The attribute requester's entity ID is always used as the NameIdentifier's SP
36   * name qualifier.
37   */
38  public class SAML1NameIdentifierAttributeDefinition extends BaseAttributeDefinition {
39  
40      /** Builder of NameIdentifier XMLObjects. */
41      private final SAMLObjectBuilder<NameIdentifier> nameIdBuilder;
42  
43      /** Format of the NameIdentifier. */
44      private String nameIdFormat;
45  
46      /** Name qualifier for the NameIdentifier. */
47      private String nameIdQualifier;
48  
49      /** Constructor. */
50      public SAML1NameIdentifierAttributeDefinition() {
51          super();
52          nameIdBuilder = (SAMLObjectBuilder<NameIdentifier>) Configuration.getBuilderFactory().getBuilder(
53                  NameIdentifier.DEFAULT_ELEMENT_NAME);
54      }
55  
56      /**
57       * Gets the format for the NameIdentifier used as an attribute value.
58       * 
59       * @return format for the NameIdentifier used as an attribute value
60       */
61      public String getNameIdFormat() {
62          return nameIdFormat;
63      }
64  
65      /**
66       * Sets the format for the NameIdentifier used as an attribute value.
67       * 
68       * @param format format for the NameIdentifier used as an attribute value
69       */
70      public void setNameIdFormat(String format) {
71          nameIdFormat = format;
72      }
73  
74      /**
75       * Gets the NameIdentifier qualifier for the NameIdentifier used as an attribute value.
76       * 
77       * @return NameIdentifier qualifier for the NameIdentifier used as an attribute value
78       */
79      public String getNameIdQualifier() {
80          return nameIdQualifier;
81      }
82  
83      /**
84       * Sets the NameIdentifier qualifier for the NameIdentifier used as an attribute value.
85       * 
86       * @param qualifier NameIdentifier qualifier for the NameIdentifier used as an attribute value
87       */
88      public void setNameIdQualifier(String qualifier) {
89          nameIdQualifier = qualifier;
90      }
91  
92      /** {@inheritDoc} */
93      protected BaseAttribute<?> doResolve(ShibbolethResolutionContext resolutionContext)
94              throws AttributeResolutionException {
95          BasicAttribute<NameIdentifier> attribute = new BasicAttribute<NameIdentifier>();
96          attribute.setId(getId());
97  
98          Collection<?> values = getValuesFromAllDependencies(resolutionContext);
99          if (values != null && !values.isEmpty()) {
100             for (Object value : values) {
101                 attribute.getValues().add(buildNameId(value.toString(), resolutionContext));
102             }
103         }
104 
105         return attribute;
106     }
107 
108     /**
109      * Builds a name ID. The provided value is the textual content of the NameIdentifier. If a {@link #nameIdQualifier}
110      * is not null it is used as the NameIdentifier's name qualifier, otherwise the attribute issuer's entity id is
111      * used.
112      * 
113      * @param nameIdValue value of the NameIdentifier
114      * @param resolutionContext current resolution context
115      * 
116      * @return the constructed NameIdentifier
117      */
118     protected NameIdentifier buildNameId(String nameIdValue, ShibbolethResolutionContext resolutionContext) {
119         NameIdentifier nameId = nameIdBuilder.buildObject();
120         nameId.setNameIdentifier(nameIdValue);
121 
122         if (nameIdFormat != null) {
123             nameId.setFormat(nameIdFormat);
124         }
125 
126         if (nameIdQualifier != null) {
127             nameId.setNameQualifier(nameIdQualifier);
128         } else {
129             nameId.setNameQualifier(resolutionContext.getAttributeRequestContext().getLocalEntityId());
130         }
131 
132         return nameId;
133     }
134 
135     /** {@inheritDoc} */
136     public void validate() throws AttributeResolutionException {
137         // do nothing
138     }
139 }