diff --git a/spec/EcPhp/CasLib/CasSpec.php b/spec/EcPhp/CasLib/CasSpec.php index ac2c29c..874ca19 100644 --- a/spec/EcPhp/CasLib/CasSpec.php +++ b/spec/EcPhp/CasLib/CasSpec.php @@ -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', @@ -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', @@ -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', diff --git a/src/Handler/Handler.php b/src/Handler/Handler.php index 703ed3f..50991d5 100644 --- a/src/Handler/Handler.php +++ b/src/Handler/Handler.php @@ -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 ...$parameters + * + * @return array + */ + 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 */ @@ -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 diff --git a/src/Redirect/Login.php b/src/Redirect/Login.php index 6f47d03..4f26576 100644 --- a/src/Redirect/Login.php +++ b/src/Redirect/Login.php @@ -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) { @@ -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'] ?? []; } /** diff --git a/src/Redirect/Logout.php b/src/Redirect/Logout.php index 2314bfe..c73d84a 100644 --- a/src/Redirect/Logout.php +++ b/src/Redirect/Logout.php @@ -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); } diff --git a/src/Service/Proxy.php b/src/Service/Proxy.php index ceb7b1a..54b1e81 100644 --- a/src/Service/Proxy.php +++ b/src/Service/Proxy.php @@ -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 ); } } diff --git a/src/Service/ProxyValidate.php b/src/Service/ProxyValidate.php index fb1478b..523b5a6 100644 --- a/src/Service/ProxyValidate.php +++ b/src/Service/ProxyValidate.php @@ -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 ); } } diff --git a/src/Service/ServiceValidate.php b/src/Service/ServiceValidate.php index 33fa863..cbc611f 100644 --- a/src/Service/ServiceValidate.php +++ b/src/Service/ServiceValidate.php @@ -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 ); } }