1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.saml2.core.Attribute;
24 import org.opensaml.saml2.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.schema.impl.XSStringBuilder;
29 import org.opensaml.xml.util.Base64;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import edu.internet2.middleware.shibboleth.common.attribute.BaseAttribute;
34
35
36
37
38
39 public class SAML2Base64AttributeEncoder extends AbstractSAML2AttributeEncoder {
40
41
42 private final Logger log = LoggerFactory.getLogger(SAML2Base64AttributeEncoder.class);
43
44
45 private final XMLObjectBuilder<XSString> stringBuilder;
46
47
48 public SAML2Base64AttributeEncoder() {
49 super();
50 stringBuilder = (XSStringBuilder) Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME);
51 }
52
53
54 public Attribute encode(BaseAttribute attribute) {
55 Attribute samlAttribute = attributeBuilder.buildObject();
56 populateAttribute(samlAttribute);
57
58 byte[] attributeValue;
59 XSString samlAttributeValue;
60 for (Object o : attribute.getValues()) {
61 if (o == null || !(o instanceof byte[])) {
62 log.debug("Skipping attribute value because it is either null or not a byte[]");
63 continue;
64 }
65
66 attributeValue = (byte[]) o;
67 samlAttributeValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
68 samlAttributeValue.setValue(Base64.encodeBytes(attributeValue));
69 samlAttribute.getAttributeValues().add(samlAttributeValue);
70 }
71
72 List<XMLObject> attributeValues = samlAttribute.getAttributeValues();
73 if (attributeValues == null || attributeValues.isEmpty()) {
74 log.debug("Unable to encode {} attribute. It does not contain any values", attribute.getId());
75 return null;
76 }
77
78 return samlAttribute;
79 }
80
81 }