コード例 #1
0
    public function testFirstAdapterProvidingTypeIsAuthenticatedAgainst()
    {
        $map = array(
            'Foo\V2' => 'oauth2',
            'Bar\V1' => 'basic',
            'Baz\V3' => 'digest',
        );
        $this->listener->setAuthMap($map);
        $request    = new HttpRequest();
        $routeMatch = new RouteMatch(array('controller' => 'Foo\V2\Rest\Test\TestController'));
        $mvcEvent   = $this->mvcAuthEvent->getMvcEvent();
        $mvcEvent
            ->setRequest($request)
            ->setRouteMatch($routeMatch);

        $types = array('oauth2');
        $adapter1 = $this->getMockBuilder('ZF\MvcAuth\Authentication\AdapterInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $adapter1->expects($this->atLeastOnce())
            ->method('provides')
            ->will($this->returnValue($types));
        $adapter1->expects($this->any())
            ->method('matches')
            ->with($this->equalTo('oauth2'))
            ->will($this->returnValue(true));
        $adapter1->expects($this->any())
            ->method('getTypeFromRequest')
            ->with($this->equalTo($request))
            ->will($this->returnValue('oauth2'));
        $expected = $this->getMockBuilder('ZF\MvcAuth\Identity\AuthenticatedIdentity')
            ->disableOriginalConstructor()
            ->getMock();
        $adapter1->expects($this->once())
            ->method('authenticate')
            ->with($this->equalTo($request), $this->equalTo($this->response))
            ->will($this->returnValue($expected));

        $adapter2 = $this->getMockBuilder('ZF\MvcAuth\Authentication\AdapterInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $adapter2->expects($this->atLeastOnce())
            ->method('provides')
            ->will($this->returnValue($types));
        $adapter2->expects($this->any())
            ->method('getTypeFromRequest')
            ->with($this->equalTo($request))
            ->will($this->returnValue('oauth2'));

        $this->listener->attach($adapter1);
        $this->listener->attach($adapter2);

        $identity = $this->listener->__invoke($this->mvcAuthEvent);
        $this->assertSame($expected, $identity);
    }
コード例 #2
0
 /**
  * @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;
 }
コード例 #3
0
 /**
  * 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;
 }
コード例 #4
0
 /**
  * @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);
 }