Skip to content

Commit

Permalink
do not consumer server options
Browse files Browse the repository at this point in the history
  • Loading branch information
basz committed Mar 10, 2017
1 parent 381852c commit bbbe022
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Container/ResourceServerMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
18 changes: 9 additions & 9 deletions src/Middleware/ResourceServerMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Expand All @@ -25,7 +25,6 @@
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\JsonResponse;
use ZfrOAuth2\Server\Exception\InvalidAccessTokenException;
use ZfrOAuth2\Server\Options\ServerOptions;
use ZfrOAuth2\Server\ResourceServerInterface;

/**
Expand All @@ -45,17 +44,17 @@ class ResourceServerMiddleware
private $resourceServer;

/**
* @var ServerOptions
* @var string
*/
private $serverOptions;
private $tokenRequestAttribute;

/**
* @param ResourceServerInterface $resourceServer
*/
public function __construct(ResourceServerInterface $resourceServer, ServerOptions $serverOptions)
public function __construct(ResourceServerInterface $resourceServer, string $tokenRequestAttribute = 'oauth_token')
{
$this->resourceServer = $resourceServer;
$this->serverOptions = $serverOptions;
$this->resourceServer = $resourceServer;
$this->tokenRequestAttribute = $tokenRequestAttribute;
}

public function __invoke(
Expand All @@ -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);
}
}
7 changes: 3 additions & 4 deletions tests/src/Middleware/ResourceServerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit bbbe022

Please sign in to comment.