using redirectUri instead of client as constructor argument
MODIFY
src/Grant/AbstractGrant.php
src/Grant/AbstractGrant.php
@@ -263,7 +263,7 @@ abstract class AbstractGrant implements GrantTypeInterface
ClientEntityInterface $client,
ServerRequestInterface $request
) {
- $validator = new RedirectUriValidator($client);
+ $validator = new RedirectUriValidator($client->getRedirectUri());
if (!$validator->validateRedirectUri($redirectUri)) {
$this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
throw OAuthServerException::invalidClient($request);

MODIFY
src/RedirectUriValidators/RedirectUriValidator.php
src/RedirectUriValidators/RedirectUriValidator.php
@@ -14,18 +14,24 @@ use League\OAuth2\Server\Entities\ClientEntityInterface;
class RedirectUriValidator implements RedirectUriValidatorInterface
{
/**
- * @var ClientEntityInterface
+ * @var array
*/
- private $client;
+ private $allowedRedirectUris;
/**
- * New validator instance for the given client
+ * New validator instance for the given uri
*
- * @param ClientEntityInterface $client
+ * @param string|array $allowedRedirectUris
*/
- public function __construct(ClientEntityInterface $client)
+ public function __construct($allowedRedirectUri)
{
- $this->client = $client;
+ if (\is_string($allowedRedirectUri)) {
+ $this->allowedRedirectUris = [ $allowedRedirectUri ];
+ } elseif (\is_array($allowedRedirectUri)) {
+ $this->allowedRedirectUris = $allowedRedirectUri;
+ } else {
+ $this->allowedRedirectUris = [];
+ }
}
/**
@@ -69,7 +75,7 @@ class RedirectUriValidator implements RedirectUriValidatorInterface
*/
private function matchExactUri($redirectUri)
{
- return \in_array($redirectUri, $this->getClientRedirectUris(), true);
+ return \in_array($redirectUri, $this->allowedRedirectUris, true);
}
/**
@@ -81,8 +87,8 @@ class RedirectUriValidator implements RedirectUriValidatorInterface
*/
private function allowDifferentPort(array $parsedUrl)
{
- foreach ($this->getClientRedirectUris() as $clientRedirectUri) {
- if ($parsedUrl == $this->parseUrl($clientRedirectUri)) {
+ foreach ($this->allowedRedirectUris as $redirectUri) {
+ if ($parsedUrl == $this->parseUrl($redirectUri)) {
return true;
}
}
@@ -104,21 +110,4 @@ class RedirectUriValidator implements RedirectUriValidatorInterface
return $parsedUrl;
}
-
- /**
- * Retrieve allowed client redirect uris
- *
- * @return array
- */
- private function getClientRedirectUris()
- {
- $clientRedirectUri = $this->client->getRedirectUri();
- if (\is_string($clientRedirectUri)) {
- return [$clientRedirectUri];
- } elseif (\is_array($clientRedirectUri)) {
- return $clientRedirectUri;
- }
-
- return [];
- }
}

MODIFY
tests/RedirectUriValidators/RedirectUriValidatorTest.php
tests/RedirectUriValidators/RedirectUriValidatorTest.php
@@ -10,79 +10,66 @@ class RedirectUriValidatorTest extends TestCase
{
public function testInvalidNonLoopbackUri()
{
- $client = new ClientEntity();
- $client->setRedirectUri([
+ $validator = new RedirectUriValidator([
'https://example.com:8443/endpoint',
'https://example.com/different/endpoint',
]);
- $redirectUri = 'https://example.com/endpoint';
- $validator = new RedirectUriValidator($client);
+ $invalidRedirectUri = 'https://example.com/endpoint';
$this->assertFalse(
- $validator->validateRedirectUri($redirectUri),
+ $validator->validateRedirectUri($invalidRedirectUri),
'Non loopback URI must match in every part'
);
}
public function testValidNonLoopbackUri()
{
- $client = new ClientEntity();
- $client->setRedirectUri([
+ $validator = new RedirectUriValidator([
'https://example.com:8443/endpoint',
'https://example.com/different/endpoint',
]);
- $redirectUri = 'https://example.com:8443/endpoint';
- $validator = new RedirectUriValidator($client);
+ $validRedirectUri = 'https://example.com:8443/endpoint';
$this->assertTrue(
- $validator->validateRedirectUri($redirectUri),
+ $validator->validateRedirectUri($validRedirectUri),
'Redirect URI must be valid when matching in every part'
);
}
public function testInvalidLoopbackUri()
{
- $client = new ClientEntity();
- $client->setRedirectUri('http://127.0.0.1:8443/endpoint');
+ $validator = new RedirectUriValidator('http://127.0.0.1:8443/endpoint');
- $redirectUri = 'http://127.0.0.1:8443/different/endpoint';
-
- $validator = new RedirectUriValidator($client);
+ $invalidRedirectUri = 'http://127.0.0.1:8443/different/endpoint';
$this->assertFalse(
- $validator->validateRedirectUri($redirectUri),
+ $validator->validateRedirectUri($invalidRedirectUri),
'Valid loopback redirect URI can change only the port number'
);
}
public function testValidLoopbackUri()
{
- $client = new ClientEntity();
- $client->setRedirectUri('http://127.0.0.1:8443/endpoint');
-
- $redirectUri = 'http://127.0.0.1:8080/endpoint';
+ $validator = new RedirectUriValidator('http://127.0.0.1:8443/endpoint');
- $validator = new RedirectUriValidator($client);
+ $validRedirectUri = 'http://127.0.0.1:8080/endpoint';
$this->assertTrue(
- $validator->validateRedirectUri($redirectUri),
+ $validator->validateRedirectUri($validRedirectUri),
'Loopback redirect URI can change the port number'
);
}
public function testValidIpv6LoopbackUri()
{
- $client = new ClientEntity();
- $client->setRedirectUri('http://[::1]:8443/endpoint');
-
- $redirectUri = 'http://[::1]:8080/endpoint';
+ $validator = new RedirectUriValidator('http://[::1]:8443/endpoint');
- $validator = new RedirectUriValidator($client);
+ $validRedirectUri = 'http://[::1]:8080/endpoint';
$this->assertTrue(
- $validator->validateRedirectUri($redirectUri),
+ $validator->validateRedirectUri($validRedirectUri),
'Loopback redirect URI can change the port number'
);
}

using redirectUri instead of client as constructor argument