[java-identity-provider] 08/27: SAML 2 scoped string support.

Scott Cantor cantor.2 at osu.edu
Fri May 3 14:31:58 EDT 2019


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch feature/IDP-1434
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=5833c753969cbd67bcd1dfb4f324362f610acb27

commit 5833c753969cbd67bcd1dfb4f324362f610acb27
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Apr 17 15:09:59 2019 -0400

    SAML 2 scoped string support.
---
 .../impl/SAML1StringAttributeTranscoder.java       |   4 +-
 .../impl/SAML2ScopedStringAttributeTranscoder.java | 138 +++++++
 .../impl/SAML2StringAttributeTranscoder.java       |   4 +-
 .../SAML2ScopedStringAttributeTranscoderTest.java  | 396 +++++++++++++++++++++
 4 files changed, 538 insertions(+), 4 deletions(-)

diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoder.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoder.java
index 3cefbd2..413bc0c 100644
--- a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoder.java
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoder.java
@@ -39,8 +39,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * {@link net.shibboleth.idp.attribute.AttributeTranscoder} that produces a SAML 2 Attribute from an
- * {@link IdPAttribute} that contains <code>String</code> values.
+ * {@link net.shibboleth.idp.attribute.AttributeTranscoder} that supports {@link AttributeDesignator} and
+ * {@link StringAttributeValue} objects.
  */
 public class SAML1StringAttributeTranscoder extends AbstractSAML1AttributeTranscoder<StringAttributeValue> {
 
diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2ScopedStringAttributeTranscoder.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2ScopedStringAttributeTranscoder.java
new file mode 100644
index 0000000..3d8ad90
--- /dev/null
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2ScopedStringAttributeTranscoder.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.saml.attribute.transcoding.impl;
+
+import java.util.Properties;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.xml.namespace.QName;
+
+import net.shibboleth.idp.attribute.AttributeEncodingException;
+import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.IdPAttributeValue;
+import net.shibboleth.idp.attribute.ScopedStringAttributeValue;
+import net.shibboleth.idp.saml.attribute.encoding.SAMLEncoderSupport;
+import net.shibboleth.idp.saml.attribute.transcoding.AbstractSAML2AttributeTranscoder;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+
+import org.opensaml.core.xml.AttributeExtensibleXMLObject;
+import org.opensaml.core.xml.XMLObject;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.opensaml.saml.saml2.core.Attribute;
+import org.opensaml.saml.saml2.core.AttributeValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link net.shibboleth.idp.attribute.AttributeTranscoder} that supports {@link Attribute} and
+ * {@link ScopedStringAttributeValue} objects.
+ */
+public class SAML2ScopedStringAttributeTranscoder extends AbstractSAML2AttributeTranscoder<ScopedStringAttributeValue> {
+
+    /** One of "inline" or "attribute", controlling the style of XML encoding. */
+    @Nonnull @NotEmpty public static final String PROP_SCOPE_TYPE = "scopeType";
+
+    /** Name of XML attribute when scopeType property is "attribute". */
+    @Nonnull @NotEmpty public static final String PROP_SCOPE_ATTR_NAME = "scopeAttributeName";
+
+    /** Scope delimiter when scopeType property is "inline". */
+    @Nonnull @NotEmpty public static final String PROP_SCOPE_DELIMITER = "scopeDelimiter";
+
+    /** Class logger. */
+    @Nonnull private final Logger log = LoggerFactory.getLogger(SAML2ScopedStringAttributeTranscoder.class);
+
+    /** {@inheritDoc} */
+    @Override protected boolean canEncodeValue(@Nonnull final IdPAttribute attribute,
+            @Nonnull final IdPAttributeValue value) {
+        return value instanceof ScopedStringAttributeValue;
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nullable protected XMLObject encodeValue(@Nullable final ProfileRequestContext profileRequestContext,
+            @Nonnull final IdPAttribute attribute, @Nonnull final Properties properties,
+            @Nonnull final ScopedStringAttributeValue value) throws AttributeEncodingException {
+                
+        final Object encodeType = properties.getOrDefault(PROP_ENCODE_TYPE, Boolean.TRUE);
+
+        final String scopeType = properties.getProperty(PROP_SCOPE_TYPE, "inline");
+        
+        if ("attribute".equals(scopeType)) {
+            final String scopeAttributeName = properties.getProperty(PROP_SCOPE_ATTR_NAME, "Scope");
+            return SAMLEncoderSupport.encodeScopedStringValueAttribute(attribute,
+                    AttributeValue.DEFAULT_ELEMENT_NAME, value, scopeAttributeName,
+                    encodeType instanceof Boolean ? (Boolean) encodeType : true);
+        } else if ("inline".equals(scopeType)) {
+            final String scopeDelimiter = properties.getProperty(PROP_SCOPE_DELIMITER, "@");
+            return SAMLEncoderSupport.encodeScopedStringValueInline(
+                    attribute, AttributeValue.DEFAULT_ELEMENT_NAME, value, scopeDelimiter,
+                    encodeType instanceof Boolean ? (Boolean) encodeType : true);
+        } else {
+            throw new AttributeEncodingException("Invalid scopeType property (must be inline or attribute)");
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override @Nullable protected IdPAttributeValue<?> decodeValue(
+            @Nullable final ProfileRequestContext profileRequestContext, @Nonnull final Attribute attribute,
+            @Nonnull final Properties properties, @Nullable final XMLObject value) {
+        
+        if (value == null) {
+            return null;
+        }
+        
+        final String stringValue = getStringValue(value);
+        if (null == stringValue) {
+            return null;
+        }
+
+        final String scopeType = properties.getProperty(PROP_SCOPE_TYPE, "inline");
+        if ("attribute".equals(scopeType)) {
+            
+            if (value instanceof AttributeExtensibleXMLObject) {
+                final String scopeValue = ((AttributeExtensibleXMLObject) value).getUnknownAttributes().get(
+                        new QName(null, properties.getProperty(PROP_SCOPE_ATTR_NAME, "Scope")));
+                if (scopeValue == null) {
+                    log.warn("Scope not found in designated XML attribute");
+                    return null;
+                }
+                
+                return ScopedStringAttributeValue.valueOf(stringValue, scopeValue);
+                
+            } else {
+                log.warn("Object does not support required interface to access the scope via XML attribute");
+                return null;
+            }
+        } else if ("inline".equals(scopeType)) {
+            final String scopeDelimiter = properties.getProperty(PROP_SCOPE_DELIMITER, "@");
+            final int offset = stringValue.indexOf(scopeDelimiter);
+            if (offset < 0) {
+                log.warn("Ignoring value with no scope delimiter ({})", scopeDelimiter);
+                return null;
+            }
+
+            return ScopedStringAttributeValue.valueOf(stringValue.substring(0, offset), stringValue.substring(offset
+                    + scopeDelimiter.length()));
+            
+        } else {
+            log.error("Invalid scopeType property (must be inline or attribute)");
+            return null;
+        }
+    }
+        
+}
\ No newline at end of file
diff --git a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoder.java b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoder.java
index dd6cfbd..0c083f9 100644
--- a/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoder.java
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoder.java
@@ -39,8 +39,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * {@link net.shibboleth.idp.attribute.AttributeTranscoder} that produces a SAML 2 Attribute from an
- * {@link IdPAttribute} that contains <code>String</code> values.
+ * {@link net.shibboleth.idp.attribute.AttributeTranscoder} that supports {@link Attribute} and
+ * {@link StringAttributeValue} objects.
  */
 public class SAML2StringAttributeTranscoder extends AbstractSAML2AttributeTranscoder<StringAttributeValue> {
 
diff --git a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2ScopedStringAttributeTranscoderTest.java b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2ScopedStringAttributeTranscoderTest.java
new file mode 100644
index 0000000..0ceeb29
--- /dev/null
+++ b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2ScopedStringAttributeTranscoderTest.java
@@ -0,0 +1,396 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements.  See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.idp.saml.attribute.transcoding.impl;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import net.shibboleth.idp.attribute.AttributeEncodingException;
+import net.shibboleth.idp.attribute.ByteAttributeValue;
+import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.IdPAttributeValue;
+import net.shibboleth.idp.attribute.IdPRequestedAttribute;
+import net.shibboleth.idp.attribute.ScopedStringAttributeValue;
+import net.shibboleth.idp.attribute.StringAttributeValue;
+import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
+import net.shibboleth.idp.attribute.transcoding.TranscoderSupport;
+import net.shibboleth.idp.attribute.transcoding.impl.AttributeTranscoderRegistryImpl;
+import net.shibboleth.idp.saml.attribute.transcoding.AbstractSAML2AttributeTranscoder;
+import net.shibboleth.idp.saml.attribute.transcoding.AbstractSAMLAttributeTranscoder;
+import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
+
+import org.opensaml.core.OpenSAMLInitBaseTestCase;
+import org.opensaml.core.xml.XMLObject;
+import org.opensaml.core.xml.XMLObjectBuilder;
+import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
+import org.opensaml.core.xml.schema.XSString;
+import org.opensaml.saml.common.SAMLObjectBuilder;
+import org.opensaml.saml.saml2.core.Attribute;
+import org.opensaml.saml.saml2.core.AttributeValue;
+import org.opensaml.saml.saml2.metadata.RequestedAttribute;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/** {@link SAML2ScopedStringAttributeTranscoder} unit test. */
+public class SAML2ScopedStringAttributeTranscoderTest extends OpenSAMLInitBaseTestCase {
+
+    private AttributeTranscoderRegistryImpl registry;
+    
+    private XMLObjectBuilder<XSString> stringBuilder;
+
+    private SAMLObjectBuilder<Attribute> attributeBuilder;
+
+    private SAMLObjectBuilder<RequestedAttribute> reqAttributeBuilder;
+
+    private final static String ATTR_NAME = "foo";
+    private final static String ATTR_NAMEFORMAT = "Namespace";
+    private final static String ATTR_FRIENDLYNAME = "friendly";
+    private final static String STRING_1 = "Value The First";
+    private final static String STRING_2 = "Second string the value is";
+    private final static String SCOPE_1 = "scope1.example.org";
+    private final static String SCOPE_2 = "scope2";
+    private final static String DELIMITER = "#";
+
+    @BeforeClass public void setUp() throws ComponentInitializationException {
+        
+        stringBuilder = XMLObjectProviderRegistrySupport.getBuilderFactory().<XSString>getBuilderOrThrow(XSString.TYPE_NAME);
+        
+        attributeBuilder = (SAMLObjectBuilder<Attribute>)
+                XMLObjectProviderRegistrySupport.getBuilderFactory().<Attribute>getBuilderOrThrow(
+                        Attribute.TYPE_NAME);
+        reqAttributeBuilder = (SAMLObjectBuilder<RequestedAttribute>)
+                XMLObjectProviderRegistrySupport.getBuilderFactory().<RequestedAttribute>getBuilderOrThrow(
+                        RequestedAttribute.TYPE_NAME);
+        
+        registry = new AttributeTranscoderRegistryImpl();
+        registry.setId("test");
+        
+        registry.setNamingRegistry(Collections.singletonMap(
+                Attribute.class, new AbstractSAML2AttributeTranscoder.NamingFunction()));
+        
+        final SAML2ScopedStringAttributeTranscoder transcoder = new SAML2ScopedStringAttributeTranscoder();
+        transcoder.initialize();
+        
+        final Map<String,Collection<Properties>> mappings = new HashMap<>();
+        
+        final Properties ruleset1 = new Properties();
+        ruleset1.put(AttributeTranscoderRegistry.PROP_TRANSCODER, transcoder);
+        ruleset1.put(AbstractSAMLAttributeTranscoder.PROP_ENCODE_TYPE, true);
+        ruleset1.setProperty(AbstractSAMLAttributeTranscoder.PROP_NAME, ATTR_NAME);
+        ruleset1.setProperty(AbstractSAML2AttributeTranscoder.PROP_NAME_FORMAT, ATTR_NAMEFORMAT);
+        ruleset1.setProperty(AbstractSAML2AttributeTranscoder.PROP_FRIENDLY_NAME, ATTR_FRIENDLYNAME);
+        ruleset1.setProperty(SAML2ScopedStringAttributeTranscoder.PROP_SCOPE_DELIMITER, DELIMITER);
+        ruleset1.setProperty(SAML2ScopedStringAttributeTranscoder.PROP_SCOPE_TYPE, "inline");
+        
+        mappings.put(ATTR_NAME, Collections.singletonList(ruleset1));
+        
+        registry.setTranscoderRegistry(mappings);
+        
+        registry.initialize();
+    }
+    
+    @AfterClass public void tearDown() {
+        registry.destroy();
+        registry = null;
+    }
+
+    @Test public void emptyEncode() throws Exception {
+        final IdPAttribute inputAttribute = new IdPAttribute(ATTR_NAME);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, Attribute.class);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final Attribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).encode(
+                null, inputAttribute, Attribute.class, ruleset);
+        
+        Assert.assertNotNull(attr);
+        Assert.assertEquals(attr.getName(), ATTR_NAME);
+        Assert.assertEquals(attr.getNameFormat(), ATTR_NAMEFORMAT);
+        Assert.assertEquals(attr.getFriendlyName(), ATTR_FRIENDLYNAME);
+        Assert.assertTrue(attr.getAttributeValues().isEmpty());
+    }
+
+    @Test public void emptyDecode() throws Exception {
+        
+        final Attribute samlAttribute = attributeBuilder.buildObject();
+        samlAttribute.setName(ATTR_NAME);
+        samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final IdPAttribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).decode(null, samlAttribute, ruleset);
+        
+        Assert.assertNotNull(attr);
+        Assert.assertEquals(attr.getId(), ATTR_NAME);
+        Assert.assertTrue(attr.getValues().isEmpty());
+    }
+
+    @Test public void emptyRequestedDecode() throws Exception {
+        
+        final RequestedAttribute samlAttribute = reqAttributeBuilder.buildObject();
+        samlAttribute.setName(ATTR_NAME);
+        samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+        samlAttribute.setIsRequired(true);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final IdPAttribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).decode(null, samlAttribute, ruleset);
+        
+        Assert.assertTrue(attr instanceof IdPRequestedAttribute);
+        Assert.assertEquals(attr.getId(), ATTR_NAME);
+        Assert.assertTrue(((IdPRequestedAttribute) attr).getIsRequired());
+        Assert.assertTrue(attr.getValues().isEmpty());
+    }
+    
+    @Test(expectedExceptions = {AttributeEncodingException.class,}) public void inappropriate() throws Exception {
+        final int[] intArray = {1, 2, 3, 4};
+        final Collection<? extends IdPAttributeValue<?>> values =
+                Arrays.asList(new ByteAttributeValue(new byte[] {1, 2, 3,}), new IdPAttributeValue<Object>() {
+                    @Override
+                    public Object getValue() {
+                        return intArray;
+                    }
+                    @Override
+                    public String getDisplayValue() {
+                        return intArray.toString();
+                    }
+                });
+
+        final IdPAttribute inputAttribute = new IdPAttribute(ATTR_NAME);
+        inputAttribute.setValues(values);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, Attribute.class);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        TranscoderSupport.<Attribute>getTranscoder(ruleset).encode(null, inputAttribute, Attribute.class, ruleset);
+    }
+    
+    @Test public void single() throws Exception {
+        final Collection<? extends IdPAttributeValue<?>> values =
+                Arrays.asList(new ByteAttributeValue(new byte[] {1, 2, 3,}),
+                        new ScopedStringAttributeValue(STRING_1, SCOPE_1),
+                        new StringAttributeValue(STRING_1),
+                        new StringAttributeValue(STRING_1 + "@" + SCOPE_1));
+
+        final IdPAttribute inputAttribute = new IdPAttribute(ATTR_NAME);
+        inputAttribute.setValues(values);
+        
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, Attribute.class);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final Attribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).encode(
+                null, inputAttribute, Attribute.class, ruleset);
+
+        Assert.assertNotNull(attr);
+        Assert.assertEquals(attr.getName(), ATTR_NAME);
+        Assert.assertEquals(attr.getNameFormat(), ATTR_NAMEFORMAT);
+        Assert.assertEquals(attr.getFriendlyName(), ATTR_FRIENDLYNAME);
+
+        final List<XMLObject> children = attr.getOrderedChildren();
+
+        Assert.assertEquals(children.size(), 1, "Encoding one entry");
+
+        final XMLObject child = children.get(0);
+
+        Assert.assertEquals(child.getElementQName(), AttributeValue.DEFAULT_ELEMENT_NAME,
+                "Attribute Value not inside <AttributeValue/>");
+
+        Assert.assertTrue(child instanceof XSString, "Child of result attribute should be a string");
+
+        final XSString childAsString = (XSString) child;
+
+        Assert.assertEquals(childAsString.getValue(), STRING_1 + DELIMITER + SCOPE_1);
+    }
+
+    @Test public void singleRequested() throws Exception {
+        final Collection<? extends IdPAttributeValue<?>> values =
+                Arrays.asList(new ByteAttributeValue(new byte[] {1, 2, 3,}),
+                        new ScopedStringAttributeValue(STRING_1, SCOPE_1));
+
+        final IdPRequestedAttribute inputAttribute = new IdPRequestedAttribute(ATTR_NAME);
+        inputAttribute.setRequired(true);
+        inputAttribute.setValues(values);
+        
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, Attribute.class);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+
+        final RequestedAttribute attr = TranscoderSupport.<RequestedAttribute>getTranscoder(ruleset).encode(
+                null, inputAttribute, RequestedAttribute.class, ruleset);
+
+        Assert.assertNotNull(attr);
+        Assert.assertEquals(attr.getName(), ATTR_NAME);
+        Assert.assertEquals(attr.getNameFormat(), ATTR_NAMEFORMAT);
+        Assert.assertEquals(attr.getFriendlyName(), ATTR_FRIENDLYNAME);
+        Assert.assertTrue(attr.isRequired());
+
+        final List<XMLObject> children = attr.getOrderedChildren();
+
+        Assert.assertEquals(children.size(), 1, "Encoding one entry");
+
+        final XMLObject child = children.get(0);
+
+        Assert.assertEquals(child.getElementQName(), AttributeValue.DEFAULT_ELEMENT_NAME,
+                "Attribute Value not inside <AttributeValue/>");
+
+        Assert.assertTrue(child instanceof XSString, "Child of result attribute should be a string");
+
+        final XSString childAsString = (XSString) child;
+
+        Assert.assertEquals(childAsString.getValue(), STRING_1 + DELIMITER + SCOPE_1);
+    }
+    
+    @Test public void singleDecode() throws Exception {
+                
+        final XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
+        stringValue.setValue(STRING_1 + DELIMITER + SCOPE_1);
+        
+        final Attribute samlAttribute = attributeBuilder.buildObject();
+        samlAttribute.setName(ATTR_NAME);
+        samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+        samlAttribute.getAttributeValues().add(stringValue);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final IdPAttribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).decode(null, samlAttribute, ruleset);
+        
+        Assert.assertNotNull(attr);
+        Assert.assertEquals(attr.getId(), ATTR_NAME);
+        Assert.assertEquals(attr.getValues().size(), 1);
+        
+        final ScopedStringAttributeValue value = (ScopedStringAttributeValue) attr.getValues().get(0);
+        Assert.assertEquals(value.getValue(), STRING_1);
+        Assert.assertEquals(value.getScope(), SCOPE_1);
+    }
+    
+    
+    @Test public void singleRequestedDecode() throws Exception {
+        
+        final XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
+        stringValue.setValue(STRING_1 + DELIMITER + SCOPE_1);
+        
+        final RequestedAttribute samlAttribute = reqAttributeBuilder.buildObject();
+        samlAttribute.setName(ATTR_NAME);
+        samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+        samlAttribute.setIsRequired(true);
+        samlAttribute.getAttributeValues().add(stringValue);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final IdPAttribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).decode(null, samlAttribute, ruleset);
+        
+        Assert.assertTrue(attr instanceof IdPRequestedAttribute);
+        Assert.assertEquals(attr.getId(), ATTR_NAME);
+        Assert.assertTrue(((IdPRequestedAttribute) attr).getIsRequired());
+        Assert.assertEquals(attr.getValues().size(), 1);
+        
+        final ScopedStringAttributeValue value = (ScopedStringAttributeValue) attr.getValues().get(0);
+        Assert.assertEquals(value.getValue(), STRING_1);
+        Assert.assertEquals(value.getScope(), SCOPE_1);
+    }
+    
+    @Test public void multi() throws Exception {
+        final Collection<? extends IdPAttributeValue<?>> values =
+                Arrays.asList(new ByteAttributeValue(new byte[] {1, 2, 3,}),
+                        new ScopedStringAttributeValue(STRING_1, SCOPE_1),
+                        new ScopedStringAttributeValue(STRING_2, SCOPE_2));
+
+        final IdPAttribute inputAttribute = new IdPAttribute(ATTR_NAME);
+        inputAttribute.setValues(values);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, Attribute.class);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final Attribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).encode(
+                null, inputAttribute, Attribute.class, ruleset);
+
+        Assert.assertNotNull(attr);
+
+        final List<XMLObject> children = attr.getOrderedChildren();
+        Assert.assertEquals(children.size(), 2, "Encoding 2 entries");
+
+        final String s1 = STRING_1 + DELIMITER + SCOPE_1;
+        final String s2 = STRING_2 + DELIMITER + SCOPE_2;
+        
+        for (final XMLObject child: children) {
+            Assert.assertTrue(child instanceof XSString, "Child of result attribute should be a string");
+            final String childAsString = ((XSString) children.get(0)).getValue();
+            Assert.assertTrue(s1.equals(childAsString) || s2.equals(childAsString));
+        }
+    }
+
+    @Test public void multiDecode() throws Exception {
+        
+        final XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
+        stringValue.setValue(STRING_1 + DELIMITER + SCOPE_1);
+
+        final XSString stringValue2 = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
+        stringValue2.setValue(STRING_2 + DELIMITER + SCOPE_2);
+
+        final XSString stringValue3 = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
+        stringValue3.setValue(STRING_2 + "@" + SCOPE_2);
+
+        final XSString stringValue4 = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
+        stringValue4.setValue(STRING_2);
+
+        final Attribute samlAttribute = attributeBuilder.buildObject();
+        samlAttribute.setName(ATTR_NAME);
+        samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+        samlAttribute.getAttributeValues().add(stringValue);
+        samlAttribute.getAttributeValues().add(stringValue2);
+        samlAttribute.getAttributeValues().add(stringValue3);
+        samlAttribute.getAttributeValues().add(stringValue4);
+
+        final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
+        Assert.assertEquals(rulesets.size(), 1);
+        final Properties ruleset = rulesets.iterator().next();
+        
+        final IdPAttribute attr = TranscoderSupport.<Attribute>getTranscoder(ruleset).decode(null, samlAttribute, ruleset);
+        
+        Assert.assertNotNull(attr);
+        Assert.assertEquals(attr.getId(), ATTR_NAME);
+        Assert.assertEquals(attr.getValues().size(), 2);
+        
+        final ScopedStringAttributeValue value1 = (ScopedStringAttributeValue) attr.getValues().get(0);
+        final ScopedStringAttributeValue value2 = (ScopedStringAttributeValue) attr.getValues().get(1);
+        Assert.assertTrue(STRING_1.equals(value1.getValue()) || STRING_1.equals(value2.getValue()));
+        Assert.assertTrue(STRING_2.equals(value1.getValue()) || STRING_2.equals(value2.getValue()));
+        Assert.assertTrue(SCOPE_1.equals(value1.getScope()) || SCOPE_1.equals(value2.getScope()));
+        Assert.assertTrue(SCOPE_2.equals(value1.getScope()) || SCOPE_2.equals(value2.getScope()));
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list