Multiple OAuth2 Instances

You may have multiple sets of distinct OAuth2 servers on the same application instance. This describes how to create a second OAuth2 server with it’s own entities.

Create a new module called SecondOAuth2 and copy the entities from zf-oauth2-doctrine into it’s src/SecondOAuth2/Entity directory. Change all the namespaces to SecondOAuth2 and this includes autogenerated fully qualified name spaces in the add and remove functions for collections.

Copy the ORM XML data from zf-oauth2-doctrine to SecondOAuth2/config/orm and edit it for the new module. Be sure to change the table names. Add doctrine config to module.config.php

Copy this code into the SecondOAuth2 Module.php:

public function onBootstrap(MvcEvent $e)
{
    /** @var ServiceLocatorInterface $serviceManager */
    $serviceManager = $e->getParam('application')->getServiceManager();
    $serviceManager->get('oauth2.doctrineadapter.second')->bootstrap($e);
}

public function getServiceConfig()
{
    return [
        'factories' => [
            'oauth2.doctrineadapter.second' => function ($serviceManager) {
                /** @var ServiceLocatorInterface | ContainerInterface $serviceManager */
                $globalConfig = $serviceManager->get('Config');
                $config = new Config($globalConfig['zf-oauth2-doctrine']['second']);
                /** @var DoctrineAdapterFactory $factory */
                $factory = $serviceManager->get(DoctrineAdapterFactory::class);
                $factory->setConfig($config);

                return $factory->createService($serviceManager);
            }
        ],
    ];
}

Create a copy of the default config in oauth2.doctrine-orm.global.php and name the block ‘second’. Create a new User entity and assign that entity to the default group. There should not be a reason to have two OAuth2 servers connected to the same User entity. Change namespaces as needed.

Finally follow the configuration section of this documentation to configure the second OAuth2 server.


This is documentation for API-Skeletons/oauth2-doctrine. If you find this useful please add your ★ star to the project.

Authored by API Skeletons, Inc. All rights reserved.