public function testServiceCreatedWithOverriddenValues() { $adapter = $this->getMockBuilder('OAuth2\\Storage\\Pdo')->disableOriginalConstructor()->getMock(); $this->services->setService('TestAdapter', $adapter); $this->services->setService('Config', array('zf-oauth2' => array('storage' => 'TestAdapter', 'enforce_state' => false, 'allow_implicit' => true, 'access_lifetime' => 12000))); $expectedService = new \OAuth2\Server($adapter, array('enforce_state' => false, 'allow_implicit' => true, 'access_lifetime' => 12000)); $expectedService->addGrantType(new ClientCredentials($adapter)); $expectedService->addGrantType(new AuthorizationCode($adapter)); $expectedService->addGrantType(new UserCredentials($adapter)); $expectedService->addGrantType(new RefreshToken($adapter)); $service = $this->factory->createService($this->services); $this->assertInstanceOf('OAuth2\\Server', $service); $this->assertEquals($expectedService, $service); }
public function testSubsequentCallsReturnTheSameInstance() { $adapter = $this->getMockBuilder('OAuth2\\Storage\\Pdo')->disableOriginalConstructor()->getMock(); $this->services->setService('TestAdapter', $adapter); $this->services->setService('Config', ['zf-oauth2' => ['storage' => 'TestAdapter', 'grant_types' => ['client_credentials' => true, 'authorization_code' => true, 'password' => true, 'refresh_token' => true, 'jwt' => true]]]); $factory = $this->factory->createService($this->services); $server = $factory(); $this->assertSame($server, $factory()); }
/** * Create an OAuth2 server by introspecting the config service * * @param ServiceLocatorInterface $services * @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException * @return false|OAuth2Adapter */ protected function createOAuth2Server(ServiceLocatorInterface $services) { if (!$services->has('Config')) { // If we don't have configuration, we cannot create an OAuth2 server. return false; } $config = $services->get('config'); if (!isset($config['zf-oauth2']['storage']) || !is_string($config['zf-oauth2']['storage']) || !$services->has($config['zf-oauth2']['storage'])) { return false; } if ($services->has('ZF\\OAuth2\\Service\\OAuth2Server')) { // If the service locator already has a pre-configured OAuth2 server, use it. $factory = $services->get('ZF\\OAuth2\\Service\\OAuth2Server'); return new OAuth2Adapter($factory()); } $factory = new ZFOAuth2ServerFactory(); try { $serverFactory = $factory->createService($services); } catch (RuntimeException $e) { // These are exceptions specifically thrown from the // ZF\OAuth2\Factory\OAuth2ServerFactory when essential // configuration is missing. switch (true) { case strpos($e->getMessage(), 'missing'): return false; case strpos($e->getMessage(), 'string or array'): return false; default: // Any other RuntimeException at this point we don't know // about and need to re-throw. throw $e; } } return new OAuth2Adapter($serverFactory(null)); }