Skip to content

Commit

Permalink
Merge 36fe993 into 3c989b6
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Jul 30, 2015
2 parents 3c989b6 + 36fe993 commit 5d7ea94
Show file tree
Hide file tree
Showing 53 changed files with 142 additions and 49 deletions.
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,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;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
use PHPUnit_Framework_TestCase;
use Zend\Authentication\AuthenticationService;
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\RequestInterface;
use ZfrOAuth2\Server\Entity\AccessToken;
use ZfrOAuth2\Server\Entity\TokenOwnerInterface;
use ZfrOAuth2\Server\Exception\OAuth2Exception;
use ZfrOAuth2\Server\ResourceServer;
use ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage;

/**
Expand Down Expand Up @@ -58,9 +63,9 @@ class AuthenticationFunctionalTest extends PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->mvcEvent = $this->getMock('Zend\Mvc\MvcEvent');
$application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
$this->resourceServer = $this->getMock('ZfrOAuth2\Server\ResourceServer', [], [], '', false);
$this->mvcEvent = $this->getMock(MvcEvent::class);
$application = $this->getMock(Application::class, [], [], '', false);
$this->resourceServer = $this->getMock(ResourceServer::class, [], [], '', false);
$this->authenticationStorage = new AccessTokenStorage($this->resourceServer, $application);
$this->authenticationService = new AuthenticationService($this->authenticationStorage);

Expand All @@ -74,7 +79,7 @@ public function testSuccessAuthenticationOnValidToken()
$this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request));

$token = new AccessToken();
$owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface');
$owner = $this->getMock(TokenOwnerInterface::class);
$token->setOwner($owner);

$this
Expand All @@ -96,7 +101,7 @@ public function testFailAuthenticationOnNoToken()
$this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request));

$token = new AccessToken();
$owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface');
$owner = $this->getMock(TokenOwnerInterface::class);
$token->setOwner($owner);

$this
Expand All @@ -117,7 +122,7 @@ public function testFailAuthenticationOnExpiredToken()
$this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request));

$token = new AccessToken();
$owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface');
$owner = $this->getMock(TokenOwnerInterface::class);
$token->setOwner($owner);

$this
Expand All @@ -127,7 +132,7 @@ public function testFailAuthenticationOnExpiredToken()
->with($request)
->will($this->throwException(new OAuth2Exception('Expired token', 123)));

$this->setExpectedException('ZfrOAuth2\Server\Exception\OAuth2Exception', 'Expired token', 123);
$this->setExpectedException(OAuth2Exception::class, 'Expired token', 123);

$this->authenticationService->getIdentity();
}
Expand All @@ -142,7 +147,7 @@ public function testFailAuthenticationOnNoRequest()

public function testFailAuthenticationOnNonHttpRequest()
{
$request = $this->getMock('Zend\Stdlib\RequestInterface');
$request = $this->getMock(RequestInterface::class);

$this->mvcEvent->expects($this->any())->method('getRequest')->will($this->returnValue($request));

Expand Down

0 comments on commit 5d7ea94

Please sign in to comment.