Skip to content
Snippets Groups Projects
Commit 33aa4f30 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Refactor SimpleSAML\Utils\Arrays::normalizeAttributesArray() to...

Refactor SimpleSAML\Utils\Arrays::normalizeAttributesArray() to SimpleSAML\Utils\Attributes::normalizeAttributesArray().
parent 60641a82
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
......
......@@ -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
}
......@@ -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;
}
}
......@@ -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 . ': ' .
......
......@@ -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());
......
......@@ -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());
......
......@@ -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 . ': ' .
......
......@@ -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.
......
......@@ -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'
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment