Skip to content
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

[WIP] Start migrating to PSR7 #23

Merged
merged 3 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
vendor
composer.lock
17 changes: 13 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
language: php

php:
- 5.4
- 5.5
- 5.6
cache:
directories:
- $HOME/.composer/cache

matrix:
fast_finish: true
include:
- php: 5.5
- php: 5.6
- php: 7.0
- php: hhvm
allow_failures:
- php: hhvm

before_script:
- composer self-update
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ compliant server.

## Requirements

- PHP 5.4 or higher
- PHP 5.5 or higher
- [ZfrOAuth2Server](https://github.com/zf-fr/zfr-oauth2-server)

## Versioning note
Expand All @@ -23,7 +23,7 @@ Please note that until I reach 1.0, I **WILL NOT** follow semantic version. This
Installation is only officially supported using Composer:

```sh
php composer.phar require zfr/zfr-oauth2-server-module:0.6.*
php composer.phar require zfr/zfr-oauth2-server-module:0.7.*
```

Copy-paste the `zfr_oauth2_server.global.php.dist` file to your `autoload` folder, and enable the module by adding
Expand All @@ -43,11 +43,14 @@ interface. Then, you need to modify the Doctrine mapping to associate this inter
class. The code is already set in the `zfr_oauth2_server.global.php.dist` file:

```php
use Application\Entity\User;
use ZfrOAuth2\Server\Entity\TokenOwnerInterface;

return [
'doctrine' => [
'entity_resolver' => [
'orm_default' => [
'ZfrOAuth2\Server\Entity\TokenOwnerInterface' => 'Application\Entity\User'
TokenOwnerInterface::class => Application\Entity\User::class
]
]
]
Expand All @@ -61,11 +64,14 @@ want to support. For instance, the following config will make your server compat
grant as well as the "Refresh token" grant:

```php
use ZfrOAuth2\Server\Grant\PasswordGrant;
use ZfrOAuth2\Server\Grant\RefreshTokenGrant';

return [
'zfr_oauth2_server' => [
'grants' => [
'ZfrOAuth2\Server\Grant\PasswordGrant',
'ZfrOAuth2\Server\Grant\RefreshTokenGrant'
PasswordGrant::class,
RefreshTokenGrant::class
]
]
]
Expand Down
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@
"homepage": "http://www.michaelgallego.fr"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4",
"php": ">=5.5",
"zendframework/zend-servicemanager": "~2.2",
"zendframework/zend-modulemanager": "~2.2",
"zendframework/zend-mvc": "~2.2",
"zendframework/zend-http": "~2.2",
"zendframework/zend-console": "~2.2",
"zendframework/zend-stdlib": "~2.2",
"doctrine/doctrine-module": "~0.9",
"zfr/zfr-oauth2-server": "0.7.*"
"zfr/zfr-oauth2-server": "dev-master as 0.7.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpunit/phpunit": "~4.6",
"squizlabs/php_codesniffer": "1.4.*",
"zendframework/zend-view": "~2.2",
"satooshi/php-coveralls": "~0.6"
},
"autoload": {
"psr-0": {
"psr-4": {
"ZfrOAuth2Module\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ZfrOAuth2ModuleTest\\": "tests/"
}
}
}
57 changes: 40 additions & 17 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,56 @@
* and is licensed under the MIT license.
*/

use Doctrine\ORM\Mapping\Driver\XmlDriver;
use ZfrOAuth2\Server\AuthorizationServer;
use ZfrOAuth2\Server\ResourceServer;
use ZfrOAuth2\Server\Service\ClientService;
use ZfrOAuth2\Server\Service\ScopeService;
use ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage;
use ZfrOAuth2Module\Server\Controller\AuthorizationController;
use ZfrOAuth2Module\Server\Controller\TokenController;
use ZfrOAuth2Module\Server\Factory\AccessTokenServiceFactory;
use ZfrOAuth2Module\Server\Factory\AccessTokenStorageFactory;
use ZfrOAuth2Module\Server\Factory\AuthorizationCodeServiceFactory;
use ZfrOAuth2Module\Server\Factory\AuthorizationControllerFactory;
use ZfrOAuth2Module\Server\Factory\AuthorizationServerFactory;
use ZfrOAuth2Module\Server\Factory\ClientServiceFactory;
use ZfrOAuth2Module\Server\Factory\GrantPluginManagerFactory;
use ZfrOAuth2Module\Server\Factory\ModuleOptionsFactory;
use ZfrOAuth2Module\Server\Factory\RefreshTokenServiceFactory;
use ZfrOAuth2Module\Server\Factory\ResourceServerFactory;
use ZfrOAuth2Module\Server\Factory\ScopeServiceFactory;
use ZfrOAuth2Module\Server\Factory\TokenControllerFactory;
use ZfrOAuth2Module\Server\Grant\GrantPluginManager;
use ZfrOAuth2Module\Server\Options\ModuleOptions;

