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