Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 26, 2022
1 parent 2ed9a41 commit 0dc18e9
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 72 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ grumphp.yml
phpspec.yml
/build/
.php_cs.cache
.envrc
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2020-2022, European Union.
Copyright (c) 2019-2022, European Union.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
"sso"
],
"require": {
"php": ">= 7.4",
"php": ">= 8.1",
"ext-json": "*",
"ext-simplexml": "*",
"ecphp/cas-bundle": "dev-master",
"ecphp/ecas": "^2",
"symfony/framework-bundle": "^5.4 || ^6"
"ecphp/cas-bundle": "dev-refactor/cas-lib-v2",
"ecphp/ecas": "^3",
"symfony/framework-bundle": "^6.1"
},
"require-dev": {
"ext-pcov": "*",
"ecphp/php-conventions": "^1",
"friends-of-phpspec/phpspec-code-coverage": "^6",
"infection/infection": "^0.24.0",
"infection/phpspec-adapter": "^0.1.2",
"infection/infection": "^0.26.0",
"infection/phpspec-adapter": "^0.2",
"nyholm/psr7": "^1.5",
"phpspec/phpspec": "^7",
"symfony/http-client": "^5.4 || ^6",
"symfony/security-core": "^5.4 || ^6"
"symfony/http-client": "^6.1",
"symfony/security-core": "^6.1"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
9 changes: 6 additions & 3 deletions grumphp.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ imports:
- { resource: vendor/ecphp/php-conventions/config/php73/grumphp.yml }

parameters:
tasks.license.date_from: 2020
tasks.phpstan.configuration: phpstan.neon
extra_tasks:
phpspec:
verbose: true
clover_coverage:
clover_file: build/logs/clover.xml
level: 50
infection:
threads: 10
test_framework: phpspec
configuration: infection.json.dist
min_msi: 10
min_covered_msi: 10
min_msi: 50
min_covered_msi: 90
8 changes: 5 additions & 3 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"timeout": 10,
"timeout": 30,
"source": {
"directories": [
"src"
]
},
"logs": {
"github": true,
"text": "build/infection.log",
"summary": "build/summary.log",
"debug": "build/debug.log",
"perMutator": "build/per-mutator.md",
"badge": {
"branch": "master"
"html": "build/report.html",
"stryker": {
"report": "master"
}
},
"testFramework":"phpspec"
Expand Down
5 changes: 5 additions & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ extensions:
- clover
- php
- text
- html
output:
html: build/coverage
clover: build/logs/clover.xml
php: build/coverage.php
whitelist:
- src
blacklist:
- src/Resources
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@

namespace spec\EcPhp\EuLoginBundle\Security\Core\User;

use EcPhp\CasBundle\Cas\SymfonyCasResponseBuilder;
use EcPhp\CasBundle\Security\Core\User\CasUserProvider;
use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\Ecas\Introspection\EcasIntrospector;
use EcPhp\CasLib\Response\CasResponseBuilder;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory as FactoryServiceValidateFactory;
use EcPhp\Ecas\Response\Factory\ServiceValidateFactory;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUser;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserInterface;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserProvider;
use Nyholm\Psr7\Response;
use loophp\psr17\Psr17;
use Nyholm\Psr7\Factory\Psr17Factory;
use PhpSpec\ObjectBehavior;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\InMemoryUser;

Expand All @@ -38,7 +46,7 @@ public function it_can_check_if_the_user_class_is_supported()
public function it_can_load_a_user_from_a_response(): void
{
// TestBody1
$response = new Response(200, ['content-type' => 'application/xml'], $this->getTestBody1());
$response = new Response($this->getTestBody1(), 200, ['content-type' => 'application/xml']);

$user = $this->loadUserByResponse($response);

Expand Down Expand Up @@ -143,7 +151,7 @@ public function it_can_load_a_user_from_a_response(): void
]);

// TestBody2
$response = new Response(200, ['content-type' => 'application/xml'], $this->getTestBody2());
$response = new Response($this->getTestBody2(), 200, ['content-type' => 'application/xml']);

$user = $this->loadUserByResponse($response);

Expand Down Expand Up @@ -256,8 +264,29 @@ public function it_is_initializable()

public function let()
{
$psr17Factory = new Psr17Factory();

$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);

$casResponseBuilder = new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory(new FactoryServiceValidateFactory(), $psr17)
);

$psrHttpFactory = new PsrHttpFactory(
$psr17Factory,
$psr17Factory,
$psr17Factory,
$psr17Factory
);

$this
->beConstructedWith(new CasUserProvider(new EcasIntrospector(new Introspector())));
->beConstructedWith(
new CasUserProvider($casResponseBuilder, $psrHttpFactory),
new SymfonyCasResponseBuilder($casResponseBuilder, $psrHttpFactory)
);
}

private function getTestBody1()
Expand Down
91 changes: 63 additions & 28 deletions spec/EcPhp/EuLoginBundle/Security/Core/User/EuLoginUserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,49 @@

