/**
  * @param ServiceLocatorInterface $services
  * @return DefaultAuthenticationListener
  */
 public function createService(ServiceLocatorInterface $services)
 {
     $listener = new DefaultAuthenticationListener();
     $httpAdapter = $this->retrieveHttpAdapter($services);
     if ($httpAdapter) {
         $listener->setHttpAdapter($httpAdapter);
     }
     $oauth2Server = $this->createOAuth2Server($services);
     if ($oauth2Server) {
         $listener->setOauth2Server($oauth2Server);
     }
     return $listener;
 }
 protected function setupMockOAuth2Server($token)
 {
     $server = $this->getMockBuilder('OAuth2\\Server')->disableOriginalConstructor()->getMock();
     $server->expects($this->atLeastOnce())->method('verifyResourceRequest')->will($this->returnValue(true));
     $server->expects($this->atLeastOnce())->method('getAccessTokenData')->will($this->returnValue($token));
     $this->listener->setOauth2Server($server);
 }
Пример #3
0
 public function __invoke(MvcAuthEvent $mvcAuthEvent)
 {
     $identity = parent::__invoke($mvcAuthEvent);
     $authIdentity = array();
     if ($identity instanceof AuthenticatedIdentity) {
         //get user details
         $authIdentity = $identity->getAuthenticationIdentity();
         if (isset($authIdentity['user_id'])) {
             $user = $this->userService->findUserByUsername($authIdentity['user_id']);
             $user->addHydratorFilter("details", new MethodMatchFilter("getDetails"), FilterComposite::CONDITION_AND);
             $userArray = $this->userHydrator->extract($user);
             $authIdentity['user_data'] = $userArray;
         }
         //get oauth client details
         if (isset($authIdentity['client_id'])) {
             $client = $this->oauthClientMapper->fetchEntity($authIdentity['client_id']);
             if ($client) {
                 $authIdentity['client_data'] = $this->oauthClientMapper->getHydrator()->extract($client);
             }
         }
         $identity = new AuthenticatedIdentity($authIdentity);
         $identity->setName($authIdentity['user_data']['roleId']);
     }
     return $identity;
 }
 /**
  * Attach an adaper to the listener as described by $type and $data.
  *
  * @param string $type
  * @param array $adapterConfig
  * @param ContainerInterface $container
  * @param DefaultAuthenticationListener $listener
  */
 private function attachAdapterOfType($type, array $adapterConfig, ContainerInterface $container, DefaultAuthenticationListener $listener)
 {
     if (!isset($adapterConfig['adapter']) || !is_string($adapterConfig['adapter'])) {
         return;
     }
     switch ($adapterConfig['adapter']) {
         case HttpAdapter::class:
             $adapter = AuthenticationHttpAdapterFactory::factory($type, $adapterConfig, $container);
             break;
         case OAuth2Adapter::class:
             $adapter = AuthenticationOAuth2AdapterFactory::factory($type, $adapterConfig, $container);
             break;
         default:
             $adapter = false;
             break;
     }
     if (!$adapter) {
         return;
     }
     $listener->attach($adapter);
 }
    public function testOauth2RequestIncludesHeaders()
    {
        $this->request->getHeaders()->addHeaderLine('Authorization', 'Bearer TOKEN');

        $server = $this->getMockBuilder('OAuth2\Server')
            ->disableOriginalConstructor()
            ->getMock();

        $server->expects($this->atLeastOnce())
            ->method('verifyResourceRequest')
            ->with($this->callback(function (OAuth2Request $request) {
                return $request->headers('Authorization') === 'Bearer TOKEN';
            }));

        $this->listener->attach(new OAuth2Adapter($server));
        $this->listener->__invoke($this->mvcAuthEvent);
    }
 /**
  * @group 83
  */
 public function testAllowsAdaptersToReturnResponsesAndReturnsThemDirectly()
 {
     $map = ['Foo\\V2' => 'custom'];
     $this->listener->setAuthMap($map);
     $request = new HttpRequest();
     $routeMatch = $this->createRouteMatch(['controller' => 'Foo\\V2\\Rest\\Test\\TestController']);
     $mvcEvent = $this->mvcAuthEvent->getMvcEvent();
     $mvcEvent->setRequest($request)->setRouteMatch($routeMatch);
     $types = ['custom'];
     $adapter = $this->getMockBuilder('ZF\\MvcAuth\\Authentication\\AdapterInterface')->disableOriginalConstructor()->getMock();
     $adapter->expects($this->atLeastOnce())->method('provides')->will($this->returnValue($types));
     $adapter->expects($this->any())->method('getTypeFromRequest')->with($this->equalTo($request))->will($this->returnValue('custom'));
     $adapter->expects($this->any())->method('matches')->with($this->equalTo('custom'))->will($this->returnValue(true));
     $response = new HttpResponse();
     $response->setStatusCode(401);
     $adapter->expects($this->once())->method('authenticate')->with($this->equalTo($request), $this->equalTo($this->response))->will($this->returnValue($response));
     $this->listener->attach($adapter);
     $result = $this->listener->__invoke($this->mvcAuthEvent);
     $this->assertSame($response, $result);
 }
 /**
  * @param ServiceLocatorInterface $services
  * @return DefaultAuthenticationListener
  */
 public function createService(ServiceLocatorInterface $services)
 {
     $listener = new DefaultAuthenticationListener();
     $httpAdapter = $this->retrieveHttpAdapter($services);
     if ($httpAdapter) {
         $listener->attach($httpAdapter);
     }
     $oauth2Server = $this->createOAuth2Server($services);
     if ($oauth2Server) {
         $listener->attach($oauth2Server);
     }
     $authenticationTypes = $this->getAuthenticationTypes($services);
     if ($authenticationTypes) {
         $listener->addAuthenticationTypes($authenticationTypes);
     }
     $listener->setAuthMap($this->getAuthenticationMap($services));
     return $listener;
 }
 /**
  * Create and return a DefaultAuthenticationListener.
  *
  * @param ContainerInterface $container
  * @param string             $requestedName
  * @param null|array         $options
  * @return DefaultAuthenticationListener
  */
 public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
 {
     $listener = new DefaultAuthenticationListener();
     $httpAdapter = $this->retrieveHttpAdapter($container);
     if ($httpAdapter) {
         $listener->attach($httpAdapter);
     }
     $oauth2Server = $this->createOAuth2Server($container);
     if ($oauth2Server) {
         $listener->attach($oauth2Server);
     }
     $authenticationTypes = $this->getAuthenticationTypes($container);
     if ($authenticationTypes) {
         $listener->addAuthenticationTypes($authenticationTypes);
     }
     $listener->setAuthMap($this->getAuthenticationMap($container));
     return $listener;
 }
    protected function getController($localFile, $globalFile)
    {
        $authListener = new AuthListener();
        $config = array_merge(require $globalFile, require $localFile);

        /* Register old authentication adapter types */
        if (isset($config['zf-oauth2'])) {
            $authListener->addAuthenticationTypes(array('oauth2'));
        } elseif (isset($config['zf-mvc-auth']['authentication']['http'])) {
            $types = array();
            if (isset($config['zf-mvc-auth']['authentication']['http']['htpasswd'])) {
                $types[] = 'basic';
            }
            if (isset($config['zf-mvc-auth']['authentication']['http']['htdigest'])) {
                $types[] = 'digest';
            }
            $authListener->addAuthenticationTypes($types);
        }

        /* Register v1.1+ adapter types */
        if (isset($config['zf-mvc-auth']['authentication']['adapters'])) {
            foreach ($config['zf-mvc-auth']['authentication']['adapters'] as $adapter => $adapterConfig) {
                if (! isset ($adapterConfig['adapter'])) {
                    continue;
                }
                if (false !== stristr($adapterConfig['adapter'], 'http')) {
                    if (isset($adapterConfig['options']['htpasswd'])) {
                        $authListener->addAuthenticationTypes(array($adapter . '-' . 'basic'));
                    }
                    if (isset($adapterConfig['options']['htdigest'])) {
                        $authListener->addAuthenticationTypes(array($adapter . '-' . 'digest'));
                    }
                    continue;
                }
                $authListener->addAuthenticationTypes(array($adapter));
            }
        }

        return new AuthenticationTypeController($authListener);
    }
 /**
  * Create a collection of adapters.
  *
  * @return ViewModel
  */
 private function createAdapterCollection()
 {
     $adapters = $this->authListener->getAuthenticationTypes();
     return $this->createViewModel($adapters);
 }