From b7bf7242d47857e4dda6e534436519cc6864775e Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Wed, 15 Apr 2015 19:11:37 +0200
Subject: [PATCH] Refactor SimpleSAML_Utilities::parseAttributes() to
 SimpleSAML_Utils_Arrays:: normalizeAttributesArray().

Schedule SimpleSAML_Utilities:: parseAttributes() for removal.
---
 lib/SimpleSAML/Utilities.php    |  2 ++
 lib/SimpleSAML/Utils/Arrays.php | 46 +++++++++++++++++++++++++++++++
 tests/Utils/Arrays.php          | 49 +++++++++++++++++++++++++++++++--
 3 files changed, 95 insertions(+), 2 deletions(-)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 6add145a5..0b6372084 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -1248,6 +1248,8 @@ class SimpleSAML_Utilities {
 	 *
 	 * @param array $attributes  The attributes we should parse and validate.
 	 * @return array  The parsed attributes.
+	 * @deprecated This method will be removed in SSP 2.0. Please use
+	 * SimpleSAML_Utils_Arrays::normalizeAttributesArray() instead.
 	 */
 	public static function parseAttributes($attributes) {
 
diff --git a/lib/SimpleSAML/Utils/Arrays.php b/lib/SimpleSAML/Utils/Arrays.php
index 0b567ad39..7f5db6c48 100644
--- a/lib/SimpleSAML/Utils/Arrays.php
+++ b/lib/SimpleSAML/Utils/Arrays.php
@@ -9,6 +9,52 @@
 class SimpleSAML_Utils_Arrays
 {
 
+    /**
+     * Validate and normalize an array with attributes.
+     *
+     * This function takes in an associative array with attributes, and parses and validates
+     * this array. On success, it will return a normalized array, where each attribute name
+     * is an index to an array of one or more strings. On failure an exception will be thrown.
+     * This exception will contain an message describing what is wrong.
+     *
+     * @param array $attributes The array containing attributes that we should validate and normalize.
+     *
+     * @return array The normalized attributes array.
+     * @throws SimpleSAML_Error_Exception If input is not an array, array keys are not strings or attribute values are
+     *     not strings.
+     *
+     * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
+     */
+    public static function normalizeAttributesArray($attributes)
+    {
+
+        if (!is_array($attributes)) {
+            throw new SimpleSAML_Error_Exception('Attributes was not an array. Was: '.print_r($attributes, true).'".');
+        }
+
+        $newAttrs = array();
+        foreach ($attributes as $name => $values) {
+            if (!is_string($name)) {
+                throw new SimpleSAML_Error_Exception('Invalid attribute name: "'.print_r($name, true).'".');
+            }
+
+            if (!is_array($values)) {
+                $values = array($values);
+            }
+
+            foreach ($values as $value) {
+                if (!is_string($value)) {
+                    throw new SimpleSAML_Error_Exception('Invalid attribute value for attribute '.$name.
+                        ': "'.print_r($value, true).'".');
+                }
+            }
+
+            $newAttrs[$name] = $values;
+        }
+
+        return $newAttrs;
+    }
+
     /**
      * This function transposes a two-dimensional array, so that $a['k1']['k2'] becomes $a['k2']['k1'].
      *
diff --git a/tests/Utils/Arrays.php b/tests/Utils/Arrays.php
index 6b4f0fa93..f2627fffc 100644
--- a/tests/Utils/Arrays.php
+++ b/tests/Utils/Arrays.php
@@ -7,6 +7,51 @@
 class Utils_Arrays extends PHPUnit_Framework_TestCase
 {
 
+    /**
+     * Test the normalizeAttributesArray() function with input not being an array
+     *
+     * @expectedException SimpleSAML_Error_Exception
+     */
+    public function testNormalizeAttributesArrayBadInput() {
+        SimpleSAML_Utils_Arrays::normalizeAttributesArray('string');
+    }
+
+    /**
+     * Test the normalizeAttributesArray() function with an array with non-string attribute names.
+     *
+     * @expectedException SimpleSAML_Error_Exception
+     */
+    public function testNormalizeAttributesArrayBadKeys() {
+        SimpleSAML_Utils_Arrays::normalizeAttributesArray(array('attr1' => 'value1', 1 => 'value2'));
+    }
+
+    /**
+     * Test the normalizeAttributesArray() function with an array with non-string attribute values.
+     *
+     * @expectedException SimpleSAML_Error_Exception
+     */
+    public function testNormalizeAttributesArrayBadValues() {
+        SimpleSAML_Utils_Arrays::normalizeAttributesArray(array('attr1' => 'value1', 'attr2' => 0));
+    }
+
+    /**
+     * Test the normalizeAttributesArray() function.
+     */
+    public function testNormalizeAttributesArray() {
+        $attributes = array(
+            'key1' => 'value1',
+            'key2' => array('value2', 'value3'),
+            'key3' => 'value1'
+        );
+        $expected = array(
+            'key1' => array('value1'),
+            'key2' => array('value2', 'value3'),
+            'key3' => array('value1')
+        );
+        $this->assertEquals($expected, SimpleSAML_Utils_Arrays::normalizeAttributesArray($attributes),
+            'Attribute array normalization failed');
+    }
+
 
     /**
      * Test the transpose() function.
@@ -14,9 +59,9 @@ class Utils_Arrays extends PHPUnit_Framework_TestCase
     public function testTranspose()
     {
         // check bad arrays
-        $this->assertEquals(false, SimpleSAML_Utils_Arrays::transpose(array('1', '2', '3')),
+        $this->assertFalse(SimpleSAML_Utils_Arrays::transpose(array('1', '2', '3')),
             'Invalid two-dimensional array was accepted');
-        $this->assertEquals(false, SimpleSAML_Utils_Arrays::transpose(array('1' => 0, '2' => '0', '3' => array(0))),
+        $this->assertFalse(SimpleSAML_Utils_Arrays::transpose(array('1' => 0, '2' => '0', '3' => array(0))),
             'Invalid elements on a two-dimensional array were accepted');
 
         // check array with numerical keys
-- 
GitLab