namespace spec\EcPhp\EuLoginBundle\Security\Core\User;

use EcPhp\CasBundle\Cas\SymfonyCasResponseBuilder;
use EcPhp\CasBundle\Security\Core\User\CasUser;
use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\CasLib\Response\CasResponseBuilder;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUser;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\Factory\Psr17Factory;
use PhpSpec\ObjectBehavior;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\HttpFoundation\Response;

class EuLoginUserSpec extends ObjectBehavior
{
public function it_can_get_groups_when_no_groups_are_available()
{
$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="0"/>
<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'];
$data = [
'user' => 'username',
'foo' => 'bar',
'proxies' => [
'proxy' => 'foo',
],
'attributes' => [
'groups' => [
'@value' => '',
'@attributes' => [
'number' => '0',
],
],
'extendedAttributes' => [
'extendedAttribute' => [
'attributeValue' => [
0 => 'rex',
1 => 'snoopy',
],
'@attributes' => [
'name' => 'http://stork.eu/motherInLawDogName',
],
],
],
],
];

$casUser = new CasUser($data);

Expand All @@ -53,6 +63,9 @@ public function it_can_get_groups_when_no_groups_are_available()
$this
->getGroups()
->shouldReturn([]);
$this
->getUserIdentifier()
->shouldReturn('username');
}

public function it_can_get_specific_attribute()
Expand Down Expand Up @@ -295,10 +308,32 @@ public function let()
</cas:serviceResponse>
EOF;

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

$psr17Factory = new Psr17Factory();

$casResponseBuilder = new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
);

$psrHttpFactory = new PsrHttpFactory(
$psr17Factory,
$psr17Factory,
$psr17Factory,
$psr17Factory
);

$symfonyCasResponseBuilder = new SymfonyCasResponseBuilder(
$casResponseBuilder,
$psrHttpFactory
);

$responseArray = $symfonyCasResponseBuilder->fromResponse($response)->toArray();

$this->beConstructedWith(new CasUser($data));
$this->beConstructedWith(new CasUser($responseArray['serviceResponse']['authenticationSuccess']));
}

private function getAttributesData(): array
Expand Down
26 changes: 12 additions & 14 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use EcPhp\CasBundle\Cas\SymfonyCasInterface;
use EcPhp\CasBundle\Security\Core\User\CasUserProviderInterface;
use EcPhp\CasLib\Contract\Configuration\PropertiesInterface;
use EcPhp\Ecas\Ecas;
use EcPhp\Ecas\EcasProperties;
use EcPhp\Ecas\Introspection\EcasIntrospector;
use EcPhp\EuLoginBundle\Security\Core\User\EuLoginUserProvider;

return static function (ContainerConfigurator $container) {
Expand All @@ -25,21 +27,17 @@
->autowire(true);

$services
->set('ecas.introspector', EcasIntrospector::class)
->decorate('cas.introspector')
->arg('$introspector', service('ecas.introspector.inner'));
->set(EuLoginUserProvider::class)
->decorate(CasUserProviderInterface::class)
->arg('$casUserProvider', service('.inner'));

$services
->set('eulogin.userprovider', EuLoginUserProvider::class)
->arg('$casUserProvider', service('cas.userprovider'));
->set(EcasProperties::class)
->decorate(PropertiesInterface::class)
->arg('$casProperties', service('.inner'));

$services
->set('ecas.configuration', EcasProperties::class)
->decorate('cas.configuration')
->arg('$casProperties', service('ecas.configuration.inner'));

$services
->set('ecas', Ecas::class)
->decorate('cas')
->arg('$cas', service('ecas.inner'));
->set(Ecas::class)
->decorate(SymfonyCasInterface::class)
->arg('$cas', service('.inner'));
};
14 changes: 6 additions & 8 deletions src/Security/Core/User/EuLoginUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@

use EcPhp\CasBundle\Security\Core\User\CasUserInterface;
use EcPhp\CasBundle\Security\Core\User\CasUserProviderInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;

use function get_class;

final class EuLoginUserProvider implements CasUserProviderInterface
{
/**
* @var CasUserProviderInterface
*/
private $casUserProvider;
private CasUserProviderInterface $casUserProvider;

public function __construct(CasUserProviderInterface $casUserProvider)
{
public function __construct(
CasUserProviderInterface $casUserProvider
) {
$this->casUserProvider = $casUserProvider;
}

Expand All @@ -36,7 +34,7 @@ public function loadUserByIdentifier(string $identifier): UserInterface
throw new UnsupportedUserException('Unsupported operation.');
}

public function loadUserByResponse(ResponseInterface $response): CasUserInterface
public function loadUserByResponse(Response $response): CasUserInterface
{
return new EuLoginUser($this->casUserProvider->loadUserByResponse($response));
}
Expand Down

0 comments on commit 0dc18e9

Please sign in to comment.