return [
'service_manager' => [
'factories' => [
/**
* Factories that map to a class
*/
'ZfrOAuth2\Server\AuthorizationServer' => 'ZfrOAuth2Module\Server\Factory\AuthorizationServerFactory',
'ZfrOAuth2\Server\ResourceServer' => 'ZfrOAuth2Module\Server\Factory\ResourceServerFactory',
'ZfrOAuth2\Server\Service\ClientService' => 'ZfrOAuth2Module\Server\Factory\ClientServiceFactory',
'ZfrOAuth2\Server\Service\ScopeService' => 'ZfrOAuth2Module\Server\Factory\ScopeServiceFactory',
'ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage' => 'ZfrOAuth2Module\Server\Factory\AccessTokenStorageFactory',
'ZfrOAuth2Module\Server\Options\ModuleOptions' => 'ZfrOAuth2Module\Server\Factory\ModuleOptionsFactory',
'ZfrOAuth2Module\Server\Grant\GrantPluginManager' => 'ZfrOAuth2Module\Server\Factory\GrantPluginManagerFactory',
AuthorizationServer::class => AuthorizationServerFactory::class,
ResourceServer::class => ResourceServerFactory::class,
ClientService::class => ClientServiceFactory::class,
ScopeService::class => ScopeServiceFactory::class,
AccessTokenStorage::class => AccessTokenStorageFactory::class,
ModuleOptions::class => ModuleOptionsFactory::class,
GrantPluginManager::class => GrantPluginManagerFactory::class,

/**
* Factories that do not map to a class
*/
'ZfrOAuth2\Server\Service\AuthorizationCodeService' => 'ZfrOAuth2Module\Server\Factory\AuthorizationCodeServiceFactory',
'ZfrOAuth2\Server\Service\AccessTokenService' => 'ZfrOAuth2Module\Server\Factory\AccessTokenServiceFactory',
'ZfrOAuth2\Server\Service\RefreshTokenService' => 'ZfrOAuth2Module\Server\Factory\RefreshTokenServiceFactory',
'ZfrOAuth2\Server\Service\AuthorizationCodeService' => AuthorizationCodeServiceFactory::class,
'ZfrOAuth2\Server\Service\AccessTokenService' => AccessTokenServiceFactory::class,
'ZfrOAuth2\Server\Service\RefreshTokenService' => RefreshTokenServiceFactory::class,
]
],

'doctrine' => [
'driver' => [
'zfr_oauth2_driver' => [
'class' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
'class' => XmlDriver::class,
'paths' => __DIR__ . '/../../zfr-oauth2-server/config/doctrine',
],
'orm_default' => [
Expand Down Expand Up @@ -85,7 +108,7 @@
'options' => [
'route' => '/authorize',
'defaults' => [
'controller' => 'ZfrOAuth2Module\Server\Controller\AuthorizationController',
'controller' => AuthorizationController::class,
'action' => 'authorize'
]
]
Expand All @@ -96,7 +119,7 @@
'options' => [
'route' => '/token',
'defaults' => [
'controller' => 'ZfrOAuth2Module\Server\Controller\TokenController',
'controller' => TokenController::class,
'action' => 'token'
]
]
Expand All @@ -107,7 +130,7 @@
'options' => [
'route' => '/revoke',
'defaults' => [
'controller' => 'ZfrOAuth2Module\Server\Controller\TokenController',
'controller' => TokenController::class,
'action' => 'revoke'
]
]
Expand All @@ -125,7 +148,7 @@
'options' => [
'route' => 'oauth2 server delete expired tokens',
'defaults' => [
'controller' => 'ZfrOAuth2Module\Server\Controller\TokenController',
'controller' => TokenController::class,
'action' => 'delete-expired-tokens'
]
]
Expand All @@ -136,8 +159,8 @@

'controllers' => [
'factories' => [
'ZfrOAuth2Module\Server\Controller\AuthorizationController' => 'ZfrOAuth2Module\Server\Factory\AuthorizationControllerFactory',
'ZfrOAuth2Module\Server\Controller\TokenController' => 'ZfrOAuth2Module\Server\Factory\TokenControllerFactory'
AuthorizationController::class => AuthorizationControllerFactory::class,
TokenController::class => TokenControllerFactory::class
]
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

namespace ZfrOAuth2Module\Server\Authentication\Storage;

use Psr\Http\Message\RequestInterface;
use Zend\Authentication\Storage\NonPersistent;
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\Application;
use Zend\Diactoros\ServerRequestFactory;
use ZfrOAuth2\Server\ResourceServer;

/**
Expand All @@ -35,57 +35,38 @@ class AccessTokenStorage extends NonPersistent
protected $resourceServer;

/**
* @var Application
* @var RequestInterface
*/
private $application;
private $request;

/**
* @param ResourceServer $resourceServer
* @param Application $application
*/
public function __construct(ResourceServer $resourceServer, Application $application)
public function __construct(ResourceServer $resourceServer)
{
$this->resourceServer = $resourceServer;
$this->application = $application;
$this->request = ServerRequestFactory::fromGlobals();
}

/**
* {@inheritDoc}
*/
public function isEmpty()
{
$request = $this->getCurrentRequest();

return $request ? $this->resourceServer->getAccessToken($request) === null : true;
return $this->request ? $this->resourceServer->getAccessToken($this->request) === null : true;
}

/**
* {@inheritDoc}
*/
public function read()
{
$request = $this->getCurrentRequest();

if (!$request) {
if (!$this->request) {
return null;
}

$accessToken = $this->resourceServer->getAccessToken($request);
$accessToken = $this->resourceServer->getAccessToken($this->request);

return $accessToken ? $accessToken->getOwner() : null;
}

/**
* @return HttpRequest|null
*/
private function getCurrentRequest()
{
$request = $this->application->getMvcEvent()->getRequest();

if (!$request instanceof HttpRequest) {
return null;
}

return $request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

namespace ZfrOAuth2Module\Server\Controller;

use Psr\Http\Message\ResponseInterface;
use Zend\Console\Request as ConsoleRequest;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Http\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;
use Zend\Mvc\Controller\AbstractActionController;
use ZfrOAuth2\Server\AuthorizationServer;
use ZfrOAuth2Module\Server\Exception\RuntimeException;
Expand Down Expand Up @@ -55,7 +58,13 @@ public function tokenAction()
return null;
}

return $this->authorizationServer->handleTokenRequest($this->request);
// Currently, ZF2 Http Request object is not PSR-7 compliant, therefore we need to create a new one from
// globals, and then convert the response back to ZF2 format

$request = ServerRequestFactory::fromGlobals();
$response = $this->authorizationServer->handleTokenRequest($request);

return $this->convertToZfResponse($response);
}

/**
Expand All @@ -70,7 +79,13 @@ public function revokeAction()
return null;
}

return $this->authorizationServer->handleRevocationRequest($this->request);
// Currently, ZF2 Http Request object is not PSR-7 compliant, therefore we need to create a new one from
// globals, and then convert the response back to ZF2 format

$request = ServerRequestFactory::fromGlobals();
$response = $this->authorizationServer->handleRevocationRequest($request);

return $this->convertToZfResponse($response);
}

/**
Expand Down Expand Up @@ -99,4 +114,25 @@ public function deleteExpiredTokensAction()

return "\nExpired tokens were properly deleted!\n\n";
}

/**
* Convert a PSR-7 response to ZF2 response
*
* @param ResponseInterface $response
* @return HttpResponse
*/
private function convertToZfResponse(ResponseInterface $response)
{
$zfResponse = new HttpResponse();

$zfResponse->setStatusCode($response->getStatusCode());
$zfResponse->setReasonPhrase($response->getReasonPhrase());
$zfResponse->setContent((string) $response->getBody());

foreach ($response->getHeaders() as $name => $values) {
$zfResponse->getHeaders()->addHeaderLine($name, implode(", ", $values));
}

return $zfResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
{
/* @var $resourceServer \ZfrOAuth2\Server\ResourceServer */
$resourceServer = $serviceLocator->get('ZfrOAuth2\Server\ResourceServer');
/* @var $application \Zend\Mvc\Application */
$application = $serviceLocator->get('Application');

return new AccessTokenStorage($resourceServer, $application);
return new AccessTokenStorage($resourceServer);
}
}
File renamed without changes.
Loading