diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index c7eb315b1212729082dab7ab2269fe51284379af..710f29f78162439c21292d1a0d362c86f1b018b6 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -429,11 +429,11 @@ class SimpleSAML_Utilities /** * @deprecated This method will be removed in SSP 2.0. Please use - * SimpleSAML\Utils\Arrays::normalizeAttributesArray() instead. + * SimpleSAML\Utils\Attributes::normalizeAttributesArray() instead. */ public static function parseAttributes($attributes) { - return SimpleSAML\Utils\Arrays::normalizeAttributesArray($attributes); + return SimpleSAML\Utils\Attributes::normalizeAttributesArray($attributes); } diff --git a/lib/SimpleSAML/Utils/Arrays.php b/lib/SimpleSAML/Utils/Arrays.php index a620a113f198599994d12ed9cbddb450cac59896..b7af49d0189ad8cdfafa03de912c5912083b8a13 100644 --- a/lib/SimpleSAML/Utils/Arrays.php +++ b/lib/SimpleSAML/Utils/Arrays.php @@ -26,50 +26,6 @@ class Arrays return (is_array($data)) ? $data : array($index => $data); } - /** - * 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 \InvalidArgumentException 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> - * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> - */ - public static function normalizeAttributesArray($attributes) - { - - if (!is_array($attributes)) { - throw new \InvalidArgumentException('Attributes was not an array. Was: '.print_r($attributes, true).'".'); - } - - $newAttrs = array(); - foreach ($attributes as $name => $values) { - if (!is_string($name)) { - throw new \InvalidArgumentException('Invalid attribute name: "'.print_r($name, true).'".'); - } - - $values = self::arrayize($values); - - foreach ($values as $value) { - if (!is_string($value)) { - throw new \InvalidArgumentException('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']. @@ -101,4 +57,4 @@ class Arrays } return $ret; } -} \ No newline at end of file +} diff --git a/lib/SimpleSAML/Utils/Attributes.php b/lib/SimpleSAML/Utils/Attributes.php index 6f67bacb5bc8061b544810197c792c03ad2165cf..d4b6d28d701a43f5848a473110e795543992333d 100644 --- a/lib/SimpleSAML/Utils/Attributes.php +++ b/lib/SimpleSAML/Utils/Attributes.php @@ -54,4 +54,52 @@ class Attributes } return reset($attribute); } + + + /** + * 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 \InvalidArgumentException 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> + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> + */ + public static function normalizeAttributesArray($attributes) + { + if (!is_array($attributes)) { + throw new \InvalidArgumentException( + 'The attributes array is not an array, it is: '.print_r($attributes, true).'".' + ); + } + + $newAttrs = array(); + foreach ($attributes as $name => $values) { + if (!is_string($name)) { + throw new \InvalidArgumentException('Invalid attribute name: "'.print_r($name, true).'".'); + } + + $values = Arrays::arrayize($values); + + foreach ($values as $value) { + if (!is_string($value)) { + throw new \InvalidArgumentException( + 'Invalid attribute value for attribute '.$name.': "'.print_r($value, true).'".' + ); + } + } + + $newAttrs[$name] = $values; + } + + return $newAttrs; + } } diff --git a/modules/authcrypt/lib/Auth/Source/Hash.php b/modules/authcrypt/lib/Auth/Source/Hash.php index 7c684176bf4a76a570dd549a64737f156a639960..6cde4b21835220bb53f2401e76614724b98b8df1 100644 --- a/modules/authcrypt/lib/Auth/Source/Hash.php +++ b/modules/authcrypt/lib/Auth/Source/Hash.php @@ -50,7 +50,7 @@ class sspmod_authcrypt_Auth_Source_Hash extends sspmod_core_Auth_UserPassBase { $passwordhash = $userpass[1]; try { - $attributes = SimpleSAML\Utils\Arrays::normalizeAttributesArray($attributes); + $attributes = SimpleSAML\Utils\Attributes::normalizeAttributesArray($attributes); } catch(Exception $e) { throw new Exception('Invalid attributes for user ' . $username . ' in authentication source ' . $this->authId . ': ' . diff --git a/modules/authcrypt/lib/Auth/Source/Htpasswd.php b/modules/authcrypt/lib/Auth/Source/Htpasswd.php index bf9fd83800f3c2fa103a8ded55adeb2293bcb323..af8ac9b70469d2950a1e5c02fb1232ba5d2e5c57 100644 --- a/modules/authcrypt/lib/Auth/Source/Htpasswd.php +++ b/modules/authcrypt/lib/Auth/Source/Htpasswd.php @@ -39,7 +39,7 @@ class sspmod_authcrypt_Auth_Source_Htpasswd extends sspmod_core_Auth_UserPassBas $this->users = explode("\n", trim($htpasswd)); try { - $this->attributes = SimpleSAML\Utils\Arrays::normalizeAttributesArray($config['static_attributes']); + $this->attributes = SimpleSAML\Utils\Attributes::normalizeAttributesArray($config['static_attributes']); } catch(Exception $e) { throw new Exception('Invalid static_attributes in authentication source ' . $this->authId . ': ' . $e->getMessage()); diff --git a/modules/exampleauth/lib/Auth/Source/Static.php b/modules/exampleauth/lib/Auth/Source/Static.php index 835544214347dfb67283e9e36b08621078464020..07351df97d4199a5449eff7ff0249dd7f001a5f6 100644 --- a/modules/exampleauth/lib/Auth/Source/Static.php +++ b/modules/exampleauth/lib/Auth/Source/Static.php @@ -34,7 +34,7 @@ class sspmod_exampleauth_Auth_Source_Static extends SimpleSAML_Auth_Source { /* Parse attributes. */ try { - $this->attributes = SimpleSAML\Utils\Arrays::normalizeAttributesArray($config); + $this->attributes = SimpleSAML\Utils\Attributes::normalizeAttributesArray($config); } catch(Exception $e) { throw new Exception('Invalid attributes for authentication source ' . $this->authId . ': ' . $e->getMessage()); diff --git a/modules/exampleauth/lib/Auth/Source/UserPass.php b/modules/exampleauth/lib/Auth/Source/UserPass.php index 1b380de5953c343d40800885bf6bc87ce3c78186..22923c1e635d76febc5dca866e7783fea8d4fa2d 100644 --- a/modules/exampleauth/lib/Auth/Source/UserPass.php +++ b/modules/exampleauth/lib/Auth/Source/UserPass.php @@ -50,7 +50,7 @@ class sspmod_exampleauth_Auth_Source_UserPass extends sspmod_core_Auth_UserPassB $password = $userpass[1]; try { - $attributes = SimpleSAML\Utils\Arrays::normalizeAttributesArray($attributes); + $attributes = SimpleSAML\Utils\Attributes::normalizeAttributesArray($attributes); } catch(Exception $e) { throw new Exception('Invalid attributes for user ' . $username . ' in authentication source ' . $this->authId . ': ' . diff --git a/tests/lib/SimpleSAML/Utils/ArraysTest.php b/tests/lib/SimpleSAML/Utils/ArraysTest.php index 5cc00de5dc2657a10dd26f680f29befc5e3c3278..014dd24b3dc2e2d1c4c984195842e4d470020547 100644 --- a/tests/lib/SimpleSAML/Utils/ArraysTest.php +++ b/tests/lib/SimpleSAML/Utils/ArraysTest.php @@ -33,55 +33,6 @@ class Utils_ArraysTest extends PHPUnit_Framework_TestCase $this->assertEquals($expected, SimpleSAML\Utils\Arrays::arrayize($expected[$index], $index)); } - /** - * Test the normalizeAttributesArray() function with input not being an array - * - * @expectedException InvalidArgumentException - */ - public function testNormalizeAttributesArrayBadInput() - { - SimpleSAML\Utils\Arrays::normalizeAttributesArray('string'); - } - - /** - * Test the normalizeAttributesArray() function with an array with non-string attribute names. - * - * @expectedException InvalidArgumentException - */ - 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 InvalidArgumentException - */ - 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. diff --git a/tests/lib/SimpleSAML/Utils/AttributesTest.php b/tests/lib/SimpleSAML/Utils/AttributesTest.php index 1960de025c78107a0a20564aada978832dc42e49..72daf1967fb4607c48f831a2f26fa02e9942aae9 100644 --- a/tests/lib/SimpleSAML/Utils/AttributesTest.php +++ b/tests/lib/SimpleSAML/Utils/AttributesTest.php @@ -98,4 +98,57 @@ class Utils_AttributesTest extends PHPUnit_Framework_TestCase $expected = 'attribute'; $this->assertEquals($value, \SimpleSAML\Utils\Attributes::getExpectedAttribute($attributes, $expected, true)); } + + + /** + * Test the normalizeAttributesArray() function with input not being an array + * + * @expectedException InvalidArgumentException + */ + public function testNormalizeAttributesArrayBadInput() + { + SimpleSAML\Utils\Attributes::normalizeAttributesArray('string'); + } + + /** + * Test the normalizeAttributesArray() function with an array with non-string attribute names. + * + * @expectedException InvalidArgumentException + */ + public function testNormalizeAttributesArrayBadKeys() + { + SimpleSAML\Utils\Attributes::normalizeAttributesArray(array('attr1' => 'value1', 1 => 'value2')); + } + + /** + * Test the normalizeAttributesArray() function with an array with non-string attribute values. + * + * @expectedException InvalidArgumentException + */ + public function testNormalizeAttributesArrayBadValues() + { + SimpleSAML\Utils\Attributes::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\Attributes::normalizeAttributesArray($attributes), + 'Attribute array normalization failed' + ); + } }