Skip to content

Commit

Permalink
Merge pull request #8 from ecphp/parse-extendedAttributes
Browse files Browse the repository at this point in the history
Parse EU Access attributes.
  • Loading branch information
drupol committed Aug 7, 2020
2 parents e0d253c + 88b8f5b commit 65f3257
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 65 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -21,11 +21,11 @@
"symfony/framework-bundle": "^5.1"
},
"require-dev": {
"drupol/php-conventions": "^1.8.6",
"drupol/php-conventions": "^1.8.16",
"friends-of-phpspec/phpspec-code-coverage": "^4.3.2",
"infection/infection": "^0.15.3",
"phpspec/phpspec": "^6.2.1",
"vimeo/psalm": "^3.12"
"vimeo/psalm": "^3.13"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpspec.yml.dist
Expand Up @@ -8,4 +8,4 @@ extensions:
output:
html: build/coverage
clover: build/logs/clover.xml
php: build/coverage.php
php: build/coverage.php
Expand Up @@ -54,6 +54,12 @@ public function it_can_load_a_user_from_a_response(ResponseInterface $response)
<cas:group>group1</cas:group>
<cas:group>group2</cas:group>
</cas:groups>
<cas:extendedAttributes>
<cas:extendedAttribute name="http://stork.eu/motherInLawDogName">
<cas:attributeValue>rex</cas:attributeValue>
<cas:attributeValue>snoopy</cas:attributeValue>
</cas:extendedAttribute>
</cas:extendedAttributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;
Expand All @@ -77,11 +83,17 @@ public function it_can_load_a_user_from_a_response(ResponseInterface $response)
'group2',
],
],
'extendedAttributes' => [
'http://stork.eu/motherInLawDogName' => [
'rex',
'snoopy',
],
],
]);

$this
->loadUserByResponse($response)
->getUser()
->getUsername()
->shouldReturn('username');

$this
Expand All @@ -92,6 +104,17 @@ public function it_can_load_a_user_from_a_response(ResponseInterface $response)
'group2',
'ROLE_CAS_AUTHENTICATED',
]);

$this->loadUserByResponse($response)
->getExtendedAttributes()
->shouldReturn(
[
'http://stork.eu/motherInLawDogName' => [
'rex',
'snoopy',
],
]
);
}

public function it_can_refresh_a_user(EuLoginUserInterface $user)
Expand Down
207 changes: 159 additions & 48 deletions spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php
Expand Up @@ -6,27 +6,47 @@

use EcPhp\CasBundle\Security\Core\User\CasUser;
use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUser;
use Nyholm\Psr7\Response;
use PhpSpec\ObjectBehavior;

