This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
forked from lightSAML/SpBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lightSAML#36 from denniscoorn/feature/simple-attri…
…bute-mapper Added a SimpleAttributeMapper
- Loading branch information
Showing
8 changed files
with
239 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/LightSaml/SpBundle/Security/User/SimpleAttributeMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the LightSAML SP-Bundle package. | ||
* | ||
* (c) Milos Tomic <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace LightSaml\SpBundle\Security\User; | ||
|
||
use LightSaml\Model\Assertion\Assertion; | ||
use LightSaml\Model\Assertion\Attribute; | ||
use LightSaml\Model\Assertion\AttributeStatement; | ||
use LightSaml\SpBundle\Security\Authentication\Token\SamlSpResponseToken; | ||
|
||
class SimpleAttributeMapper implements AttributeMapperInterface | ||
{ | ||
/** | ||
* @param SamlSpResponseToken $token | ||
* | ||
* @return array | ||
*/ | ||
public function getAttributes(SamlSpResponseToken $token) | ||
{ | ||
$response = $token->getResponse(); | ||
$assertions = $response->getAllAssertions(); | ||
|
||
return array_reduce($assertions, [$this, 'resolveAttributesFromAssertion'], []); | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param Assertion $assertion | ||
* | ||
* @return array | ||
*/ | ||
private function resolveAttributesFromAssertion(array $attributes, Assertion $assertion) | ||
{ | ||
$attributeStatements = $assertion->getAllAttributeStatements(); | ||
|
||
return array_reduce($attributeStatements, [$this, 'resolveAttributesFromAttributeStatement'], $attributes); | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param AttributeStatement $attributeStatement | ||
* | ||
* @return array | ||
*/ | ||
private function resolveAttributesFromAttributeStatement(array $attributes, AttributeStatement $attributeStatement) | ||
{ | ||
$statementAttributes = $attributeStatement->getAllAttributes(); | ||
|
||
return array_reduce($statementAttributes, [$this, 'mapAttributeValues'], $attributes); | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param Attribute $attribute | ||
* | ||
* @return array | ||
*/ | ||
private function mapAttributeValues(array $attributes, Attribute $attribute) | ||
{ | ||
$key = $attribute->getName(); | ||
$value = $attribute->getAllAttributeValues(); | ||
|
||
if (!array_key_exists($key, $attributes) && count($value) === 1) { | ||
$value = array_shift($value); | ||
} | ||
|
||
if (array_key_exists($key, $attributes)) { | ||
$currentValue = (is_array($attributes[$key]) ? $attributes[$key] : [$attributes[$key]]); | ||
|
||
$value = array_merge($currentValue, $value); | ||
} | ||
|
||
$attributes[$key] = $value; | ||
|
||
return $attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
tests/LightSaml/SpBundle/Tests/Security/User/SimpleAttributeMapperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace LightSaml\SpBundle\Tests\Security\User; | ||
|
||
use LightSaml\Model\Assertion\Assertion; | ||
use LightSaml\Model\Assertion\Attribute; | ||
use LightSaml\Model\Assertion\AttributeStatement; | ||
use LightSaml\Model\Protocol\Response; | ||
use LightSaml\SpBundle\Security\Authentication\Token\SamlSpResponseToken; | ||
use LightSaml\SpBundle\Security\User\SimpleAttributeMapper; | ||
|
||
class SimpleAttributeMapperTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function test_get_attributes_from_single_assertion_response() | ||
{ | ||
$assertion = $this->buildAssertion([ | ||
'organization' => 'test', | ||
'name' => 'John', | ||
'email_address' => '[email protected]', | ||
'test' => ['one', 'two'], | ||
]); | ||
$response = $this->buildResponse($assertion); | ||
$samlSpResponseToken = $this->buildSamlSpResponseToken($response); | ||
|
||
$expectedAttributes = [ | ||
'organization' => 'test', | ||
'name' => 'John', | ||
'email_address' => '[email protected]', | ||
'test' => ['one', 'two'], | ||
]; | ||
|
||
$simpleAttributeMapper = new SimpleAttributeMapper(); | ||
$actualAttributes = $simpleAttributeMapper->getAttributes($samlSpResponseToken); | ||
|
||
$this->assertEquals($expectedAttributes, $actualAttributes); | ||
} | ||
|
||
public function test_get_attributes_from_multi_assertions_response() | ||
{ | ||
$assertion = $this->buildAssertion([ | ||
'organization' => 'test', | ||
'name' => 'John', | ||
'email_address' => '[email protected]', | ||
'test' => ['one', 'two'], | ||
]); | ||
$response = $this->buildResponse($assertion); | ||
|
||
$assertion = $this->buildAssertion([ | ||
'name' => 'Doe', | ||
'email_address' => '[email protected]', | ||
'test' => ['three', 'four'], | ||
]); | ||
$response = $this->buildResponse($assertion, $response); | ||
|
||
$samlSpResponseToken = $this->buildSamlSpResponseToken($response); | ||
|
||
$expectedAttributes = [ | ||
'organization' => 'test', | ||
'name' => ['John', 'Doe'], | ||
'email_address' => ['[email protected]', '[email protected]'], | ||
'test' => ['one', 'two', 'three', 'four'], | ||
]; | ||
|
||
$simpleAttributeMapper = new SimpleAttributeMapper(); | ||
$actualAttributes = $simpleAttributeMapper->getAttributes($samlSpResponseToken); | ||
|
||
$this->assertEquals($expectedAttributes, $actualAttributes); | ||
} | ||
|
||
public function test_get_attributes_from_multi_attribute_statements_response() | ||
{ | ||
$assertion = $this->buildAssertion([ | ||
'organization' => 'test', | ||
'name' => 'John', | ||
'email_address' => '[email protected]', | ||
'test' => ['one', 'two'] | ||
]); | ||
$assertion = $this->buildAssertion([ | ||
'name' => 'Doe', | ||
'email_address' => '[email protected]', | ||
'test' => ['three', 'four'] | ||
], $assertion); | ||
$response = $this->buildResponse($assertion); | ||
|
||
$samlSpResponseToken = $this->buildSamlSpResponseToken($response); | ||
|
||
$expectedAttributes = [ | ||
'organization' => 'test', | ||
'name' => ['John', 'Doe'], | ||
'email_address' => ['[email protected]', '[email protected]'], | ||
'test' => ['one', 'two', 'three', 'four'], | ||
]; | ||
|
||
$simpleAttributeMapper = new SimpleAttributeMapper(); | ||
$actualAttributes = $simpleAttributeMapper->getAttributes($samlSpResponseToken); | ||
|
||
$this->assertEquals($expectedAttributes, $actualAttributes); | ||
} | ||
|
||
/** | ||
* @param Response $response | ||
* | ||
* @return \LightSaml\SpBundle\Security\Authentication\Token\SamlSpResponseToken | ||
*/ | ||
private function buildSamlSpResponseToken(Response $response) | ||
{ | ||
return new SamlSpResponseToken($response, 'test'); | ||
} | ||
|
||
/** | ||
* @param Assertion $assertion | ||
* @param Response $response | ||
* | ||
* @return Response | ||
*/ | ||
private function buildResponse(Assertion $assertion, Response $response = null) | ||
{ | ||
if (null == $response) { | ||
$response = new Response(); | ||
} | ||
|
||
$response->addAssertion($assertion); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @param array $assertionAttributes | ||
* @param Assertion $assertion | ||
* | ||
* @return Assertion | ||
*/ | ||
private function buildAssertion(array $assertionAttributes, Assertion $assertion = null) | ||
{ | ||
if (null == $assertion) { | ||
$assertion = new Assertion(); | ||
} | ||
|
||
$assertion->addItem($attributeStatement = new AttributeStatement()); | ||
foreach ($assertionAttributes as $attributeName => $attributeValue) { | ||
$attributeStatement->addAttribute(new Attribute($attributeName, $attributeValue)); | ||
} | ||
|
||
return $assertion; | ||
} | ||
} |