/**
  * @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;
 }
    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);
 }
 /**
  * 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);
 }