class EuLoginUserSpec extends ObjectBehavior
{
public function it_can_get_groups_when_no_groups_are_available()
{
$attributes = $this->getAttributesData();
unset($attributes['groups']);

$data = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
'attributes' => $attributes,
];
$body = <<<'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
<cas:foo>bar</cas:foo>
<cas:proxies>
<cas:proxy>foo</cas:proxy>
</cas:proxies>
<cas:attributes>
<cas:groups number="10">
<cas:group>group1</cas:group>
<cas:group>group2</cas:group>
</cas:groups>
<cas:extendedAttributes>
<cas:extendedAttribute name="http://stork.eu/motherInLawDogName">
<cas:attributeValue>rex</cas:attributeValue>
<cas:attributeValue>snoopy</cas:attributeValue>
</cas:extendedAttribute>
</cas:extendedAttributes>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;

$response = new Response(200, ['Content-Type' => 'application/xml'], $body);
$data = (new Introspector())->parse($response)['serviceResponse']['authenticationSuccess'];
unset($data['attributes']['groups']);

$casUser = new CasUser($data);

$this
->beConstructedWith(new CasUser($data));
->beConstructedWith($casUser);

$this
->getGroups()
Expand All @@ -37,11 +57,11 @@ public function it_can_get_specific_attribute()
{
$this
->getAssuranceLevel()
->shouldReturn('assuranceLevel');
->shouldReturn('40');

$this
->getAuthenticationFactors()
->shouldReturn(['foobar']);
->shouldReturn(['ecphp@ec.europa.eu']);

$this
->getDepartmentNumber()
Expand Down Expand Up @@ -73,7 +93,10 @@ public function it_can_get_specific_attribute()

$this
->getGroups()
->shouldReturn(['foo']);
->shouldReturn([
'group1',
'group2',
]);

$this
->getLastName()
Expand Down Expand Up @@ -114,28 +137,14 @@ public function it_can_get_specific_attribute()
$this
->getUid()
->shouldReturn('uid');

$this
->getAttributes()
->shouldReturn($this->getAttributesData());
}

public function it_can_get_the_attributes_only(CasUserInterface $user)
{
$data = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
'attributes' => $this->getAttributesData(),
];

$user
->getAttributes()
->willReturn($this->getAttributesData());

$user
->beConstructedWith($data);
$this
->beConstructedWith($user);

$this
->getAttributes()
->shouldReturn($this->getAttributesData());
Expand All @@ -144,30 +153,115 @@ public function it_can_get_the_attributes_only(CasUserInterface $user)
public function it_is_initializable()
{
$this->shouldHaveType(EuLoginUser::class);

$this
->getPassword()
->shouldBeNull();

$this
->getPgt()
->shouldReturn('proxyGrantingTicket');

$this
->getSalt()
->shouldBeNull();

$this
->getAttribute('user')
->shouldReturn('username');

$this
->getUser()
->shouldReturn('username');

$this
->get('foo', 'bar')
->shouldReturn('bar');

$this
->eraseCredentials()
->shouldBeNull();
}

public function let(CasUserInterface $user)
{
$data = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy1',
],
'attributes' => $this->getAttributesData(),
];
$body = <<<'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
<cas:foo>bar</cas:foo>
<cas:proxies>
<cas:proxy>foo</cas:proxy>
</cas:proxies>
<cas:proxyGrantingTicket>
proxyGrantingTicket
</cas:proxyGrantingTicket>
<cas:attributes>
<cas:authenticationFactors>
<cas:moniker number="1">
ecphp@ec.europa.eu
</cas:moniker>
</cas:authenticationFactors>
<cas:assuranceLevel>40</cas:assuranceLevel>
<cas:groups number="10">
<cas:group>group1</cas:group>
<cas:group>group2</cas:group>
</cas:groups>
<cas:extendedAttributes>
<cas:extendedAttribute name="http://stork.eu/motherInLawDogName">
<cas:attributeValue>rex</cas:attributeValue>
<cas:attributeValue>snoopy</cas:attributeValue>
</cas:extendedAttribute>
</cas:extendedAttributes>
</cas:attributes>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;

$response = new Response(200, ['Content-Type' => 'application/json'], $body);
$data = (new Introspector())->parse($response)['serviceResponse']['authenticationSuccess'];

$user
->beConstructedWith($data);

$user
->getAttribute('extendedAttributes', [])
->willReturn([
'extendedAttribute' => [
'attributeValue' => [
'value1',
'value2',
],
'@attributes' => [
'name' => 'attr1',
],
],
]);

$user
->get('foo', 'bar')
->willReturn('bar');

$user
->getUsername()
->willReturn('username');

$user
->getAttribute('user', null)
->willReturn('username');

$user
->getPgt()
->willReturn('proxyGrantingTicket');

$user
->getAttribute('assuranceLevel')
->willReturn('assuranceLevel');
->willReturn($data['attributes']['assuranceLevel']);

$user
->getAttribute('authenticationFactors', [])
->willReturn([
'foobar',
'ecphp@ec.europa.eu',
]);

$user
Expand Down Expand Up @@ -199,9 +293,15 @@ public function let(CasUserInterface $user)
->willReturn('firstName');

$user
->getAttribute('groups', [])
->getAttribute('groups', ['group' => []])
->willReturn([
'foo',
'group' => [
'group1',
'group2',
],
'@attributes' => [
'number' => 2,
],
]);

$user
Expand Down Expand Up @@ -246,6 +346,10 @@ public function let(CasUserInterface $user)
->getAttribute('uid')
->willReturn('uid');

$user
->getAttributes()
->willReturn($this->getAttributesData());

$this
->beConstructedWith($user);
}
Expand All @@ -257,24 +361,31 @@ private function getAttributesData(): array
'email' => 'email',
'employeeNumber' => 'employeeNumber',
'employeeType' => 'employeeType',
'extendedAttributes' => [
'attr1' => [
'value1',
'value2',
],
],
'firstName' => 'firstName',
'lastName' => 'lastName',
'domain' => 'domain',
'domainUsername' => 'domainUsername',
'telephoneNumber' => 'telephoneNumber',
'locale' => 'locale',
'assuranceLevel' => 'assuranceLevel',
'assuranceLevel' => '40',
'uid' => 'uid',
'orgId' => 'orgId',
'teleworkingPriority' => 'teleworkingPriority',
'groups' => [
'foo',
'group1',
'group2',
],
'strengths' => [
'bar',
],
'authenticationFactors' => [
'foobar',
'ecphp@ec.europa.eu',
],
'loginDate' => 'loginDate',
'sso' => 'sso',
Expand Down

0 comments on commit 65f3257

Please sign in to comment.