[java-identity-provider] branch main updated: IDP-2240 Improve adding attributes to views
Rod Widdowson
rdw at steadingsoftware.com
Thu Feb 8 20:12:06 UTC 2024
This is an automated email from the git hooks/post-receive script.
rdw pushed a commit to branch main
in repository java-identity-provider.
View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=ee1a2ed3565c614cc0dba1edfede2880b661f3b1
The following commit(s) were added to refs/heads/main by this push:
new ee1a2ed35 IDP-2240 Improve adding attributes to views
ee1a2ed35 is described below
commit ee1a2ed3565c614cc0dba1edfede2880b661f3b1
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Feb 8 19:49:06 2024 +0000
IDP-2240 Improve adding attributes to views
https://shibboleth.atlassian.net/browse/IDP-2240
Initial cut at helper class
---
idp-ui/pom.xml | 6 +
.../shibboleth/idp/ui/impl/AttributeHelper.java | 139 +++++++++++++++++++++
.../net/shibboleth/idp/ui/AttributeHelperTest.java | 39 ++++++
3 files changed, 184 insertions(+)
diff --git a/idp-ui/pom.xml b/idp-ui/pom.xml
index 1ebae9a06..a96f16832 100644
--- a/idp-ui/pom.xml
+++ b/idp-ui/pom.xml
@@ -98,6 +98,12 @@
<!-- Runtime Dependencies -->
<!-- Test Dependencies -->
+ <dependency>
+ <groupId>${shib-attribute.groupId}</groupId>
+ <artifactId>shib-attribute-testing</artifactId>
+ <scope>test</scope>
+ </dependency>
+
<!-- Needed for XMLObject providers. -->
<dependency>
<groupId>${opensaml.groupId}</groupId>
diff --git a/idp-ui/src/main/java/net/shibboleth/idp/ui/impl/AttributeHelper.java b/idp-ui/src/main/java/net/shibboleth/idp/ui/impl/AttributeHelper.java
new file mode 100644
index 000000000..d478e2b3c
--- /dev/null
+++ b/idp-ui/src/main/java/net/shibboleth/idp/ui/impl/AttributeHelper.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed 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.ui.impl;
+
+import java.util.List;
+import java.util.function.Function;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.messaging.context.navigate.ChildContextLookup;
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.slf4j.Logger;
+
+import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.IdPAttributeValue;
+import net.shibboleth.idp.attribute.context.AttributeContext;
+import net.shibboleth.profile.context.RelyingPartyContext;
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent;
+import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.LoggerFactory;
+
+/** Class to help Attribute Extraction in views. */
+public final class AttributeHelper extends AbstractIdentifiableInitializableComponent {
+
+ /** How to get the AttributeContext we are looking at. */
+ @SuppressWarnings("null")
+ @Nonnull private Function<ProfileRequestContext,AttributeContext> attributeContextStrategy =
+ new ChildContextLookup<>(AttributeContext.class).compose(new ChildContextLookup<>(RelyingPartyContext.class));
+
+ /** Logger. */
+ @Nonnull private final Logger log = LoggerFactory.getLogger(AttributeHelper.class);
+
+ /** Set the way to get the {@link AttributeContext}.
+ * @param strategy the strategy to use
+ */
+ public void setAttributeContextStrategy(@Nonnull
+ Function<ProfileRequestContext, AttributeContext> strategy) {
+ attributeContextStrategy = Constraint.isNotNull(strategy, "Injected strategy must not be null");
+ }
+
+ /** Return the first (filtered) attribute Value from the attribute of that name.
+ * @param prc the ProfileRequestContext
+ * @param attributeName the attribute name to look up
+ * @param defaultValue what to return if nothing found.
+ * @return the default value or the attribute value
+ */
+ @Nonnull public String getFirstAttributeValue(final ProfileRequestContext prc,
+ final @Nonnull @NotEmpty String attributeName,
+ final @Nonnull String defaultValue) {
+ if (prc == null) {
+ log.error("Provided ProfileRequestContext was null, returning {}", defaultValue);
+ return defaultValue;
+ }
+ Constraint.isNotNull(defaultValue, "Default value must be non-null");
+ Constraint.isNotNull(attributeName, "Attribute Name must be non-niull");
+ final AttributeContext context = attributeContextStrategy.apply(prc);
+ if (context == null) {
+ log.error("Attribute Context could not be located, returning {}", defaultValue);
+ return defaultValue;
+ }
+ return getFirstValue(context.getIdPAttributes().get(attributeName), defaultValue);
+ }
+
+ /** Return the first (filtered) attribute Value from the attribute of that name.
+ * @param prc the ProfileRequestContext
+ * @param attributeName the attribute name to look up
+ * @return the attribute value or ""
+ */
+ @Nonnull public String getFirstAttributeValue(final ProfileRequestContext prc,
+ final @Nonnull @NotEmpty String attributeName) {
+ return getFirstAttributeValue(prc, attributeName, "");
+ }
+
+ /** Return the first (unfiltered) attribute Value from the attribute of that name.
+ * @param prc the ProfileRequestContext
+ * @param attributeName the attribute name to look up
+ * @param defaultValue what to return if nothing found.
+ * @return the default value or the attribute value
+ */
+ @Nonnull public String getFirstUnfilteredAttributeValue(final ProfileRequestContext prc,
+ final @Nonnull @NotEmpty String attributeName,
+ final @Nonnull String defaultValue) {
+
+ if (prc == null) {
+ log.error("Provided ProfileRequestContext was null, returning {}", defaultValue);
+ return defaultValue;
+ }
+ Constraint.isNotNull(defaultValue, "Default value must be non-null");
+ Constraint.isNotNull(attributeName, "Attribute Name must be non-niull");
+ final AttributeContext context = attributeContextStrategy.apply(prc);
+ if (context == null) {
+ log.error("Attribute Context could not be located, returning {}", defaultValue);
+ return defaultValue;
+ }
+ return getFirstValue(context.getUnfilteredIdPAttributes().get(attributeName), defaultValue);
+ }
+
+ /** Return the first (unfiltered) attribute Value from the attribute of that name.
+ * @param prc the ProfileRequestContext
+ * @param attributeName the attribute name to look up
+ * @param defaultValue what to return if nothing found.
+ * @return the attribute value or ""
+ */
+ @Nonnull public String getFirstUnfilteredAttributeValue(final ProfileRequestContext prc,
+ @Nonnull @NotEmpty final String attributeName) {
+ return getFirstUnfilteredAttributeValue(prc, attributeName, "");
+ }
+
+ /** Helper method to get the first attribute name from the attribute.
+ * @param Attribute the Attribute or null if there wasn't one
+ * @param defaultValue the value to return if no values are found
+ * @return defaultValue or the display string of the first attribute
+ */
+ @Nonnull private String getFirstValue(final @Nullable IdPAttribute attribute, @Nonnull String defaultValue) {
+ if (attribute == null) {
+ log.info("No attribute found, returning {}", defaultValue);
+ return defaultValue;
+ }
+ List<IdPAttributeValue> values = attribute.getValues();
+ if (values == null|| values.size() < 1) {
+ log.info("No attribute values associated with {}, returning {}", attribute.getId(), defaultValue);
+ return defaultValue;
+ }
+ return values.get(0).getDisplayValue();
+ }
+}
diff --git a/idp-ui/src/test/java/net/shibboleth/idp/ui/AttributeHelperTest.java b/idp-ui/src/test/java/net/shibboleth/idp/ui/AttributeHelperTest.java
new file mode 100644
index 000000000..beb14d1fb
--- /dev/null
+++ b/idp-ui/src/test/java/net/shibboleth/idp/ui/AttributeHelperTest.java
@@ -0,0 +1,39 @@
+package net.shibboleth.idp.ui;
+
+import static org.testng.Assert.assertEquals;
+
+import org.opensaml.profile.context.ProfileRequestContext;
+import org.testng.annotations.Test;
+
+import net.shibboleth.idp.attribute.IdPAttribute;
+import net.shibboleth.idp.attribute.context.AttributeContext;
+import net.shibboleth.idp.attribute.resolver.testing.ResolverTestSupport;
+import net.shibboleth.idp.ui.impl.AttributeHelper;
+import net.shibboleth.profile.context.RelyingPartyContext;
+import net.shibboleth.shared.collection.CollectionSupport;
+import net.shibboleth.shared.component.ComponentInitializationException;
+
+public class AttributeHelperTest {
+
+ @Test public void TestHelper() throws ComponentInitializationException {
+ final ProfileRequestContext prc = new ProfileRequestContext();
+ final AttributeContext ac = prc.ensureSubcontext(RelyingPartyContext.class).ensureSubcontext(AttributeContext.class);
+ final IdPAttribute a1 = ResolverTestSupport.buildAttribute("A1", "A1Value1", "Value2");
+ final IdPAttribute a2 = ResolverTestSupport.buildAttribute("A2", "A2Value1");
+ final AttributeHelper ah = new AttributeHelper();
+ ah.setId("AttributeHelper");
+ ah.initialize();
+
+ ac.setUnfilteredIdPAttributes(CollectionSupport.arrayAsList(a1, a2));
+ ac.setIdPAttributes(CollectionSupport.singleton(a1));
+
+ assertEquals(ah.getFirstAttributeValue(prc, "A1"), "A1Value1");
+ assertEquals(ah.getFirstAttributeValue(prc, "A2"), "");
+ assertEquals(ah.getFirstAttributeValue(prc, "A3", "Nothing"), "Nothing");
+
+ assertEquals(ah.getFirstUnfilteredAttributeValue(prc, "A1"), "A1Value1");
+ assertEquals(ah.getFirstUnfilteredAttributeValue(prc, "A2"), "A2Value1");
+ assertEquals(ah.getFirstAttributeValue(prc, "A3"), "");
+
+ }
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list