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      /** SP name qualifier for the NameID. */
49      private String nameIdSPQualifier;
50      
51      /** Constructor. */
52      public SAML2NameIDAttributeDefinition() {
53          super();
54          nameIdBuilder = (SAMLObjectBuilder<NameID>) Configuration.getBuilderFactory().getBuilder(
55                  NameID.DEFAULT_ELEMENT_NAME);
56      }
57  
58      /**
59       * Gets the format for the NameID used as an attribute value.
60       * 
61       * @return format for the NameID used as an attribute value
62       */
63      public String getNameIdFormat() {
64          return nameIdFormat;
65      }
66  
67      /**
68       * Sets the format for the NameID used as an attribute value.
69       * 
70       * @param format format for the NameID used as an attribute value
71       */
72      public void setNameIdFormat(String format) {
73          nameIdFormat = format;
74      }
75  
76      /**
77       * Gets the NameQualifier for the NameID used as an attribute value.
78       * 
79       * @return NameQualifier for the NameID used as an attribute value
80       */
81      public String getNameIdQualifier() {
82          return nameIdQualifier;
83      }
84  
85      /**
86       * Sets the NameQualifier for the NameID used as an attribute value.
87       * 
88       * @param qualifier NameQualifier for the NameID used as an attribute value
89       */
90      public void setNameIdQualifier(String qualifier) {
91          nameIdQualifier = qualifier;
92      }
93  
94      /**
95       * Gets the SPNameQualifier for the NameID used as an attribute value.
96       * 
97       * @return SPNameQualifier for the NameID used as an attribute value
98       */
99      public String getNameIdSPQualifier() {
100         return nameIdSPQualifier;
101     }
102 
103     /**
104      * Sets the SPNameQualifier for the NameID used as an attribute value.
105      * 
106      * @param qualifier SPNameQualifier for the NameID used as an attribute value
107      */
108     public void setNameIdSPQualifier(String qualifier) {
109         nameIdSPQualifier = qualifier;
110     }
111     
112     /** {@inheritDoc} */
113     protected BaseAttribute<?> doResolve(ShibbolethResolutionContext resolutionContext)
114             throws AttributeResolutionException {
115         BasicAttribute<NameID> attribute = new BasicAttribute<NameID>();
116         attribute.setId(getId());
117 
118         Collection<?> values = getValuesFromAllDependencies(resolutionContext);
119         if (values != null && !values.isEmpty()) {
120             for (Object value : values) {
121                 attribute.getValues().add(buildNameId(value.toString(), resolutionContext));
122             }
123         }
124 
125         return attribute;
126     }
127 
128     /**
129      * Builds a name ID. The provided value is the textual content of the NameID. The
130      * NameQualifier and SPNameQualifier are set according to the configuration, or
131      * to the local and requesting entityIDs respectively. 
132      * 
133      * @param nameIdValue value of the NameID
134      * @param resolutionContext current resolution context
135      * 
136      * @return the constructed NameID
137      */
138     protected NameID buildNameId(String nameIdValue, ShibbolethResolutionContext resolutionContext) {
139         NameID nameId = nameIdBuilder.buildObject();
140         nameId.setValue(nameIdValue);
141 
142         if (nameIdFormat != null) {
143             nameId.setFormat(nameIdFormat);
144         }
145 
146         if (nameIdQualifier != null) {
147             nameId.setNameQualifier(nameIdQualifier);
148         } else {
149             nameId.setNameQualifier(resolutionContext.getAttributeRequestContext().getLocalEntityId());
150         }
151         
152         if (nameIdSPQualifier != null) {
153             nameId.setSPNameQualifier(nameIdSPQualifier);
154         } else {
155             nameId.setSPNameQualifier(resolutionContext.getAttributeRequestContext().getInboundMessageIssuer());
156         }
157 
158         return nameId;
159     }
160 
161     /** {@inheritDoc} */
162     public void validate() throws AttributeResolutionException {
163         // do nothing
164     }
165 }