diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 6add145a5d89b0b5edf23e0b0f8030c57bf4a240..0b63720843d9bbb697ec8b6b48d01653e7b0a177 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 0b567ad39643d212e80fe8c3ca135e45f9655d6f..7f5db6c48cfaa8249f06670649665bf538f75b09 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 6b4f0fa9345fdb9e2ec0d77f04f0e39e4191a63f..f2627fffc238f5d93825681da53be372acfc1d64 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