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