diff --git a/src/Container/ResourceServerMiddlewareFactory.php b/src/Container/ResourceServerMiddlewareFactory.php index 2c265b6..de7ae2c 100644 --- a/src/Container/ResourceServerMiddlewareFactory.php +++ b/src/Container/ResourceServerMiddlewareFactory.php @@ -36,8 +36,9 @@ public function __invoke(ContainerInterface $container): ResourceServerMiddlewar { /** @var ResourceServerInterface $resourceServer */ $resourceServer = $container->get(ResourceServerInterface::class); + /** @var ServerOptions $serverOptions */ $serverOptions = $container->get(ServerOptions::class); - return new ResourceServerMiddleware($resourceServer, $serverOptions); + return new ResourceServerMiddleware($resourceServer, $serverOptions->getTokenRequestAttribute()); } } diff --git a/src/Middleware/ResourceServerMiddleware.php b/src/Middleware/ResourceServerMiddleware.php index a458536..23b81a8 100644 --- a/src/Middleware/ResourceServerMiddleware.php +++ b/src/Middleware/ResourceServerMiddleware.php @@ -1,6 +1,6 @@ resourceServer = $resourceServer; - $this->serverOptions = $serverOptions; + $this->resourceServer = $resourceServer; + $this->tokenRequestAttribute = $tokenRequestAttribute; } public function __invoke( @@ -68,10 +67,11 @@ public function __invoke( } catch (InvalidAccessTokenException $exception) { // If we're here, this means that there was an access token, but it's either expired or invalid. If // that's the case we must immediately return - return new JsonResponse(['error' => $exception->getCode(), 'error_description' => $exception->getMessage()], 401); + return new JsonResponse(['error' => $exception->getCode(), 'error_description' => $exception->getMessage()], + 401); } // Otherwise, if we actually have a token and set it as part of the request attribute for next step - return $next($request->withAttribute($this->serverOptions->getTokenRequestAttribute(), $token), $response); + return $next($request->withAttribute($this->tokenRequestAttribute, $token), $response); } } diff --git a/tests/src/Middleware/ResourceServerMiddlewareTest.php b/tests/src/Middleware/ResourceServerMiddlewareTest.php index 1492bbe..e6d8eed 100644 --- a/tests/src/Middleware/ResourceServerMiddlewareTest.php +++ b/tests/src/Middleware/ResourceServerMiddlewareTest.php @@ -27,7 +27,6 @@ use ZfrOAuth2\Server\Exception\InvalidAccessTokenException; use ZfrOAuth2\Server\Middleware\ResourceServerMiddleware; use ZfrOAuth2\Server\Model\AccessToken; -use ZfrOAuth2\Server\Options\ServerOptions; use ZfrOAuth2\Server\ResourceServer; /** @@ -40,7 +39,7 @@ class ResourceServerMiddlewareTest extends TestCase public function testWillGetAccessTokenWithAccessTokenAsResult() { $resourceServer = $this->createMock(ResourceServer::class); - $middleware = new ResourceServerMiddleware($resourceServer, ServerOptions::fromArray()); + $middleware = new ResourceServerMiddleware($resourceServer, 'oauth_token'); $accessToken = $this->createMock(AccessToken::class); $request = $this->createMock(RequestInterface::class); $response = $this->createMock(ResponseInterface::class); @@ -66,7 +65,7 @@ public function testWillGetAccessTokenWithAccessTokenAsResult() public function testWillGetAccessTokenWithNullAsResult() { $resourceServer = $this->createMock(ResourceServer::class); - $middleware = new ResourceServerMiddleware($resourceServer, ServerOptions::fromArray()); + $middleware = new ResourceServerMiddleware($resourceServer, 'oauth_token'); $accessToken = null; $request = $this->createMock(RequestInterface::class); $response = $this->createMock(ResponseInterface::class); @@ -92,7 +91,7 @@ public function testWillGetAccessTokenWithNullAsResult() public function testWillCallGetAccessTokenWithException() { $resourceServer = $this->createMock(ResourceServer::class); - $middleware = new ResourceServerMiddleware($resourceServer, ServerOptions::fromArray()); + $middleware = new ResourceServerMiddleware($resourceServer, 'oauth_token'); $request = $this->createMock(RequestInterface::class); $response = $this->createMock(ResponseInterface::class);