Skip to content

Commit

Permalink
Merge pull request #43 from trikoder/psr-bridge-upgrade
Browse files Browse the repository at this point in the history
Remove direct dependency on "zend-diactoros"
  • Loading branch information
spideyfusion authored Apr 24, 2019
2 parents 1e15327 + 3da3e52 commit 4973e1c
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 70 deletions.
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ addons:

env:
# PHP 7.2
- PHP_VERSION=7.2 SYMFONY_VERSION=3.4.0
- PHP_VERSION=7.2 SYMFONY_VERSION=4.1.0
- PHP_VERSION=7.2 SYMFONY_VERSION=4.2.0
- PHP_VERSION=7.2 PSR_HTTP_PROVIDER=nyholm SYMFONY_VERSION=3.4.0
- PHP_VERSION=7.2 PSR_HTTP_PROVIDER=nyholm SYMFONY_VERSION=4.2.0
- PHP_VERSION=7.2 PSR_HTTP_PROVIDER=zendframework SYMFONY_VERSION=3.4.0
- PHP_VERSION=7.2 PSR_HTTP_PROVIDER=zendframework SYMFONY_VERSION=4.2.0

# PHP 7.3
- PHP_VERSION=7.3 SYMFONY_VERSION=3.4.0
- PHP_VERSION=7.3 SYMFONY_VERSION=4.1.0
- PHP_VERSION=7.3 SYMFONY_VERSION=4.2.0
- PHP_VERSION=7.3 PSR_HTTP_PROVIDER=nyholm SYMFONY_VERSION=3.4.0
- PHP_VERSION=7.3 PSR_HTTP_PROVIDER=nyholm SYMFONY_VERSION=4.2.0
- PHP_VERSION=7.3 PSR_HTTP_PROVIDER=zendframework SYMFONY_VERSION=3.4.0
- PHP_VERSION=7.3 PSR_HTTP_PROVIDER=zendframework SYMFONY_VERSION=4.2.0

install:
- dev/bin/docker-compose build --build-arg PHP_VERSION=${PHP_VERSION} php
Expand Down
10 changes: 6 additions & 4 deletions Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

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 Zend\Diactoros\Response;

final class TokenController
{
Expand All @@ -20,9 +20,11 @@ public function __construct(AuthorizationServer $server)
$this->server = $server;
}

public function indexAction(ServerRequestInterface $serverRequest): ResponseInterface
{
$serverResponse = new Response();
public function indexAction(
ServerRequestInterface $serverRequest,
ResponseFactoryInterface $responseFactory
): ResponseInterface {
$serverResponse = $responseFactory->createResponse();

try {
return $this->server->respondToAccessTokenRequest($serverRequest, $serverResponse);
Expand Down
45 changes: 44 additions & 1 deletion DependencyInjection/TrikoderOAuth2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
use DateInterval;
use League\OAuth2\Server\CryptKey;
use LogicException;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -19,7 +24,7 @@
use Trikoder\Bundle\OAuth2Bundle\Manager\ScopeManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\Scope as ScopeModel;

final class TrikoderOAuth2Extension extends Extension implements PrependExtensionInterface
final class TrikoderOAuth2Extension extends Extension implements PrependExtensionInterface, CompilerPassInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -62,6 +67,44 @@ public function prepend(ContainerBuilder $container)
]);
}

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$this->assertPsrHttpAliasesExist($container);
}

private function assertPsrHttpAliasesExist(ContainerBuilder $container): void
{
$requiredAliases = [
ServerRequestFactoryInterface::class,
StreamFactoryInterface::class,
UploadedFileFactoryInterface::class,
ResponseFactoryInterface::class,
];

foreach ($requiredAliases as $requiredAlias) {
$definition = $container
->getDefinition(
$container->getAlias($requiredAlias)
)
;

$aliasedClass = $definition->getClass();

if (!class_exists($aliasedClass)) {
throw new LogicException(
sprintf(
'Alias \'%s\' points to a non-existing class \'%s\'. Did you configure a PSR-7/17 compatible library?',
$requiredAlias,
$aliasedClass
)
);
}
}
}

private function configureAuthorizationServer(ContainerBuilder $container, array $config): void
{
$authorizationServer = $container
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ This package is currently in the active development.
## Requirements

* [PHP 7.2](http://php.net/releases/7_2_0.php) or greater
* [Symfony 4](https://symfony.com/4) or [Symfony 3.4](https://symfony.com/roadmap/3.4)
* [Symfony 4.2](https://symfony.com/roadmap/4.2) or [Symfony 3.4](https://symfony.com/roadmap/3.4)

## Installation

1. Require the bundle with Composer:
1. Require the bundle and a PSR 7/17 implementation with Composer:

```sh
composer require trikoder/oauth2-bundle --no-plugins --no-scripts
composer require trikoder/oauth2-bundle nyholm/psr7 --no-plugins --no-scripts
```

> **NOTE:** Due to required pre-configuration, this bundle is currently not compatible with [Symfony Flex](https://github.com/symfony/flex).
> **NOTE #1:** Due to required pre-configuration, this bundle is currently not compatible with [Symfony Flex](https://github.com/symfony/flex).

> **NOTE #2:** This bundle requires a PSR 7/17 implementation to operate. We recommend that you use [nyholm/psr7](https://github.com/Nyholm/psr7). Check out this [document](docs/psr-implementation-switching.md) if you wish to use a different implementation.

2. Create the bundle configuration file under `config/packages/trikoder_oauth2.yaml`. Here is a reference configuration file:

Expand Down
33 changes: 20 additions & 13 deletions Tests/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use League\OAuth2\Server\ResourceServer;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand All @@ -38,8 +39,6 @@
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken;
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\FixtureFactory;
use Trikoder\Bundle\OAuth2Bundle\Tests\TestHelper;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

abstract class AbstractIntegrationTest extends TestCase
{
Expand Down Expand Up @@ -78,6 +77,11 @@ abstract class AbstractIntegrationTest extends TestCase
*/
protected $resourceServer;

/**
* @var Psr17Factory
*/
private $psrFactory;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -112,6 +116,8 @@ protected function setUp()
);

$this->resourceServer = $this->createResourceServer($accessTokenRepository);

$this->psrFactory = new Psr17Factory();
}

protected function getAccessToken(string $jwtToken): ?AccessToken
Expand Down Expand Up @@ -146,25 +152,26 @@ protected function getRefreshToken(string $encryptedPayload): ?RefreshToken

protected function createAuthorizationRequest(?string $credentials, array $body = []): ServerRequestInterface
{
$headers = [
'Authorization' => sprintf('Basic %s', base64_encode($credentials)),
];

return new ServerRequest([], [], null, null, 'php://temp', $headers, [], [], $body);
return $this
->psrFactory
->createServerRequest('', '')
->withHeader('Authorization', sprintf('Basic %s', base64_encode($credentials)))
->withParsedBody($body)
;
}

protected function createResourceRequest(string $jwtToken): ServerRequestInterface
{
$headers = [
'Authorization' => sprintf('Bearer %s', $jwtToken),
];

return new ServerRequest([], [], null, null, 'php://temp', $headers);
return $this
->psrFactory
->createServerRequest('', '')
->withHeader('Authorization', sprintf('Bearer %s', $jwtToken))
;
}

protected function handleAuthorizationRequest(ServerRequestInterface $serverRequest): array
{
$response = new Response();
$response = $this->psrFactory->createResponse();

try {
$response = $this->authorizationServer->respondToAccessTokenRequest($serverRequest, $response);
Expand Down
Loading

0 comments on commit 4973e1c

Please sign in to comment.