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.encoding.provider;
19  
20  import java.util.List;
21  
22  import org.opensaml.Configuration;
23  import org.opensaml.saml1.core.Attribute;
24  import org.opensaml.saml1.core.AttributeValue;
25  import org.opensaml.xml.XMLObject;
26  import org.opensaml.xml.XMLObjectBuilder;
27  import org.opensaml.xml.schema.XSString;
28  import org.opensaml.xml.util.Base64;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
33  
34  /**
35   * Implementation of SAML 1.X attribute encoder.
36   * 
37   * This attribute encoder only operates of {@link BaseAttribute}s with values of type <code>byte[]</code>.
38   */
39  public class SAML1Base64AttributeEncoder extends AbstractSAML1AttributeEncoder {
40  
41      /** Class logger. */
42      private final Logger log = LoggerFactory.getLogger(SAML1Base64AttributeEncoder.class);
43  
44      /** XSString factory. */
45      private final XMLObjectBuilder<XSString> stringBuilder;
46  
47      /** Constructor. */
48      public SAML1Base64AttributeEncoder() {
49          super();
50  
51          stringBuilder = Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME);
52      }
53  
54      /** {@inheritDoc} */
55      public Attribute encode(BaseAttribute attribute) {
56          Attribute samlAttribute = attributeBuilder.buildObject();
57          populateAttribute(samlAttribute);
58  
59          byte[] attributeValue;
60          XSString samlAttributeValue;
61          for (Object o : attribute.getValues()) {
62              if (o == null || !(o instanceof byte[])) {
63                  log.debug("Skipping attribute value because it is either null or not a byte[]");
64                  continue;
65              }
66              
67              attributeValue = (byte[]) o;
68              samlAttributeValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
69              samlAttributeValue.setValue(Base64.encodeBytes(attributeValue));
70              samlAttribute.getAttributeValues().add(samlAttributeValue);
71          }
72  
73          List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
74          if (attributeValues == null || attributeValues.isEmpty()) {
75              log.debug("Unable to encode {} attribute.  It does not contain any values", attribute.getId());
76              return null;
77          }
78  
79          return samlAttribute;
80      }
81  
82  }