Skip to content

New instrospect and detect method #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "BSD-3-Clause",
"require": {
"php": ">= 7.1.3",
"ecphp/cas-lib": "1.0.*"
"ecphp/cas-lib": "1.1.*"
},
"require-dev": {
"drupol/php-conventions": "^1.8.6",
Expand Down
5 changes: 4 additions & 1 deletion spec/EcPhp/Ecas/EcasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace spec\EcPhp\Ecas;

use EcPhp\CasLib\Cas;
use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\Ecas\Introspection\EcasIntrospector;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7Server\ServerRequestCreator;
Expand Down Expand Up @@ -134,7 +136,8 @@ public function let()
$psr17Factory,
$psr17Factory,
new ArrayAdapter(),
new NullLogger()
new NullLogger(),
new EcasIntrospector(new Introspector())
);

$this->beConstructedWith($cas, $psr17Factory);
Expand Down
6 changes: 6 additions & 0 deletions src/Ecas.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use EcPhp\CasLib\CasInterface;
use EcPhp\CasLib\Configuration\PropertiesInterface;
use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand Down Expand Up @@ -41,6 +42,11 @@ public function authenticate(array $parameters = []): ?array
return $this->cas->authenticate($parameters);
}

public function detect(ResponseInterface $response): IntrospectionInterface
{
return $this->cas->detect($response);
}

/**
* {@inheritdoc}
*/
Expand Down
79 changes: 79 additions & 0 deletions src/Introspection/EcasIntrospector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace EcPhp\Ecas\Introspection;

use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface;
use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface;
use EcPhp\CasLib\Introspection\ServiceValidate;
use Psr\Http\Message\ResponseInterface;

use function in_array;

final class EcasIntrospector implements IntrospectorInterface
{
/**
* @var \EcPhp\CasLib\Introspection\Contract\IntrospectorInterface
*/
private $introspector;

public function __construct(IntrospectorInterface $introspector)
{
$this->introspector = $introspector;
}

public function detect(ResponseInterface $response): IntrospectionInterface
{
$introspect = $this->introspector->detect($response);

if ($introspect instanceof ServiceValidate) {
return $introspect
->withParsedResponse(
$this->normalizeUserData($introspect->getParsedResponse())
);
}

return $introspect;
}

/**
* {@inheritdoc}
*/
public function parse(ResponseInterface $response, string $format = 'XML'): array
{
return $this->introspector->parse($response, $format);
}

/**
* Normalize user data from EU Login to standard CAS user data.
*
* @param array<array|string> $data
* The data from EU Login
*
* @return array<array|string>
* The normalized data.
*/
private function normalizeUserData(array $data): array
{
$data = $data['serviceResponse']['authenticationSuccess'];
$data += ['attributes' => []];

$rootAttributes = ['user', 'proxyGrantingTicket', 'proxies', 'attributes'];

foreach ($data as $key => $property) {
if (in_array($key, $rootAttributes, true)) {
continue;
}

$data['attributes'] += [$key => $property];
unset($data[$key]);
}

return [
'serviceResponse' => [
'authenticationSuccess' => $data,
],
];
}
}