-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add PSR15/PSR-17 compliant middleware #1029
Open
datapp
wants to merge
9
commits into
thephpleague:master
Choose a base branch
from
datapp:feature/psr15-middleware-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16ae177
add PSR15/PSR-17 compliant middleware
d6731f9
fix styleci issues
665ec51
fix styleci issues
8c3a7e4
fix styleci issues
e2c49f2
Merge upstream master into this branch
Sephster c8bd2b6
Update changelog
Sephster c2a51cb
Update changelog
Sephster 5875dc5
make client confidential
Sephster 203a78c
Make client confidential and use DateTimeImmutable
Sephster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ examples/public.key | |
examples/private.key | ||
build | ||
*.orig | ||
/nbproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/** | ||
* @author Alex Bilbie <[email protected]> | ||
* @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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This statement is unreachable. |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/** | ||
* @author Alex Bilbie <[email protected]> | ||
* @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); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
tests/Middleware/Psr15AuthorizationServerMiddlewareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Middleware; | ||
|
||
use League\OAuth2\Server\AuthorizationServer; | ||
use League\OAuth2\Server\Grant\ClientCredentialsGrant; | ||
use League\OAuth2\Server\Middleware\Psr15AuthorizationServerMiddleware; | ||
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; | ||
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; | ||
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; | ||
use LeagueTests\Stubs\AccessTokenEntity; | ||
use LeagueTests\Stubs\ClientEntity; | ||
use LeagueTests\Stubs\ScopeEntity; | ||
use LeagueTests\Stubs\StubResponseType; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\ServerRequestFactory; | ||
|
||
class Psr15AuthorizationServerMiddlewareTest extends TestCase | ||
{ | ||
const DEFAULT_SCOPE = 'basic'; | ||
|
||
public function testValidResponse() | ||
{ | ||
$client = new ClientEntity(); | ||
$client->setConfidential(); | ||
|
||
$clientRepository = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); | ||
$clientRepository->method('getClientEntity')->willReturn($client); | ||
|
||
$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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for the body of the response. Now it is empty. |
||
} | ||
|
||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Middleware; | ||
|
||
use DateInterval; | ||
use DateTimeImmutable; | ||
use League\OAuth2\Server\CryptKey; | ||
use League\OAuth2\Server\Middleware\Psr15ResourceServerMiddleware; | ||
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; | ||
use League\OAuth2\Server\ResourceServer; | ||
use LeagueTests\Stubs\AccessTokenEntity; | ||
use LeagueTests\Stubs\ClientEntity; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\ServerRequest; | ||
|
||
class Psr15ResourceServerMiddlewareTest extends TestCase | ||
{ | ||
public function testValidResponse() | ||
{ | ||
$server = new ResourceServer( | ||
$this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(), | ||
'file://' . __DIR__ . '/../Stubs/public.key' | ||
); | ||
|
||
$client = new ClientEntity(); | ||
$client->setIdentifier('clientName'); | ||
$client->setConfidential(); | ||
|
||
$accessToken = new AccessTokenEntity(); | ||
$accessToken->setIdentifier('test'); | ||
$accessToken->setUserIdentifier(123); | ||
$accessToken->setExpiryDateTime((new DateTimeImmutable())->add(new DateInterval('PT1H'))); | ||
$accessToken->setClient($client); | ||
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); | ||
|
||
$token = (string) $accessToken; | ||
|
||
$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 DateTimeImmutable())->sub(new DateInterval('PT1H'))); | ||
$accessToken->setClient($client); | ||
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); | ||
|
||
$token = (string) $accessToken; | ||
|
||
$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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should return the response here