From 3a12e85d48f5d2efacbb87e13729eed66af0c1dc Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Wed, 5 Aug 2015 11:59:17 +0200
Subject: [PATCH] Avoid code duplication when checking if an entity should be
 hidden from discovery service.

---
 lib/SimpleSAML/Metadata/SAMLParser.php   |  8 ++---
 lib/SimpleSAML/Utils/Config/Metadata.php | 43 +++++++++++++++++++++++-
 www/saml2/idp/metadata.php               |  8 ++---
 3 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/lib/SimpleSAML/Metadata/SAMLParser.php b/lib/SimpleSAML/Metadata/SAMLParser.php
index 5cbd5632a..394038dbc 100644
--- a/lib/SimpleSAML/Metadata/SAMLParser.php
+++ b/lib/SimpleSAML/Metadata/SAMLParser.php
@@ -490,12 +490,8 @@ class SimpleSAML_Metadata_SAMLParser
             $metadata['EntityAttributes'] = $this->entityAttributes;
 
             // check for entity categories
-            $entity_category = 'http://macedir.org/entity-category';
-            $hide_from_discovery = 'http://refeds.org/category/hide-from-discovery';
-            if (array_key_exists($entity_category, $metadata['EntityAttributes'])) {
-                if (in_array($hide_from_discovery, $metadata['EntityAttributes'][$entity_category])) {
-                    $metadata['hide.from.discovery'] = true;
-                }
+            if (SimpleSAML\Utils\Config\Metadata::isHiddenFromDiscovery($metadata)) {
+                $metadata['hide.from.discovery'] = true;
             }
         }
 
diff --git a/lib/SimpleSAML/Utils/Config/Metadata.php b/lib/SimpleSAML/Utils/Config/Metadata.php
index 4e086ed67..9ec472015 100644
--- a/lib/SimpleSAML/Utils/Config/Metadata.php
+++ b/lib/SimpleSAML/Utils/Config/Metadata.php
@@ -10,6 +10,22 @@ namespace SimpleSAML\Utils\Config;
 class Metadata
 {
 
+    /**
+     * The string that identities Entity Categories.
+     *
+     * @var string
+     */
+    public static $ENTITY_CATEGORY = 'http://macedir.org/entity-category';
+
+
+    /**
+     * The string the identifies the REFEDS "Hide From Discovery" Entity Category.
+     *
+     * @var string
+     */
+    public static $HIDE_FROM_DISCOVERY = 'http://refeds.org/category/hide-from-discovery';
+
+
     /**
      * @var array The valid configuration options for a contact configuration array.
      * @see "Metadata for the OASIS Security Assertion Markup Language (SAML) V2.0", section 2.3.2.2.
@@ -157,7 +173,9 @@ class Metadata
             if (empty($contact['telephoneNumber']) ||
                 !(is_string($contact['telephoneNumber']) || is_array($contact['telephoneNumber']))
             ) {
-                throw new \InvalidArgumentException('"telephoneNumber" must be a string or an array and cannot be empty.');
+                throw new \InvalidArgumentException(
+                    '"telephoneNumber" must be a string or an array and cannot be empty.'
+                );
             }
             if (is_array($contact['telephoneNumber'])) {
                 foreach ($contact['telephoneNumber'] as $address) {
@@ -223,4 +241,27 @@ class Metadata
          */
         return $firstAllowed;
     }
+
+
+    /**
+     * Determine if an entity should be hidden in the discovery service.
+     *
+     * This method searches for the "Hide From Discovery" REFEDS Entity Category, and tells if the entity should be
+     * hidden or not depending on it.
+     *
+     * @see https://refeds.org/category/hide-from-discovery
+     *
+     * @param array $metadata An associative array with the metadata representing an entity.
+     *
+     * @return boolean True if the entity should be hidden, false otherwise.
+     */
+    public static function isHiddenFromDiscovery($metadata)
+    {
+        if (array_key_exists(self::$ENTITY_CATEGORY, $metadata['EntityAttributes'])) {
+            if (in_array(self::$HIDE_FROM_DISCOVERY, $metadata['EntityAttributes'][self::$ENTITY_CATEGORY])) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
diff --git a/www/saml2/idp/metadata.php b/www/saml2/idp/metadata.php
index 979c41c2e..7784a117e 100644
--- a/www/saml2/idp/metadata.php
+++ b/www/saml2/idp/metadata.php
@@ -147,12 +147,8 @@ try {
         $metaArray['EntityAttributes'] = $idpmeta->getArray('EntityAttributes');
 
         // check for entity categories
-        $entity_category = 'http://macedir.org/entity-category';
-        $hide_from_discovery = 'http://refeds.org/category/hide-from-discovery';
-        if (array_key_exists($entity_category, $metaArray['EntityAttributes'])) {
-            if (in_array($hide_from_discovery, $metaArray['EntityAttributes'][$entity_category])) {
-                $metaArray['hide.from.discovery'] = true;
-            }
+        if (SimpleSAML\Utils\Config\Metadata::isHiddenFromDiscovery($metaArray)) {
+            $metaArray['hide.from.discovery'] = true;
         }
     }
 
-- 
GitLab