Skip to content

Commit

Permalink
Merge pull request #3 from TomHAnderson/feature/laminas2
Browse files Browse the repository at this point in the history
Feature/laminas2
  • Loading branch information
TomHAnderson authored Jul 18, 2020
2 parents fa17651 + 7678658 commit 8abe333
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 221 deletions.
12 changes: 3 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,15 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1
- php: 7.3
env:
- DEPS=lowest
- php: 7.1
- php: 7.3
env:
- DEPS=latest
- EXECUTE_COVERAGE=true
- EXECUTE_CS_CHECK=true
- php: 7.2
env:
- DEPS=lowest
- php: 7.2
env:
- DEPS=latest
- php: nightly
- php: 7.4

allow_failures:
- php: nightly
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
OAuth2 Doctrine Identity
------------------------

[![Build Status](https://travis-ci.org/API-Skeletons/zf-oauth2-doctrine-identity.svg)](https://travis-ci.org/API-Skeletons/zf-oauth2-doctrine-identity)
[![Build Status](https://travis-ci.org/API-Skeletons/oauth2-doctrine-identity.svg)](https://travis-ci.org/API-Skeletons/oauth2-doctrine-identity)
[![Gitter](https://badges.gitter.im/api-skeletons/open-source.svg)](https://gitter.im/api-skeletons/open-source)
[![Patreon](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/apiskeletons)
[![Total Downloads](https://poser.pugx.org/api-skeletons/zf-oauth2-doctrine-identity/downloads)](https://packagist.org/packages/api-skeletons/zf-oauth2-doctrine-identity)
[![Total Downloads](https://poser.pugx.org/api-skeletons/oauth2-doctrine-identity/downloads)](https://packagist.org/packages/api-skeletons/oauth2-doctrine-identity)

Versions
--------
Expand All @@ -15,31 +15,34 @@ Versions
About
-----

By default [zfcampus/zf-mvc-auth](https://github.com/zfcampus/zf-mvc-auth) returns an [`ZF\MvcAuth\Identity\AuthenticatedIdentity`](https://github.com/zfcampus/zf-mvc-auth/blob/master/src/Identity/AuthenticatedIdentity.php) when authenticated with a valid access token. This repository replaces that identity with a [`ZF\OAuth2\Doctrine\Identity\AuthenticatedIdentity`](https://github.com/API-Skeletons/zf-oauth2-doctrine-identity/blob/master/src/AuthenticatedIdentity.php).
By default [laminas-api-tools/api-tools-mvc-auth](https://github.com/laminas-api-tools/api-tools-mvc-auth) returns an [`Laminas\ApiTools\MvcAuth\Identity\AuthenticatedIdentity`](https://github.com/laminas-api-tools/api-tools-mvc-auth/blob/master/src/Identity/AuthenticatedIdentity.php) when authenticated with a valid access token. This repository replaces that identity with a [`ApiSkeletons\OAuth2\Doctrine\Identity\AuthenticatedIdentity`](https://github.com/API-Skeletons/oauth2-doctrine-identity/blob/master/src/AuthenticatedIdentity.php).

[`ZF\OAuth2\Doctrine\Identity\AuthenticatedIdentity`](https://github.com/API-Skeletons/zf-oauth2-doctrine-identity/blob/master/src/AuthenticatedIdentity.php) stores the [api-skeletons/zf-oauth2-doctrine](https://github.com/API-Skeletons/zf-oauth2-doctrine) `AccessToken` Doctrine entity. The `AuthentiatedIdentity` has the functions `getUser()`, `getAccessToken()`, `getClient()` which return entities. With these your application can continue to work with ORM through the rest of the request lifecycle.
[`ApiSkeletons\OAuth2\Doctrine\Identity\AuthenticatedIdentity`](https://github.com/API-Skeletons/oauth2-doctrine-identity/blob/master/src/AuthenticatedIdentity.php) stores the [api-skeletons/oauth2-doctrine](https://github.com/API-Skeletons/oauth2-doctrine) `AccessToken` Doctrine entity. The `AuthentiatedIdentity` has the functions `getUser()`, `getAccessToken()`, `getClient()` which return entities. With these your application can continue to work with ORM through the rest of the request lifecycle.

[api-skeletons/zf-oauth2-doctrine](https://github.com/API-Skeletons/zf-oauth2-doctrine) supports multiple OAuth2 configurations and [api-skeletons/zf-oauth2-doctrine-identity](https://github.com/API-Skeletons/zf-oauth2-doctrine-identity) searches through each configuration to find the `AccessToken` entity based on the `access_token` and `client_id` supplied by [`ZF\MvcAuth\Identity\AuthenticatedIdentity`](https://github.com/zfcampus/zf-mvc-auth/blob/master/src/Identity/AuthenticatedIdentity.php).
[api-skeletons/oauth2-doctrine](https://github.com/API-Skeletons/oauth2-doctrine) supports multiple OAuth2 configurations and [api-skeletons/oauth2-doctrine-identity](https://github.com/API-Skeletons/oauth2-doctrine-identity) searches through each configuration to find the `AccessToken` entity based on the `access_token` and `client_id` supplied by [`Laminas\ApiTools\MvcAuth\Identity\AuthenticatedIdentity`](https://github.com/laminas-api-tools/api-tools-mvc-auth/blob/master/src/Identity/AuthenticatedIdentity.php).


Authorization
-------------

The zf-mvc-auth Authorization Service is injected into the `AuthenticatedIdentity`. You may fetch the Authorization Service with `$identity->getAuthorizationService()`. There is a convenience method for ACL `$identity->isAuthorized($resource, $privilege)`.
The api-tools-mvc-auth Authorization Service is injected into the `AuthenticatedIdentity`. You may fetch the Authorization Service with `$identity->getAuthorizationService()`. There is a convenience method for ACL `$identity->isAuthorized($resource, $privilege)`.


Installation
------------

Installation of this module uses composer. For composer documentation, please refer to [getcomposer.org](http://getcomposer.org/).

```sh
composer require api-skeletons/zf-oauth2-doctrine-identity
composer require api-skeletons/oauth2-doctrine-identity
```

This will be added to your application's list of modules:

```php
'modules' => array(
...
'ZF\OAuth2\Doctrine\Identity',
'ApiSkeletons\OAuth2\Doctrine\Identity',
),
```

38 changes: 20 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "api-skeletons/zf-oauth2-doctrine-identity",
"name": "api-skeletons/oauth2-doctrine-identity",
"description": "Doctrine Enabled AuthenticatedIdentity",
"type": "library",
"license": "MIT",
"keywords": [
"oauth2",
"doctrine",
"apigility",
"identity"
"identity",
"laminas"
],
"authors": [
{
Expand All @@ -16,37 +17,38 @@
}
],
"require": {
"php": "^7.1",
"php": "^7.3",
"container-interop/container-interop": "^1.1",
"api-skeletons/zf-oauth2-doctrine": "^3.1",
"api-skeletons/oauth2-doctrine": "^5.0",
"bshaffer/oauth2-server-php": "^1.10",
"gianarb/angry": "^0.1"
"gianarb/angry": "^0.1",
"laminas/laminas-dependency-plugin": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"satooshi/php-coveralls": "^1.0",
"zendframework/zend-test": "^3.0",
"zfcampus/zf-apigility": "^1.3",
"zfcampus/zf-apigility-doctrine": "^2.1",
"zendframework/zend-i18n": "^2.7",
"zendframework/zend-hydrator": "^2.2",
"api-skeletons/coding-standard": "^1.0",
"phpro/zf-doctrine-hydration-module": "^2.0 || ^3.0"
"laminas/laminas-hydrator": "^2.2",
"laminas/laminas-i18n": "^2.7",
"laminas/laminas-test": "^3.0",
"laminas-api-tools/api-tools": "^1.4.1",
"laminas-api-tools/api-tools-doctrine": "^2.3.0",
"phpunit/phpunit": "^5.7",
"phpro/zf-doctrine-hydration-module": "^2.0.1 || ^3.0 || ^4.1",
"satooshi/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
"ZF\\OAuth2\\Doctrine\\Identity\\": "src/"
"ApiSkeletons\\OAuth2\\Doctrine\\Identity\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ZFTest\\OAuth2\\Doctrine\\Identity\\": "test/"
"ApiSkeletonsTest\\OAuth2\\Doctrine\\Identity\\": "test/Identity/"
}
},
"extra": {
"zf": {
"component": "ZF\\OAuth2\\Doctrine\\Identity",
"config-provider": "ZF\\OAuth2\\Doctrine\\ConfigProvider"
"laminas": {
"component": "ApiSkeletons\\OAuth2\\Doctrine\\Identity",
"config-provider": "ApiSkeletons\\OAuth2\\Doctrine\\Identity\\ConfigProvider"
}
}
}
2 changes: 1 addition & 1 deletion src/AccessTokenException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ZF\OAuth2\Doctrine\Identity;
namespace ApiSkeletons\OAuth2\Doctrine\Identity;

use Exception;

Expand Down
14 changes: 7 additions & 7 deletions src/AuthenticatedIdentity.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ZF\OAuth2\Doctrine\Identity;
namespace ApiSkeletons\OAuth2\Doctrine\Identity;

use ZF\MvcAuth\Identity\IdentityInterface;
use ZF\MvcAuth\Authorization\AuthorizationInterface;
use Zend\Permissions\Rbac\Role as RbacRole;
use Zend\Permissions\Acl\Acl;
use ZF\OAuth2\Doctrine\Identity\Exception;
use Laminas\ApiTools\MvcAuth\Identity\IdentityInterface;
use Laminas\ApiTools\MvcAuth\Authorization\AuthorizationInterface;
use Laminas\Permissions\Rbac\Role as RbacRole;
use Laminas\Permissions\Acl\Acl;
use ApiSkeletons\OAuth2\Doctrine\Identity\Exception;
use GianArb\Angry\Uninvokable;

class AuthenticatedIdentity extends RbacRole implements
Expand Down Expand Up @@ -60,7 +60,7 @@ public function getRoleId()
return $this->getName();
}

// For ZF\OAuth2\Provider\UserId\AuthenticationService
// For Laminas\ApiTools\OAuth2\Provider\UserId\AuthenticationService
public function getId()
{
return $this->getUser()->getId();
Expand Down
14 changes: 7 additions & 7 deletions src/AuthenticationPostListener.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace ZF\OAuth2\Doctrine\Identity;
namespace ApiSkeletons\OAuth2\Doctrine\Identity;

use Interop\Container\ContainerInterface;
use ZF\MvcAuth\MvcAuthEvent;
use ZF\MvcAuth\Identity\AuthenticatedIdentity as MvcAuthAuthenticatedIdentity;
use ZF\OAuth2\Doctrine\Identity\AuthenticatedIdentity as DoctrineAuthenticatedIdentity;
use ZF\OAuth2\Doctrine\Identity\AccessTokenException;
use Laminas\ApiTools\MvcAuth\MvcAuthEvent;
use Laminas\ApiTools\MvcAuth\Identity\AuthenticatedIdentity as MvcAuthAuthenticatedIdentity;
use ApiSkeletons\OAuth2\Doctrine\Identity\AuthenticatedIdentity as DoctrineAuthenticatedIdentity;
use ApiSkeletons\OAuth2\Doctrine\Identity\AccessTokenException;
use GianArb\Angry\Unclonable;
use GianArb\Angry\Unserializable;

Expand Down Expand Up @@ -39,7 +39,7 @@ public function __invoke(MvcAuthEvent $mvcAuthEvent)
$accessToken,
$mvcAuthEvent->getAuthorizationService()
);
$mvcAuthEvent->getMvcEvent()->setParam('ZF\MvcAuth\Identity', $doctrineAuthenticatedIdentity);
$mvcAuthEvent->getMvcEvent()->setParam('Laminas\ApiTools\MvcAuth\Identity', $doctrineAuthenticatedIdentity);
$mvcAuthEvent->setIdentity($doctrineAuthenticatedIdentity);
}

Expand All @@ -48,7 +48,7 @@ private function findAccessToken(array $identity)
{
$config = $this->container->get('config');

foreach ($config['zf-oauth2-doctrine'] as $oauth2Config) {
foreach ($config['apiskeletons-oauth2-doctrine'] as $oauth2Config) {
if (array_key_exists('object_manager', $oauth2Config)) {
$objectManager = $this->container->get($oauth2Config['object_manager']);
$accessTokenRepository = $objectManager
Expand Down
2 changes: 1 addition & 1 deletion src/AuthenticationPostListenerFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ZF\OAuth2\Doctrine\Identity;
namespace ApiSkeletons\OAuth2\Doctrine\Identity;

use Interop\Container\ContainerInterface;

Expand Down
2 changes: 1 addition & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ZF\OAuth2\Doctrine\Identity;
namespace ApiSkeletons\OAuth2\Doctrine\Identity;

class ConfigProvider
{
Expand Down
10 changes: 5 additions & 5 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace ZF\OAuth2\Doctrine\Identity;
namespace ApiSkeletons\OAuth2\Doctrine\Identity;

use Zend\Mvc\ModuleRouteListener;
use ZF\MvcAuth\MvcAuthEvent;
use Zend\Mvc\MvcEvent;
use Laminas\Mvc\ModuleRouteListener;
use Laminas\ApiTools\MvcAuth\MvcAuthEvent;
use Laminas\Mvc\MvcEvent;

class Module
{
Expand All @@ -25,7 +25,7 @@ public function getConfig()
public function getAutoloaderConfig()
{
return [
'Zend\Loader\StandardAutoloader' => [
'Laminas\Loader\StandardAutoloader' => [
'namespaces' => [
__NAMESPACE__ => __DIR__,
],
Expand Down
22 changes: 11 additions & 11 deletions test/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
*/

namespace ZFTest\OAuth2\Doctrine\Identity;
namespace ApiSkeletonsTest\OAuth2\Doctrine\Identity;

use Zend\Loader\AutoloaderFactory;
use Laminas\Loader\AutoloaderFactory;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
Expand All @@ -26,10 +26,10 @@ public static function init()
{
$config = __DIR__ . '/asset/autoload/oauth2.doctrine-orm.global.php';
copy(
__DIR__ . '/../vendor/api-skeletons/zf-oauth2-doctrine/config/oauth2.doctrine-orm.global.php.dist',
__DIR__ . '/../vendor/api-skeletons/oauth2-doctrine/config/oauth2.doctrine-orm.global.php.dist',
$config
);
`find $config -type f -exec sed -i '' -e 's/ZFTest\\\\OAuth2\\\\Doctrine\\\\Entity\\\\User/ZFTest\\\\OAuth2\\\\Doctrine\\\\Identity\\\\Entity\\\\User/g' {} \;`;
`find $config -type f -exec sed -i '' -e 's/Testing\\\\Entity\\\\User/ApiSkeletonsTest\\\\OAuth2\\\\Doctrine\\\\Identity\\\\Entity\\\\User/g' {} \;`;

static::initAutoloader();
}
Expand All @@ -43,21 +43,21 @@ protected static function initAutoloader()
return;
}

$zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));
$laminasPath = getenv('LAMINAS_PATH') ?: (defined('LAMINAS_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/Laminas/library') ? $vendorPath . '/Laminas/library' : false));

if (!$zf2Path) {
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
if (!$laminasPath) {
throw new RuntimeException('Unable to load Laminas. Run `php composer.phar install` or define a LAMINAS_PATH environment variable.');
}

if (isset($loader)) {
$loader->add('Zend', $zf2Path . '/Zend');
$loader->add('Laminas', $laminasPath . '/Laminas');
} else {
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
include $laminasPath . '/Laminas/Loader/AutoloaderFactory.php';
AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'Laminas\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
'ZF\OAuth2\Doctrine\Identity' => __DIR__ . '/../src/',
'ApiSkeletons\OAuth2\Doctrine\Identity' => realpath(__DIR__ . '/../src/'),
__NAMESPACE__ => __DIR__,
),
),
Expand Down
14 changes: 7 additions & 7 deletions test/Identity/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace ZFTest\OAuth2\Doctrine\Identity;
namespace ApiSkeletonsTest\OAuth2\Doctrine\Identity;

use Doctrine\ORM\Tools\SchemaTool;
use ZF\OAuth2\Doctrine\Entity;
use ZFTest\OAuth2\Doctrine\Identity\Entity\User;
use Zend\Crypt\Password\Bcrypt;
use Datetime;
use Exception;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use Doctrine\ORM\Tools\SchemaTool;
use ApiSkeletons\OAuth2\Doctrine\Entity;
use Laminas\Crypt\Password\Bcrypt;
use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use ApiSkeletonsTest\OAuth2\Doctrine\Identity\Entity\User;

abstract class AbstractTest extends AbstractHttpControllerTestCase
{
Expand Down Expand Up @@ -39,7 +39,7 @@ protected function setUp()
$objectManager = $serviceManager->get('doctrine.entitymanager.orm_default');

try {
$objectManager->getRepository('ZF\OAuth2\Doctrine\Entity\Scope')->findAll();
$objectManager->getRepository('ApiSkeletons\OAuth2\Doctrine\Entity\Scope')->findAll();
} catch (Exception $e) {
$bcrypt = new Bcrypt();
$bcrypt->setCost(10);
Expand Down
17 changes: 9 additions & 8 deletions test/Identity/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace ZFTest\OAuth2\Doctrine\Identity;
namespace ApiSkeletonsTest\OAuth2\Doctrine\Identity;

use Zend\Stdlib\Request;
use Laminas\Stdlib\Request;

class UserTest extends AbstractTest
{
Expand Down Expand Up @@ -41,19 +41,20 @@ public function testAuthenticatedUser()
$authorization = $serviceManager->get('authentication');
$identity = $authorization->getIdentity();

$this->assertTrue($identity instanceof \ZF\OAuth2\Doctrine\Identity\AuthenticatedIdentity);
$this->assertTrue($identity->getUser() instanceof \ZFTest\OAuth2\Doctrine\Identity\Entity\User);
$this->assertTrue($identity->getClient() instanceof \ZF\OAuth2\Doctrine\Entity\Client);
$this->assertTrue($identity->getAccessToken() instanceof \ZF\OAuth2\Doctrine\Entity\AccessToken);
$this->assertTrue($identity instanceof \ApiSkeletons\OAuth2\Doctrine\Identity\AuthenticatedIdentity);
$this->assertTrue($identity->getUser() instanceof \ApiSkeletonsTest\OAuth2\Doctrine\Identity\Entity\User);
$this->assertTrue($identity->getClient() instanceof \ApiSkeletons\OAuth2\Doctrine\Entity\Client);
$this->assertTrue($identity->getAccessToken() instanceof \ApiSkeletons\OAuth2\Doctrine\Entity\AccessToken);
$this->assertTrue(
$identity->getAuthorizationService() instanceof \ZF\MvcAuth\Authorization\AuthorizationInterface
$identity->getAuthorizationService()
instanceof \Laminas\ApiTools\MvcAuth\Authorization\AuthorizationInterface
);
$this->assertEquals('doctrine', $identity->getRoleId());
$this->assertTrue(is_array($identity->getAuthenticationIdentity()));
}

/**
* @expectedE xception ZF\OAuth2\Doctrine\Permissions\Acl\Exception\AccessTokenException
* @expectedE xception Laminas\ApiTools\OAuth2\Doctrine\Permissions\Acl\Exception\AccessTokenException
*/
public function testInvalidAccessToken()
{
Expand Down
Loading

0 comments on commit 8abe333

Please sign in to comment.