From 16ae17728b6d83a28f0f2aec0fc4ca0828c15af0 Mon Sep 17 00:00:00 2001 From: Manuel Dimmler Date: Mon, 8 Jul 2019 13:07:02 +0200 Subject: [PATCH 1/8] add PSR15/PSR-17 compliant middleware --- .gitignore | 1 + composer.json | 4 +- .../Psr15AuthorizationServerMiddleware.php | 67 +++++++++++ .../Psr15ResourceServerMiddleware.php | 67 +++++++++++ ...Psr15AuthorizationServerMiddlewareTest.php | 105 ++++++++++++++++ .../Psr15ResourceServerMiddlewareTest.php | 112 ++++++++++++++++++ 6 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 src/Middleware/Psr15AuthorizationServerMiddleware.php create mode 100644 src/Middleware/Psr15ResourceServerMiddleware.php create mode 100644 tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php create mode 100644 tests/Middleware/Psr15ResourceServerMiddlewareTest.php diff --git a/.gitignore b/.gitignore index 897c80b18..0dcf1ef8d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ phpunit.xml examples/public.key examples/private.key build +/nbproject diff --git a/composer.json b/composer.json index cb54606c3..52ee46310 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,9 @@ "league/event": "^2.1", "lcobucci/jwt": "^3.2.2", "psr/http-message": "^1.0.1", - "defuse/php-encryption": "^2.1" + "defuse/php-encryption": "^2.1", + "psr/http-server-middleware": "^1.0", + "psr/http-factory": "^1.0" }, "require-dev": { "phpunit/phpunit": "^6.3 || ^7.0", diff --git a/src/Middleware/Psr15AuthorizationServerMiddleware.php b/src/Middleware/Psr15AuthorizationServerMiddleware.php new file mode 100644 index 000000000..be3bd379b --- /dev/null +++ b/src/Middleware/Psr15AuthorizationServerMiddleware.php @@ -0,0 +1,67 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\Middleware; + +use Exception; +use League\OAuth2\Server\AuthorizationServer; +use League\OAuth2\Server\Exception\OAuthServerException; +use Psr\Http\Message\ResponseFactoryInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class Psr15AuthorizationServerMiddleware implements MiddlewareInterface +{ + + /** + * @var AuthorizationServer + */ + private $server; + + /** + * @var ResponseFactoryInterface + */ + private $responseFactory; + + /** + * @param AuthorizationServer $server + * @param ResponseFactoryInterface $responseFactory + */ + public function __construct(AuthorizationServer $server, ResponseFactoryInterface $responseFactory) + { + $this->server = $server; + $this->responseFactory = $responseFactory; + } + + /** + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + try { + $response = $this->server->respondToAccessTokenRequest($request, $this->responseFactory->createResponse()); + } catch (OAuthServerException $exception) { + return $exception->generateHttpResponse($this->responseFactory->createResponse()); + // @codeCoverageIgnoreStart + } catch (Exception $exception) { + return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500)) + ->generateHttpResponse($this->responseFactory->createResponse()); + // @codeCoverageIgnoreEnd + } + + // Pass the request on to the next responder in the chain + return $handler->handle($request); + } + +} diff --git a/src/Middleware/Psr15ResourceServerMiddleware.php b/src/Middleware/Psr15ResourceServerMiddleware.php new file mode 100644 index 000000000..390a66bde --- /dev/null +++ b/src/Middleware/Psr15ResourceServerMiddleware.php @@ -0,0 +1,67 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * + * @link https://github.com/thephpleague/oauth2-server + */ + +namespace League\OAuth2\Server\Middleware; + +use Exception; +use League\OAuth2\Server\Exception\OAuthServerException; +use League\OAuth2\Server\ResourceServer; +use Psr\Http\Message\ResponseFactoryInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class Psr15ResourceServerMiddleware implements MiddlewareInterface +{ + + /** + * @var ResourceServer + */ + private $server; + + /** + * @var ResponseFactoryInterface + */ + private $responseFactory; + + /** + * @param ResourceServer $server + * @param ResponseFactoryInterface $responseFactory + */ + public function __construct(ResourceServer $server, ResponseFactoryInterface $responseFactory) + { + $this->server = $server; + $this->responseFactory = $responseFactory; + } + + /** + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + try { + $request = $this->server->validateAuthenticatedRequest($request); + } catch (OAuthServerException $exception) { + return $exception->generateHttpResponse($this->responseFactory->createResponse()); + // @codeCoverageIgnoreStart + } catch (Exception $exception) { + return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500)) + ->generateHttpResponse($this->responseFactory->createResponse()); + // @codeCoverageIgnoreEnd + } + + // Pass the request on to the next responder in the chain + return $handler->handle($request); + } + +} diff --git a/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php b/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php new file mode 100644 index 000000000..c55a8dcf3 --- /dev/null +++ b/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php @@ -0,0 +1,105 @@ +getMockBuilder(ClientRepositoryInterface::class)->getMock(); + $clientRepository->method('getClientEntity')->willReturn(new ClientEntity()); + + $scopeEntity = new ScopeEntity; + $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); + $scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity); + $scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0); + + $accessRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); + $accessRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity()); + + $server = new AuthorizationServer( + $clientRepository, + $accessRepositoryMock, + $scopeRepositoryMock, + 'file://' . __DIR__ . '/../Stubs/private.key', + base64_encode(random_bytes(36)), + new StubResponseType() + ); + + $server->setDefaultScope(self::DEFAULT_SCOPE); + $server->enableGrantType(new ClientCredentialsGrant()); + + $_POST['grant_type'] = 'client_credentials'; + $_POST['client_id'] = 'foo'; + $_POST['client_secret'] = 'bar'; + + $request = ServerRequestFactory::fromGlobals(); + + $responseFactoryMock = $this->getMockBuilder(ResponseFactoryInterface::class)->getMock(); + $responseFactoryMock->method('createResponse')->willReturn(new Response()); + $requestHandlerMock = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); + $requestHandlerMock->method('handle')->willReturn(new Response()); + + $middleware = new Psr15AuthorizationServerMiddleware($server, $responseFactoryMock); + $response = $middleware->process( + $request, + $requestHandlerMock + ); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function testOAuthErrorResponse() + { + $clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); + $clientRepository->method('getClientEntity')->willReturn(null); + + $server = new AuthorizationServer( + $clientRepository, + $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(), + $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(), + 'file://' . __DIR__ . '/../Stubs/private.key', + base64_encode(random_bytes(36)), + new StubResponseType() + ); + + $server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M')); + + $_POST['grant_type'] = 'client_credentials'; + $_POST['client_id'] = 'foo'; + $_POST['client_secret'] = 'bar'; + + $request = ServerRequestFactory::fromGlobals(); + + $responseFactoryMock = $this->getMockBuilder(ResponseFactoryInterface::class)->getMock(); + $responseFactoryMock->method('createResponse')->willReturn(new Response()); + $requestHandlerMock = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); + $requestHandlerMock->method('handle')->willReturn(new Response()); + + $middleware = new Psr15AuthorizationServerMiddleware($server, $responseFactoryMock); + + $response = $middleware->process( + $request, + $requestHandlerMock + ); + + $this->assertEquals(401, $response->getStatusCode()); + } +} diff --git a/tests/Middleware/Psr15ResourceServerMiddlewareTest.php b/tests/Middleware/Psr15ResourceServerMiddlewareTest.php new file mode 100644 index 000000000..4d8b5a709 --- /dev/null +++ b/tests/Middleware/Psr15ResourceServerMiddlewareTest.php @@ -0,0 +1,112 @@ +getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(), + 'file://' . __DIR__ . '/../Stubs/public.key' + ); + + $client = new ClientEntity(); + $client->setIdentifier('clientName'); + + $accessToken = new AccessTokenEntity(); + $accessToken->setIdentifier('test'); + $accessToken->setUserIdentifier(123); + $accessToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H'))); + $accessToken->setClient($client); + + $token = $accessToken->convertToJWT(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + + $request = new ServerRequest(); + $request = $request->withHeader('authorization', sprintf('Bearer %s', $token)); + + $responseFactoryMock = $this->getMockBuilder(ResponseFactoryInterface::class)->getMock(); + $responseFactoryMock->method('createResponse')->willReturn(new Response()); + $requestHandlerMock = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); + $requestHandlerMock->method('handle')->willReturn(new Response()); + + $middleware = new Psr15ResourceServerMiddleware($server, $responseFactoryMock); + $response = $middleware->process( + $request, + $requestHandlerMock + ); + + $this->assertEquals(200, $response->getStatusCode()); + } + + public function testValidResponseExpiredToken() + { + $server = new ResourceServer( + $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(), + 'file://' . __DIR__ . '/../Stubs/public.key' + ); + + $client = new ClientEntity(); + $client->setIdentifier('clientName'); + + $accessToken = new AccessTokenEntity(); + $accessToken->setIdentifier('test'); + $accessToken->setUserIdentifier(123); + $accessToken->setExpiryDateTime((new \DateTime())->sub(new \DateInterval('PT1H'))); + $accessToken->setClient($client); + + $token = $accessToken->convertToJWT(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + + $request = new ServerRequest(); + $request = $request->withHeader('authorization', sprintf('Bearer %s', $token)); + + $responseFactoryMock = $this->getMockBuilder(ResponseFactoryInterface::class)->getMock(); + $responseFactoryMock->method('createResponse')->willReturn(new Response()); + $requestHandlerMock = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); + $requestHandlerMock->method('handle')->willReturn(new Response()); + + $middleware = new Psr15ResourceServerMiddleware($server, $responseFactoryMock); + $response = $middleware->process( + $request, + $requestHandlerMock + ); + + $this->assertEquals(401, $response->getStatusCode()); + } + + public function testErrorResponse() + { + $server = new ResourceServer( + $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(), + 'file://' . __DIR__ . '/../Stubs/public.key' + ); + + $request = new ServerRequest(); + $request = $request->withHeader('authorization', ''); + + $responseFactoryMock = $this->getMockBuilder(ResponseFactoryInterface::class)->getMock(); + $responseFactoryMock->method('createResponse')->willReturn(new Response()); + $requestHandlerMock = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); + $requestHandlerMock->method('handle')->willReturn(new Response()); + + $middleware = new Psr15ResourceServerMiddleware($server, $responseFactoryMock); + $response = $middleware->process( + $request, + $requestHandlerMock + ); + + $this->assertEquals(401, $response->getStatusCode()); + } +} From d6731f941a699f83d00697a5b7139c9e0f587af8 Mon Sep 17 00:00:00 2001 From: Manuel Dimmler Date: Mon, 8 Jul 2019 13:24:40 +0200 Subject: [PATCH 2/8] fix styleci issues --- src/Middleware/Psr15AuthorizationServerMiddleware.php | 6 ++---- src/Middleware/Psr15ResourceServerMiddleware.php | 6 ++---- tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Middleware/Psr15AuthorizationServerMiddleware.php b/src/Middleware/Psr15AuthorizationServerMiddleware.php index be3bd379b..5ca2d1a61 100644 --- a/src/Middleware/Psr15AuthorizationServerMiddleware.php +++ b/src/Middleware/Psr15AuthorizationServerMiddleware.php @@ -21,7 +21,6 @@ class Psr15AuthorizationServerMiddleware implements MiddlewareInterface { - /** * @var AuthorizationServer */ @@ -33,7 +32,7 @@ class Psr15AuthorizationServerMiddleware implements MiddlewareInterface private $responseFactory; /** - * @param AuthorizationServer $server + * @param AuthorizationServer $server * @param ResponseFactoryInterface $responseFactory */ public function __construct(AuthorizationServer $server, ResponseFactoryInterface $responseFactory) @@ -43,7 +42,7 @@ public function __construct(AuthorizationServer $server, ResponseFactoryInterfac } /** - * @param ServerRequestInterface $request + * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface */ @@ -63,5 +62,4 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface // Pass the request on to the next responder in the chain return $handler->handle($request); } - } diff --git a/src/Middleware/Psr15ResourceServerMiddleware.php b/src/Middleware/Psr15ResourceServerMiddleware.php index 390a66bde..d8c1f5ff6 100644 --- a/src/Middleware/Psr15ResourceServerMiddleware.php +++ b/src/Middleware/Psr15ResourceServerMiddleware.php @@ -21,7 +21,6 @@ class Psr15ResourceServerMiddleware implements MiddlewareInterface { - /** * @var ResourceServer */ @@ -33,7 +32,7 @@ class Psr15ResourceServerMiddleware implements MiddlewareInterface private $responseFactory; /** - * @param ResourceServer $server + * @param ResourceServer $server * @param ResponseFactoryInterface $responseFactory */ public function __construct(ResourceServer $server, ResponseFactoryInterface $responseFactory) @@ -43,7 +42,7 @@ public function __construct(ResourceServer $server, ResponseFactoryInterface $re } /** - * @param ServerRequestInterface $request + * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * @return ResponseInterface */ @@ -63,5 +62,4 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface // Pass the request on to the next responder in the chain return $handler->handle($request); } - } diff --git a/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php b/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php index c55a8dcf3..6d544db8c 100644 --- a/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php +++ b/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php @@ -92,7 +92,7 @@ public function testOAuthErrorResponse() $responseFactoryMock->method('createResponse')->willReturn(new Response()); $requestHandlerMock = $this->getMockBuilder(RequestHandlerInterface::class)->getMock(); $requestHandlerMock->method('handle')->willReturn(new Response()); - + $middleware = new Psr15AuthorizationServerMiddleware($server, $responseFactoryMock); $response = $middleware->process( From 665ec5155e575c145e7324a0712088b3851554ad Mon Sep 17 00:00:00 2001 From: Manuel Dimmler Date: Mon, 8 Jul 2019 13:27:11 +0200 Subject: [PATCH 3/8] fix styleci issues --- src/Middleware/Psr15AuthorizationServerMiddleware.php | 1 + src/Middleware/Psr15ResourceServerMiddleware.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Middleware/Psr15AuthorizationServerMiddleware.php b/src/Middleware/Psr15AuthorizationServerMiddleware.php index 5ca2d1a61..c735a1daf 100644 --- a/src/Middleware/Psr15AuthorizationServerMiddleware.php +++ b/src/Middleware/Psr15AuthorizationServerMiddleware.php @@ -44,6 +44,7 @@ public function __construct(AuthorizationServer $server, ResponseFactoryInterfac /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler + * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface diff --git a/src/Middleware/Psr15ResourceServerMiddleware.php b/src/Middleware/Psr15ResourceServerMiddleware.php index d8c1f5ff6..bb507f7b2 100644 --- a/src/Middleware/Psr15ResourceServerMiddleware.php +++ b/src/Middleware/Psr15ResourceServerMiddleware.php @@ -44,6 +44,7 @@ public function __construct(ResourceServer $server, ResponseFactoryInterface $re /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler + * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface From 8c3a7e450e98b89ec464594ecc1202c7ad73f728 Mon Sep 17 00:00:00 2001 From: Manuel Dimmler Date: Mon, 8 Jul 2019 13:28:27 +0200 Subject: [PATCH 4/8] fix styleci issues --- src/Middleware/Psr15AuthorizationServerMiddleware.php | 2 +- src/Middleware/Psr15ResourceServerMiddleware.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Middleware/Psr15AuthorizationServerMiddleware.php b/src/Middleware/Psr15AuthorizationServerMiddleware.php index c735a1daf..979988d65 100644 --- a/src/Middleware/Psr15AuthorizationServerMiddleware.php +++ b/src/Middleware/Psr15AuthorizationServerMiddleware.php @@ -44,7 +44,7 @@ public function __construct(AuthorizationServer $server, ResponseFactoryInterfac /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler - * + * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface diff --git a/src/Middleware/Psr15ResourceServerMiddleware.php b/src/Middleware/Psr15ResourceServerMiddleware.php index bb507f7b2..a3a3ce4bb 100644 --- a/src/Middleware/Psr15ResourceServerMiddleware.php +++ b/src/Middleware/Psr15ResourceServerMiddleware.php @@ -44,7 +44,7 @@ public function __construct(ResourceServer $server, ResponseFactoryInterface $re /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler - * + * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface From c8bd2b606b2b1735de2fe830a599b4d6c36aa2d2 Mon Sep 17 00:00:00 2001 From: Andrew Millington Date: Sun, 10 Nov 2019 12:49:19 +0000 Subject: [PATCH 5/8] Update changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a5e9d61c..9abfba01d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- Added PSR15/17 compatible middleware (PR #1029) + ### Changed - If an error is encountered when running `preg_match()` to validate an RSA key, the server will now throw a RuntimeException (PR #1047) - Replaced deprecated methods with recommended ones when using `Lcobucci\JWT\Builder` to build a JWT token. (PR #1060) - When storing a key, we no longer touch the file before writing it as this is an unnecessary step (PR #1064) - ### Fixed - Clients are now explicitly prevented from using the Client Credentials grant unless they are confidential to conform with the OAuth2 spec (PR #1035) From c2a51cba7693d419805a075c84d8f4f11cb3b1ef Mon Sep 17 00:00:00 2001 From: Andrew Millington Date: Sun, 10 Nov 2019 12:53:14 +0000 Subject: [PATCH 6/8] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9abfba01d..0d78281c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added -- Added PSR15/17 compatible middleware (PR #1029) +- Add PSR15 compatible middleware (PR #1029) ### Changed - If an error is encountered when running `preg_match()` to validate an RSA key, the server will now throw a RuntimeException (PR #1047) From 5875dc561a8ebd1a59af6765b3ba3af1d6de7c9f Mon Sep 17 00:00:00 2001 From: Andrew Millington Date: Wed, 13 Nov 2019 16:45:10 +0000 Subject: [PATCH 7/8] make client confidential --- tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php b/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php index 6d544db8c..654c99631 100644 --- a/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php +++ b/tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php @@ -24,8 +24,11 @@ class Psr15AuthorizationServerMiddlewareTest extends TestCase public function testValidResponse() { + $client = new ClientEntity(); + $client->setConfidential(); + $clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); - $clientRepository->method('getClientEntity')->willReturn(new ClientEntity()); + $clientRepository->method('getClientEntity')->willReturn($client); $scopeEntity = new ScopeEntity; $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); @@ -63,6 +66,7 @@ public function testValidResponse() $request, $requestHandlerMock ); + $this->assertEquals(200, $response->getStatusCode()); } From 203a78c6be932fc251bff4fc0f16dffa1a5231b9 Mon Sep 17 00:00:00 2001 From: Andrew Millington Date: Wed, 13 Nov 2019 16:57:41 +0000 Subject: [PATCH 8/8] Make client confidential and use DateTimeImmutable --- .../Psr15ResourceServerMiddlewareTest.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/Middleware/Psr15ResourceServerMiddlewareTest.php b/tests/Middleware/Psr15ResourceServerMiddlewareTest.php index 4d8b5a709..aa3085408 100644 --- a/tests/Middleware/Psr15ResourceServerMiddlewareTest.php +++ b/tests/Middleware/Psr15ResourceServerMiddlewareTest.php @@ -2,6 +2,8 @@ namespace LeagueTests\Middleware; +use DateInterval; +use DateTimeImmutable; use League\OAuth2\Server\CryptKey; use League\OAuth2\Server\Middleware\Psr15ResourceServerMiddleware; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; @@ -25,14 +27,16 @@ public function testValidResponse() $client = new ClientEntity(); $client->setIdentifier('clientName'); + $client->setConfidential(); $accessToken = new AccessTokenEntity(); $accessToken->setIdentifier('test'); $accessToken->setUserIdentifier(123); - $accessToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('PT1H'))); + $accessToken->setExpiryDateTime((new DateTimeImmutable())->add(new DateInterval('PT1H'))); $accessToken->setClient($client); + $accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); - $token = $accessToken->convertToJWT(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $token = (string) $accessToken; $request = new ServerRequest(); $request = $request->withHeader('authorization', sprintf('Bearer %s', $token)); @@ -64,10 +68,11 @@ public function testValidResponseExpiredToken() $accessToken = new AccessTokenEntity(); $accessToken->setIdentifier('test'); $accessToken->setUserIdentifier(123); - $accessToken->setExpiryDateTime((new \DateTime())->sub(new \DateInterval('PT1H'))); + $accessToken->setExpiryDateTime((new DateTimeImmutable())->sub(new DateInterval('PT1H'))); $accessToken->setClient($client); + $accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); - $token = $accessToken->convertToJWT(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); + $token = (string) $accessToken; $request = new ServerRequest(); $request = $request->withHeader('authorization', sprintf('Bearer %s', $token));