[java-identity-provider] 07/27: Add SAML 1 code and change "no values" handling.
Scott Cantor
cantor.2 at osu.edu
Fri May 3 14:31:57 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=a1ed8b975663dee3659bdb9e2f1e89f3b133d7f1
commit a1ed8b975663dee3659bdb9e2f1e89f3b133d7f1
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Fri Apr 12 13:25:36 2019 -0400
Add SAML 1 code and change "no values" handling.
---
.../transcoding/AttributeTranscoderRegistry.java | 6 -
....java => AbstractSAML1AttributeTranscoder.java} | 113 ++++++++---------
.../AbstractSAML2AttributeTranscoder.java | 12 +-
.../AbstractSAMLAttributeTranscoder.java | 25 +---
.../impl/SAML1StringAttributeTranscoder.java | 81 +++++++++++++
...ava => SAML1StringAttributeTranscoderTest.java} | 134 ++++++++-------------
...ava => SAML2StringAttributeTranscoderTest.java} | 4 +-
7 files changed, 203 insertions(+), 172 deletions(-)
diff --git a/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java b/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java
index d378b06..07dce7b 100644
--- a/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java
+++ b/idp-attribute-api/src/main/java/net/shibboleth/idp/attribute/transcoding/AttributeTranscoderRegistry.java
@@ -44,12 +44,6 @@ public interface AttributeTranscoderRegistry extends IdentifiedComponent {
/** Property name for accessing {@link AttributeTranscoder} object to use. */
@Nonnull @NotEmpty static final String PROP_TRANSCODER = "transcoder";
- /** Property name for whether a result with no values should be allowed, default false . */
- @Nonnull @NotEmpty static final String PROP_ENCODE_NO_VALUES = "encodeNoValues";
-
- /** Property name for whether a result with no values should be allowed, default true. */
- @Nonnull @NotEmpty static final String PROP_DECODE_NO_VALUES = "decodeNoValues";
-
/**
* Obtains a set of instructions for encoding an input {@link IdPAttribute} into a target type.
*
diff --git a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML1AttributeTranscoder.java
similarity index 59%
copy from idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java
copy to idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML1AttributeTranscoder.java
index 44e444d..3a0af31 100644
--- a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java
+++ b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML1AttributeTranscoder.java
@@ -31,6 +31,7 @@ import net.shibboleth.idp.attribute.IdPAttribute;
import net.shibboleth.idp.attribute.IdPAttributeValue;
import net.shibboleth.idp.attribute.IdPRequestedAttribute;
import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
+import net.shibboleth.idp.saml.xml.SAMLConstants;
import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
@@ -38,52 +39,54 @@ import org.opensaml.core.xml.XMLObject;
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
import org.opensaml.profile.context.ProfileRequestContext;
import org.opensaml.saml.common.SAMLObjectBuilder;
-import org.opensaml.saml.saml2.core.Attribute;
-import org.opensaml.saml.saml2.metadata.RequestedAttribute;
+import org.opensaml.saml.saml1.core.Attribute;
+import org.opensaml.saml.saml1.core.AttributeDesignator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.google.common.base.Strings;
/**
- * Base class for transcoders that operate on a SAML 2 {@link Attribute}.
+ * Base class for transcoders that operate on a SAML 1 {@link Attribute} or {@link AttributeDesignator}.
*
* @param <EncodedType> the type of data that can be handled by the transcoder
*/
-public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAttributeValue> extends
- AbstractSAMLAttributeTranscoder<Attribute,EncodedType> {
+public abstract class AbstractSAML1AttributeTranscoder<EncodedType extends IdPAttributeValue> extends
+ AbstractSAMLAttributeTranscoder<AttributeDesignator,EncodedType> {
- /** A friendly, human readable, name for the attribute. */
- @Nonnull @NotEmpty public static final String PROP_FRIENDLY_NAME = "friendlyName";
-
- /** The format of the attribute name. */
- @Nonnull @NotEmpty public static final String PROP_NAME_FORMAT = "nameFormat";
+ /** The namespace of the attribute name. */
+ @Nonnull @NotEmpty public static final String PROP_NAMESPACE = "namespace";
+ /** Class logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(AbstractSAML1AttributeTranscoder.class);
+
/** Builder used to construct {@link Attribute} objects. */
@Nonnull private final SAMLObjectBuilder<Attribute> attributeBuilder;
- /** Builder used to construct {@link RequestedAttribute} objects. */
- @Nonnull private final SAMLObjectBuilder<RequestedAttribute> reqAttributeBuilder;
+ /** Builder used to construct {@link AttributeDesignator} objects. */
+ @Nonnull private final SAMLObjectBuilder<AttributeDesignator> designatorBuilder;
/** Constructor. */
- public AbstractSAML2AttributeTranscoder() {
+ public AbstractSAML1AttributeTranscoder() {
attributeBuilder = (SAMLObjectBuilder<Attribute>)
XMLObjectProviderRegistrySupport.getBuilderFactory().<Attribute>getBuilderOrThrow(
Attribute.TYPE_NAME);
- reqAttributeBuilder = (SAMLObjectBuilder<RequestedAttribute>)
- XMLObjectProviderRegistrySupport.getBuilderFactory().<RequestedAttribute>getBuilderOrThrow(
- RequestedAttribute.TYPE_NAME);
+ designatorBuilder = (SAMLObjectBuilder<AttributeDesignator>)
+ XMLObjectProviderRegistrySupport.getBuilderFactory().<AttributeDesignator>getBuilderOrThrow(
+ AttributeDesignator.TYPE_NAME);
}
/** {@inheritDoc} */
- @Nonnull public Class<Attribute> getEncodedType() {
- return Attribute.class;
+ @Nonnull public Class<AttributeDesignator> getEncodedType() {
+ return AttributeDesignator.class;
}
/** {@inheritDoc} */
@Nullable public String getEncodedName(@Nonnull final Properties properties) {
try {
- // SAML 2 naming should be based on only what needs to be available from the properties alone.
- return new NamingFunction().apply(buildAttribute(null, null, Attribute.class, properties,
+ // SAML 1 naming should be based on only what needs to be available from the properties alone.
+ return new NamingFunction().apply(buildAttribute(null, null, AttributeDesignator.class, properties,
Collections.emptyList()));
} catch (final AttributeEncodingException e) {
return null;
@@ -92,38 +95,39 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
/** {@inheritDoc} */
@Override
- @Nonnull protected Attribute buildAttribute(@Nullable final ProfileRequestContext profileRequestContext,
- @Nullable final IdPAttribute attribute, @Nonnull final Class<? extends Attribute> to,
+ @Nonnull protected AttributeDesignator buildAttribute(@Nullable final ProfileRequestContext profileRequestContext,
+ @Nullable final IdPAttribute attribute, @Nonnull final Class<? extends AttributeDesignator> to,
@Nonnull final Properties properties, @Nonnull @NonnullElements final List<XMLObject> attributeValues)
throws AttributeEncodingException {
+ if (attribute != null && !attribute.getValues().isEmpty() && attributeValues.isEmpty()) {
+ throw new AttributeEncodingException("Failed to encode any values for attribute " + attribute.getId());
+ }
+
final String name = properties.getProperty(PROP_NAME);
if (Strings.isNullOrEmpty(name)) {
throw new AttributeEncodingException("Required transcoder property 'name' not found");
}
- final Attribute samlAttribute;
+ final AttributeDesignator samlAttribute;
if (to.equals(Attribute.class)) {
- samlAttribute = attributeBuilder.buildObject();
- } else if (to.equals(RequestedAttribute.class)) {
- samlAttribute = reqAttributeBuilder.buildObject();
- if (attribute instanceof IdPRequestedAttribute) {
- ((RequestedAttribute) samlAttribute).setIsRequired(((IdPRequestedAttribute) attribute).getIsRequired());
+ if (attributeValues.isEmpty()) {
+ throw new AttributeEncodingException("Unable to encode a SAML 1 Attribute with no values");
}
+
+ samlAttribute = attributeBuilder.buildObject();
+ ((Attribute) samlAttribute).getAttributeValues().addAll(attributeValues);
+ } else if (to.equals(AttributeDesignator.class)) {
+ samlAttribute = designatorBuilder.buildObject();
+ log.warn("Lossy conversion to AttributeDesignator");
} else {
throw new AttributeEncodingException("Unsupported target object type: " + to.getName());
}
- samlAttribute.setName(name);
- samlAttribute.setNameFormat(properties.getProperty(PROP_NAME_FORMAT, Attribute.URI_REFERENCE));
- samlAttribute.getAttributeValues().addAll(attributeValues);
-
- final String friendlyName = properties.getProperty(PROP_FRIENDLY_NAME,
- attribute != null ? attribute.getId() : "");
- if (!friendlyName.isBlank()) {
- samlAttribute.setFriendlyName(friendlyName);
- }
+ samlAttribute.setAttributeName(name);
+ samlAttribute.setAttributeNamespace(
+ properties.getProperty(PROP_NAMESPACE, SAMLConstants.SAML1_ATTR_NAMESPACE_URI));
return samlAttribute;
}
@@ -131,7 +135,7 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
/** {@inheritDoc} */
@Override
@Nonnull protected IdPAttribute buildIdPAttribute(
- @Nullable final ProfileRequestContext profileRequestContext, @Nonnull final Attribute attribute,
+ @Nullable final ProfileRequestContext profileRequestContext, @Nonnull final AttributeDesignator attribute,
@Nonnull final Properties properties,
@Nonnull @NonnullElements final List<IdPAttributeValue<?>> attributeValues)
throws AttributeDecodingException {
@@ -142,11 +146,14 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
}
final IdPAttribute idpAttribute;
- if (attribute instanceof RequestedAttribute) {
- idpAttribute = new IdPRequestedAttribute(id);
- ((IdPRequestedAttribute) idpAttribute).setRequired(((RequestedAttribute) attribute).isRequired());
- } else {
+ if (attribute instanceof Attribute) {
+ if (!((Attribute) attribute).getAttributeValues().isEmpty() && attributeValues.isEmpty()) {
+ throw new AttributeDecodingException("Failed to decode any values for attribute "
+ + attribute.getAttributeName());
+ }
idpAttribute = new IdPAttribute(id);
+ } else {
+ idpAttribute = new IdPRequestedAttribute(id);
}
idpAttribute.setValues(attributeValues);
@@ -156,29 +163,23 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
/** {@inheritDoc} */
@Override
- @Nonnull protected Iterable<XMLObject> getValues(@Nonnull final Attribute input) {
- return input.getAttributeValues();
+ @Nonnull protected Iterable<XMLObject> getValues(@Nonnull final AttributeDesignator input) {
+ return input instanceof Attribute ? ((Attribute) input).getAttributeValues() : Collections.emptyList();
}
- /**
- * A function to produce a "canonical" name for a SAML 2.0 {@link Attribute} for transcoding rules.
- */
- public static class NamingFunction implements Function<Attribute,String> {
+ /** A function to produce a "canonical" name for a SAML 1 {@link AttributeDesignator} for transcoding rules. */
+ public static class NamingFunction implements Function<AttributeDesignator,String> {
/** {@inheritDoc} */
- @Nullable public String apply(@Nullable final Attribute input) {
+ @Nullable public String apply(@Nullable final AttributeDesignator input) {
- if (input == null || input.getName() == null) {
+ if (input == null || input.getAttributeName() == null || input.getAttributeNamespace() == null) {
return null;
}
- String format = input.getNameFormat();
- if (format == null) {
- format = Attribute.UNSPECIFIED;
- }
-
final StringBuilder builder = new StringBuilder();
- builder.append("SAML2:{").append(format).append('}').append(input.getName());
+ builder.append("SAML1:{").append(input.getAttributeNamespace()).append('}')
+ .append(input.getAttributeName());
return builder.toString();
}
diff --git a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java
index 44e444d..49482f4 100644
--- a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java
+++ b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAML2AttributeTranscoder.java
@@ -44,7 +44,7 @@ import org.opensaml.saml.saml2.metadata.RequestedAttribute;
import com.google.common.base.Strings;
/**
- * Base class for transcoders that operate on a SAML 2 {@link Attribute}.
+ * Base class for transcoders that operate on a SAML 2 {@link Attribute} or {@link RequestedAttribute}.
*
* @param <EncodedType> the type of data that can be handled by the transcoder
*/
@@ -56,7 +56,7 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
/** The format of the attribute name. */
@Nonnull @NotEmpty public static final String PROP_NAME_FORMAT = "nameFormat";
-
+
/** Builder used to construct {@link Attribute} objects. */
@Nonnull private final SAMLObjectBuilder<Attribute> attributeBuilder;
@@ -97,6 +97,10 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
@Nonnull final Properties properties, @Nonnull @NonnullElements final List<XMLObject> attributeValues)
throws AttributeEncodingException {
+ if (attribute != null && !attribute.getValues().isEmpty() && attributeValues.isEmpty()) {
+ throw new AttributeEncodingException("Failed to encode any values for attribute " + attribute.getId());
+ }
+
final String name = properties.getProperty(PROP_NAME);
if (Strings.isNullOrEmpty(name)) {
throw new AttributeEncodingException("Required transcoder property 'name' not found");
@@ -136,6 +140,10 @@ public abstract class AbstractSAML2AttributeTranscoder<EncodedType extends IdPAt
@Nonnull @NonnullElements final List<IdPAttributeValue<?>> attributeValues)
throws AttributeDecodingException {
+ if (!attribute.getAttributeValues().isEmpty() && attributeValues.isEmpty()) {
+ throw new AttributeDecodingException("Failed to decode any values for attribute " + attribute.getName());
+ }
+
final String id = properties.getProperty(AttributeTranscoderRegistry.PROP_ID);
if (Strings.isNullOrEmpty(id)) {
throw new AttributeDecodingException("Required transcoder property 'id' not found");
diff --git a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAMLAttributeTranscoder.java b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAMLAttributeTranscoder.java
index c9ce061..7722794 100644
--- a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAMLAttributeTranscoder.java
+++ b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/AbstractSAMLAttributeTranscoder.java
@@ -30,7 +30,6 @@ import net.shibboleth.idp.attribute.AttributeEncodingException;
import net.shibboleth.idp.attribute.IdPAttribute;
import net.shibboleth.idp.attribute.IdPAttributeValue;
import net.shibboleth.idp.attribute.transcoding.AbstractAttributeTranscoder;
-import net.shibboleth.idp.attribute.transcoding.AttributeTranscoderRegistry;
import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
import net.shibboleth.utilities.java.support.component.ComponentSupport;
@@ -109,17 +108,7 @@ public abstract class AbstractSAMLAttributeTranscoder<AttributeType extends SAML
}
}
- if (samlAttributeValues.isEmpty()) {
- Object allowNoValues = properties.get(AttributeTranscoderRegistry.PROP_ENCODE_NO_VALUES);
- if (!(allowNoValues instanceof Boolean)) {
- allowNoValues = false;
- }
- if (! (Boolean) allowNoValues) {
- throw new AttributeEncodingException("No values encoded for attribute " + attributeId);
- }
- }
-
- log.debug("Completed encoding {} values for attribute {}", samlAttributeValues.size(), attributeId);
+ log.debug("Encoded {} values for attribute {}", samlAttributeValues.size(), attributeId);
return buildAttribute(profileRequestContext, attribute, to, properties, samlAttributeValues);
}
@@ -157,17 +146,7 @@ public abstract class AbstractSAMLAttributeTranscoder<AttributeType extends SAML
}
}
- if (idpAttributeValues.isEmpty()) {
- Object allowNoValues = properties.get(AttributeTranscoderRegistry.PROP_DECODE_NO_VALUES);
- if (!(allowNoValues instanceof Boolean)) {
- allowNoValues = true;
- }
- if (! (Boolean) allowNoValues) {
- throw new AttributeDecodingException("No values decoded for attribute " + attributeName);
- }
- }
-
- log.debug("Completed decoding {} values for attribute {}", idpAttributeValues.size(), attributeName);
+ log.debug("Decoded {} values for attribute {}", idpAttributeValues.size(), attributeName);
return buildIdPAttribute(profileRequestContext, input, properties, idpAttributeValues);
}
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
new file mode 100644
index 0000000..3cefbd2
--- /dev/null
+++ b/idp-saml-impl/src/main/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoder.java
@@ -0,0 +1,81 @@
+/*
+ * 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 net.shibboleth.idp.attribute.AttributeEncodingException;
+import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.IdPAttributeValue;
+import net.shibboleth.idp.attribute.LocalizedStringAttributeValue;
+import net.shibboleth.idp.attribute.ScopedStringAttributeValue;
+import net.shibboleth.idp.attribute.StringAttributeValue;
+import net.shibboleth.idp.saml.attribute.encoding.SAMLEncoderSupport;
+import net.shibboleth.idp.saml.attribute.transcoding.AbstractSAML1AttributeTranscoder;
+
+import org.opensaml.core.xml.XMLObject;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.opensaml.saml.saml1.core.AttributeDesignator;
+import org.opensaml.saml.saml1.core.AttributeValue;
+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.
+ */
+public class SAML1StringAttributeTranscoder extends AbstractSAML1AttributeTranscoder<StringAttributeValue> {
+
+ /** Class logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(SAML1StringAttributeTranscoder.class);
+
+ /** {@inheritDoc} */
+ @Override protected boolean canEncodeValue(@Nonnull final IdPAttribute attribute,
+ @Nonnull final IdPAttributeValue value) {
+ return value instanceof StringAttributeValue;
+ }
+
+ /** {@inheritDoc} */
+ @Override @Nullable protected XMLObject encodeValue(@Nullable final ProfileRequestContext profileRequestContext,
+ @Nonnull final IdPAttribute attribute, @Nonnull final Properties properties,
+ @Nonnull final StringAttributeValue value) throws AttributeEncodingException {
+
+ if (value instanceof LocalizedStringAttributeValue || value instanceof ScopedStringAttributeValue) {
+ log.warn("Attribute '{}': Lossy encoding of attribute value of type {} to SAML1 String Attribute",
+ attribute.getId(), value.getClass().getSimpleName());
+ }
+
+ final Object encodeType = properties.getOrDefault(PROP_ENCODE_TYPE, Boolean.TRUE);
+
+ return SAMLEncoderSupport.encodeStringValue(attribute,
+ AttributeValue.DEFAULT_ELEMENT_NAME, value.getValue(),
+ encodeType instanceof Boolean ? (Boolean) encodeType : true);
+ }
+
+ /** {@inheritDoc} */
+ @Override @Nullable protected IdPAttributeValue<?> decodeValue(
+ @Nullable final ProfileRequestContext profileRequestContext, @Nonnull final AttributeDesignator attribute,
+ @Nonnull final Properties properties, @Nullable final XMLObject value) {
+
+ return value != null ? StringAttributeValue.valueOf(getStringValue(value)) : null;
+ }
+
+}
\ No newline at end of file
diff --git a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeEncoderTest.java b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoderTest.java
similarity index 75%
copy from idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeEncoderTest.java
copy to idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoderTest.java
index 8a6eb2a..d51cb90 100644
--- a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeEncoderTest.java
+++ b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML1StringAttributeTranscoderTest.java
@@ -35,7 +35,7 @@ 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.AbstractSAML1AttributeTranscoder;
import net.shibboleth.idp.saml.attribute.transcoding.AbstractSAMLAttributeTranscoder;
import net.shibboleth.utilities.java.support.component.ComponentInitializationException;
@@ -45,16 +45,16 @@ 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.opensaml.saml.saml1.core.Attribute;
+import org.opensaml.saml.saml1.core.AttributeValue;
+import org.opensaml.saml.saml1.core.AttributeDesignator;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
-/** {@link SAML2StringAttributeTranscoder} unit test. */
-public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
+/** {@link SAML1StringAttributeTranscoder} unit test. */
+public class SAML1StringAttributeTranscoderTest extends OpenSAMLInitBaseTestCase {
private AttributeTranscoderRegistryImpl registry;
@@ -62,11 +62,10 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
private SAMLObjectBuilder<Attribute> attributeBuilder;
- private SAMLObjectBuilder<RequestedAttribute> reqAttributeBuilder;
+ private SAMLObjectBuilder<AttributeDesignator> designatorBuilder;
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 ATTR_NAMESPACE = "Namespace";
private final static String STRING_1 = "Value The First";
private final static String STRING_2 = "Second string the value is";
@@ -77,18 +76,18 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
attributeBuilder = (SAMLObjectBuilder<Attribute>)
XMLObjectProviderRegistrySupport.getBuilderFactory().<Attribute>getBuilderOrThrow(
Attribute.TYPE_NAME);
- reqAttributeBuilder = (SAMLObjectBuilder<RequestedAttribute>)
- XMLObjectProviderRegistrySupport.getBuilderFactory().<RequestedAttribute>getBuilderOrThrow(
- RequestedAttribute.TYPE_NAME);
+ designatorBuilder = (SAMLObjectBuilder<AttributeDesignator>)
+ XMLObjectProviderRegistrySupport.getBuilderFactory().<AttributeDesignator>getBuilderOrThrow(
+ AttributeDesignator.TYPE_NAME);
registry = new AttributeTranscoderRegistryImpl();
registry.setId("test");
+
+ final SAML1StringAttributeTranscoder transcoder = new SAML1StringAttributeTranscoder();
+ transcoder.initialize();
registry.setNamingRegistry(Collections.singletonMap(
- Attribute.class, new AbstractSAML2AttributeTranscoder.NamingFunction()));
-
- final SAML2StringAttributeTranscoder transcoder = new SAML2StringAttributeTranscoder();
- transcoder.initialize();
+ transcoder.getEncodedType(), new AbstractSAML1AttributeTranscoder.NamingFunction()));
final Map<String,Collection<Properties>> mappings = new HashMap<>();
@@ -96,8 +95,7 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
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(AbstractSAML1AttributeTranscoder.PROP_NAMESPACE, ATTR_NAMESPACE);
mappings.put(ATTR_NAME, Collections.singletonList(ruleset1));
@@ -122,17 +120,32 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
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.assertEquals(attr.getAttributeName(), ATTR_NAME);
+ Assert.assertEquals(attr.getAttributeNamespace(), ATTR_NAMESPACE);
Assert.assertTrue(attr.getAttributeValues().isEmpty());
}
+ @Test public void emptyRequestedEncode() throws Exception {
+ final IdPAttribute inputAttribute = new IdPAttribute(ATTR_NAME);
+
+ final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, AttributeDesignator.class);
+ Assert.assertEquals(rulesets.size(), 1);
+ final Properties ruleset = rulesets.iterator().next();
+
+ final AttributeDesignator attr = TranscoderSupport.<AttributeDesignator>getTranscoder(ruleset).encode(
+ null, inputAttribute, AttributeDesignator.class, ruleset);
+
+ Assert.assertNotNull(attr);
+ Assert.assertEquals(attr.getAttributeName(), ATTR_NAME);
+ Assert.assertEquals(attr.getAttributeNamespace(), ATTR_NAMESPACE);
+ }
+
@Test public void emptyDecode() throws Exception {
+ // This isn't technically legal in SAML, but it should functionally work in this direction.
final Attribute samlAttribute = attributeBuilder.buildObject();
- samlAttribute.setName(ATTR_NAME);
- samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+ samlAttribute.setAttributeName(ATTR_NAME);
+ samlAttribute.setAttributeNamespace(ATTR_NAMESPACE);
final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
Assert.assertEquals(rulesets.size(), 1);
@@ -147,20 +160,19 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
@Test public void emptyRequestedDecode() throws Exception {
- final RequestedAttribute samlAttribute = reqAttributeBuilder.buildObject();
- samlAttribute.setName(ATTR_NAME);
- samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
- samlAttribute.setIsRequired(true);
+ final AttributeDesignator samlAttribute = designatorBuilder.buildObject();
+ samlAttribute.setAttributeName(ATTR_NAME);
+ samlAttribute.setAttributeNamespace(ATTR_NAMESPACE);
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);
+ final IdPAttribute attr = TranscoderSupport.<AttributeDesignator>getTranscoder(ruleset).decode(null, samlAttribute, ruleset);
Assert.assertTrue(attr instanceof IdPRequestedAttribute);
Assert.assertEquals(attr.getId(), ATTR_NAME);
- Assert.assertTrue(((IdPRequestedAttribute) attr).getIsRequired());
+ Assert.assertFalse(((IdPRequestedAttribute) attr).getIsRequired());
Assert.assertTrue(attr.getValues().isEmpty());
}
@@ -203,9 +215,8 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
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.assertEquals(attr.getAttributeName(), ATTR_NAME);
+ Assert.assertEquals(attr.getAttributeNamespace(), ATTR_NAMESPACE);
final List<XMLObject> children = attr.getOrderedChildren();
@@ -228,36 +239,18 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
Arrays.asList(new ByteAttributeValue(new byte[] {1, 2, 3,}), new StringAttributeValue(STRING_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);
+ final AttributeDesignator attr = TranscoderSupport.<AttributeDesignator>getTranscoder(ruleset).encode(
+ null, inputAttribute, AttributeDesignator.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);
+ Assert.assertEquals(attr.getAttributeName(), ATTR_NAME);
+ Assert.assertEquals(attr.getAttributeNamespace(), ATTR_NAMESPACE);
}
@Test public void singleDecode() throws Exception {
@@ -266,8 +259,8 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
stringValue.setValue(STRING_1);
final Attribute samlAttribute = attributeBuilder.buildObject();
- samlAttribute.setName(ATTR_NAME);
- samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+ samlAttribute.setAttributeName(ATTR_NAME);
+ samlAttribute.setAttributeNamespace(ATTR_NAMESPACE);
samlAttribute.getAttributeValues().add(stringValue);
final Collection<Properties> rulesets = registry.getTranscodingProperties(samlAttribute);
@@ -281,32 +274,7 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
Assert.assertEquals(attr.getValues().size(), 1);
Assert.assertEquals(attr.getValues().get(0).getValue().toString(), STRING_1);
}
-
-
- @Test public void singleRequestedDecode() throws Exception {
-
- final XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
- stringValue.setValue(STRING_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);
- Assert.assertEquals(attr.getValues().get(0).getValue().toString(), STRING_1);
- }
-
@Test public void multi() throws Exception {
final Collection<? extends IdPAttributeValue<?>> values =
Arrays.asList(new ByteAttributeValue(new byte[] {1, 2, 3,}),
@@ -345,8 +313,8 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
stringValue2.setValue(STRING_2);
final Attribute samlAttribute = attributeBuilder.buildObject();
- samlAttribute.setName(ATTR_NAME);
- samlAttribute.setNameFormat(ATTR_NAMEFORMAT);
+ samlAttribute.setAttributeName(ATTR_NAME);
+ samlAttribute.setAttributeNamespace(ATTR_NAMESPACE);
samlAttribute.getAttributeValues().add(stringValue);
samlAttribute.getAttributeValues().add(stringValue2);
diff --git a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeEncoderTest.java b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoderTest.java
similarity index 98%
rename from idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeEncoderTest.java
rename to idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoderTest.java
index 8a6eb2a..3daa775 100644
--- a/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeEncoderTest.java
+++ b/idp-saml-impl/src/test/java/net/shibboleth/idp/saml/attribute/transcoding/impl/SAML2StringAttributeTranscoderTest.java
@@ -54,7 +54,7 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/** {@link SAML2StringAttributeTranscoder} unit test. */
-public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
+public class SAML2StringAttributeTranscoderTest extends OpenSAMLInitBaseTestCase {
private AttributeTranscoderRegistryImpl registry;
@@ -111,7 +111,7 @@ public class SAML2StringAttributeEncoderTest extends OpenSAMLInitBaseTestCase {
registry = null;
}
- @Test(expectedExceptions = {AttributeEncodingException.class,}) public void emptyEncode() throws Exception {
+ @Test public void emptyEncode() throws Exception {
final IdPAttribute inputAttribute = new IdPAttribute(ATTR_NAME);
final Collection<Properties> rulesets = registry.getTranscodingProperties(inputAttribute, Attribute.class);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list