Skip to content

Commit

Permalink
fix: make sure handlers parameters are correctly handled (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Feb 3, 2023
1 parent f958de1 commit 599f407
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 32 deletions.
6 changes: 3 additions & 3 deletions spec/EcPhp/CasLib/CasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public function it_can_logout()
->withServerRequest($request)
->logout()
->getHeader('Location')
->shouldReturn(['http://local/cas/logout']);
->shouldReturn(['http://local/cas/logout?service=http%3A%2F%2Flocal%2F']);

$parameters = [
'custom' => 'bar',
Expand All @@ -860,7 +860,7 @@ public function it_can_logout()
->withServerRequest($request)
->logout($parameters)
->getHeader('Location')
->shouldReturn(['http://local/cas/logout?custom=bar']);
->shouldReturn(['http://local/cas/logout?custom=bar&service=http%3A%2F%2Flocal%2F']);

$parameters = [
'custom' => 'bar',
Expand All @@ -883,7 +883,7 @@ public function it_can_logout()
->withServerRequest($request)
->logout($parameters)
->getHeader('Location')
->shouldReturn(['http://local/cas/logout?custom=bar']);
->shouldReturn(['http://local/cas/logout?custom=bar&service=http%3A%2F%2Flocal%2F']);

$parameters = [
'custom' => 'bar',
Expand Down
25 changes: 24 additions & 1 deletion src/Handler/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ public function __construct(
$this->logger = $logger;
}

/**
* This function will aggregate all the input arrays into a single array.
*
* The rule of concatenation is that the previous array will have precedence
* over the current array.
*
* Therefore: buildParameters([a=>1], [a=>2,b=>3]) will return [a=>1, b=>3]
*
* @param array<array-key, mixed> ...$parameters
*
* @return array<array-key, mixed>
*/
protected function buildParameters(array ...$parameters): array
{
return $this->formatProtocolParameters(
array_reduce(
$parameters,
static fn (array $carry, array $item): array => $carry + $item,
[]
)
);
}

/**
* @param mixed[]|string[]|UriInterface[] $query
*/
Expand Down Expand Up @@ -158,7 +181,7 @@ protected function getLogger(): LoggerInterface

protected function getParameters(): array
{
return $this->parameters + ($this->getProtocolProperties()['default_parameters'] ?? []);
return $this->parameters;
}

protected function getProperties(): PropertiesInterface
Expand Down
15 changes: 7 additions & 8 deletions src/Redirect/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ final class Login extends Redirect implements RedirectInterface
{
public function handle(): ?ResponseInterface
{
$parameters = $this->formatProtocolParameters($this->getParameters());
$parameters = $this->buildParameters(
$this->getParameters(),
$this->getProtocolProperties()['default_parameters'] ?? [],
['service' => (string) $this->getServerRequest()->getUri()],
);
$parameters = $this->formatProtocolParameters($parameters);
$validatedParameters = $this->validate($parameters);

if (null === $validatedParameters) {
Expand Down Expand Up @@ -58,13 +63,7 @@ protected function formatProtocolParameters(array $parameters): array

protected function getProtocolProperties(): array
{
$protocolProperties = $this->getProperties()['protocol']['login'] ?? [];

$protocolProperties['default_parameters'] += [
'service' => (string) $this->getServerRequest()->getUri(),
];

return $protocolProperties;
return $this->getProperties()['protocol']['login'] ?? [];
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Redirect/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ protected function getProtocolProperties(): array
private function getUri(): UriInterface
{
$serverRequest = $this->getServerRequest()->getUri();
$parameters = $this->formatProtocolParameters($this->getParameters());

$parameters = $this->buildParameters(
$this->getParameters(),
$this->getProtocolProperties()['default_parameters'] ?? [],
['service' => (string) $serverRequest],
);

return $this->buildUri($serverRequest, 'logout', $parameters);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Service/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ protected function getProtocolProperties(): array

protected function getUri(): UriInterface
{
$parameters = $this->buildParameters(
$this->getParameters(),
$this->getProtocolProperties()['default_parameters'] ?? []
);

return $this->buildUri(
$this->getServerRequest()->getUri(),
'proxy',
$this->formatProtocolParameters($this->getParameters())
$parameters
);
}
}
20 changes: 11 additions & 9 deletions src/Service/ProxyValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ final class ProxyValidate extends Service implements ServiceInterface
{
protected function getProtocolProperties(): array
{
$protocolProperties = $this->getProperties()['protocol']['proxyValidate'] ?? [];

$protocolProperties['default_parameters'] += [
'service' => (string) $this->getServerRequest()->getUri(),
'ticket' => Uri::getParam($this->getServerRequest()->getUri(), 'ticket'),
];

return $protocolProperties;
return $this->getProperties()['protocol']['proxyValidate'] ?? [];
}

protected function getUri(): UriInterface
{
$parameters = $this->buildParameters(
$this->getParameters(),
[
'service' => (string) $this->getServerRequest()->getUri(),
'ticket' => Uri::getParam($this->getServerRequest()->getUri(), 'ticket'),
],
$this->getProtocolProperties()['default_parameters'] ?? []
);

return $this->buildUri(
$this->getServerRequest()->getUri(),
'proxyValidate',
$this->formatProtocolParameters($this->getParameters())
$parameters
);
}
}
20 changes: 11 additions & 9 deletions src/Service/ServiceValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ final class ServiceValidate extends Service implements ServiceInterface
{
protected function getProtocolProperties(): array
{
$protocolProperties = $this->getProperties()['protocol']['serviceValidate'] ?? [];

$protocolProperties['default_parameters'] += [
'service' => (string) $this->getServerRequest()->getUri(),
'ticket' => Uri::getParam($this->getServerRequest()->getUri(), 'ticket'),
];

return $protocolProperties;
return $this->getProperties()['protocol']['serviceValidate'] ?? [];
}

protected function getUri(): UriInterface
{
$parameters = $this->buildParameters(
$this->getParameters(),
[
'service' => (string) $this->getServerRequest()->getUri(),
'ticket' => Uri::getParam($this->getServerRequest()->getUri(), 'ticket'),
],
$this->getProtocolProperties()['default_parameters'] ?? []
);

return $this->buildUri(
$this->getServerRequest()->getUri(),
'serviceValidate',
$this->formatProtocolParameters($this->getParameters())
$parameters
);
}
}

0 comments on commit 599f407

Please sign in to comment.