/**
  * Create and return an OAuth2Adapter instance.
  *
  * @param string|array $type
  * @param array $config
  * @param ServiceLocatorInterface $services
  * @return OAuth2Adapter
  * @throws ServiceNotCreatedException when missing details necessary to
  *     create instance and/or dependencies.
  */
 public static function factory($type, array $config, ServiceLocatorInterface $services)
 {
     if (!isset($config['storage']) || !is_array($config['storage'])) {
         throw new ServiceNotCreatedException('Missing storage details for OAuth2 server');
     }
     return new OAuth2Adapter(OAuth2ServerFactory::factory($config['storage'], $services), $type);
 }
Exemplo n.º 2
0
    public function __invoke($services)
    {
        $config = $services->get('Config');

        $oauth2Config  = isset($config['zf-oauth2']) ? $config['zf-oauth2'] : array();
        $mvcAuthConfig = isset($config['zf-mvc-auth']['authentication']['adapters'])
            ? $config['zf-mvc-auth']['authentication']['adapters']
            : array();

        $servers = (object) array('application' => null, 'api' => array());
        return function ($type = null) use ($oauth2Config, $mvcAuthConfig, $services, $servers) {
            // Empty type == legacy configuration.
            if (empty($type)) {
                if ($servers->application) {
                    return $servers->application;
                }
                $factory = new OAuth2ServerInstanceFactory($oauth2Config, $services);
                return $servers->application = $factory();
            }

            if (isset($servers->api[$type])) {
                return $servers->api[$type];
            }

            foreach ($mvcAuthConfig as $name => $adapterConfig) {
                if (! isset($adapterConfig['storage']['route'])) {
                    // Not a zf-oauth2 config
                    continue;
                }

                if ($type !== $adapterConfig['storage']['route']) {
                    continue;
                }

                // Found!
                return $servers->api[$type] = OAuth2ServerFactory::factory(
                    $adapterConfig['storage'],
                    $services
                );
            }

            // At this point, a $type was specified, but no matching adapter
            // was found. Attempt to pull a global OAuth2 instance; if none is
            // present, this will raise an exception anyways.
            if ($servers->application) {
                return $servers->application;
            }
            $factory = new OAuth2ServerInstanceFactory($oauth2Config, $services);
            return $servers->application = $factory();
        };
    }
 /**
  * @dataProvider disableGrantType
  * @group 77
  */
 public function testServerCreatedHasDefaultGrantTypesAsDefinedByOAuth2Module($disable)
 {
     $options = $this->getOAuth2Options();
     $options['zf-oauth2']['grant_types'][$disable] = false;
     $options['zf-oauth2']['storage_settings'] = ['client_table' => 'CLIENTS', 'code_table' => 'AUTHORIZATION_CODES', 'user_table' => 'USERS', 'refresh_token_table' => 'REFRESH_TOKENS', 'jwt_table' => 'JWT'];
     $services = new ServiceManager();
     $services->setService('Config', $options);
     $config = ['adapter' => 'pdo', 'dsn' => 'sqlite::memory:'];
     $server = OAuth2ServerFactory::factory($config, $services);
     $this->assertInstanceOf('OAuth2\\Server', $server);
     $grantTypes = $server->getGrantTypes();
     foreach ($options['zf-oauth2']['grant_types'] as $type => $enabled) {
         // jwt is hinted differently in OAuth2\Server
         if ($type === 'jwt') {
             $type = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
         }
         // If the grant type is not enabled, it should not be present in
         // the returned grant types.
         if (!$enabled) {
             $this->assertArrayNotHasKey($type, $grantTypes);
             continue;
         }
         // If it *is* enabled, it MUST be present.
         $this->assertArrayHasKey($type, $grantTypes);
         switch ($type) {
             case 'client_credentials':
                 $class = 'OAuth2\\GrantType\\ClientCredentials';
                 break;
             case 'authorization_code':
                 $class = 'OAuth2\\GrantType\\AuthorizationCode';
                 break;
             case 'password':
                 $class = 'OAuth2\\GrantType\\UserCredentials';
                 break;
             case 'urn:ietf:params:oauth:grant-type:jwt-bearer':
                 $class = 'OAuth2\\GrantType\\JwtBearer';
                 break;
             case 'refresh_token':
                 $class = 'OAuth2\\GrantType\\RefreshToken';
                 break;
             default:
                 $this->fail(sprintf('Unknown grant type: %s!', $type));
                 break;
         }
         // and have an instance of the appropriate class.
         $this->assertInstanceOf($class, $grantTypes[$type]);
     }
     // Now verify that storage settings are also merged in, which was the
     // original issue.
     $storage = $server->getStorage('scope');
     $r = new ReflectionProperty($storage, 'config');
     $r->setAccessible(true);
     $storageConfig = $r->getValue($storage);
     foreach ($options['zf-oauth2']['storage_settings'] as $key => $value) {
         $this->assertArrayHasKey($key, $storageConfig);
         $this->assertEquals($value, $storageConfig[$key]);
     }
 }
Exemplo n.º 4
0
    public function testCanCreateMongoAdapterBackedServer()
    {
        if (! class_exists('MongoDB')) {
            $this->markTestSkipped('Mongo extension is required for this test');
        }

        $services = $this->mockConfig(new ServiceManager());
        $config = array(
            'adapter' => 'mongo',
            'database' => 'zf-mvc-auth-test',
        );
        $server = OAuth2ServerFactory::factory($config, $services);
        $this->assertInstanceOf('OAuth2\Server', $server);
    }