예제 #1
0
 /**
  * Create an HttpAuth instance based on the configuration passed.
  *
  * @param array $config
  * @param ServiceLocatorInterface $serviceLocator
  * @return HttpAuth
  * @throws ServiceNotCreatedException if any required elements are missing
  */
 public static function factory(array $config, ServiceLocatorInterface $serviceLocator = null)
 {
     if (!isset($config['accept_schemes']) || !is_array($config['accept_schemes'])) {
         throw new ServiceNotCreatedException('"accept_schemes" is required when configuring an HTTP authentication adapter');
     }
     if (!isset($config['realm'])) {
         throw new ServiceNotCreatedException('"realm" is required when configuring an HTTP authentication adapter');
     }
     if (in_array('digest', $config['accept_schemes'])) {
         if (!isset($config['digest_domains']) || !isset($config['nonce_timeout'])) {
             throw new ServiceNotCreatedException('Both "digest_domains" and "nonce_timeout" are required ' . 'when configuring an HTTP digest authentication adapter');
         }
     }
     $httpAdapter = new HttpAuth(array_merge($config, ['accept_schemes' => implode(' ', $config['accept_schemes'])]));
     if (in_array('basic', $config['accept_schemes'])) {
         if (isset($config['basic_resolver_factory']) && self::serviceLocatorHasKey($serviceLocator, $config['basic_resolver_factory'])) {
             $httpAdapter->setBasicResolver($serviceLocator->get($config['basic_resolver_factory']));
         } elseif (isset($config['htpasswd'])) {
             $httpAdapter->setBasicResolver(new ApacheResolver($config['htpasswd']));
         }
     }
     if (in_array('digest', $config['accept_schemes'])) {
         if (isset($config['digest_resolver_factory']) && self::serviceLocatorHasKey($serviceLocator, $config['digest_resolver_factory'])) {
             $httpAdapter->setDigestResolver($serviceLocator->get($config['digest_resolver_factory']));
         } elseif (isset($config['htdigest'])) {
             $httpAdapter->setDigestResolver(new FileResolver($config['htdigest']));
         }
     }
     return $httpAdapter;
 }
 /**
  * @param ServiceLocatorInterface $services
  * @throws ServiceNotCreatedException
  * @return false|HttpAuthAdapter
  */
 public function createService(ServiceLocatorInterface $services)
 {
     // If no configuration present, nothing to create
     if (!$services->has('config')) {
         return false;
     }
     $config = $services->get('config');
     // If no HTTP adapter configuration present, nothing to create
     if (!isset($config['zf-mvc-auth']['authentication']['http'])) {
         return false;
     }
     $httpConfig = $config['zf-mvc-auth']['authentication']['http'];
     if (!isset($httpConfig['accept_schemes']) || !is_array($httpConfig['accept_schemes'])) {
         throw new ServiceNotCreatedException('"accept_schemes" is required when configuring an HTTP authentication adapter');
     }
     if (!isset($httpConfig['realm'])) {
         throw new ServiceNotCreatedException('"realm" is required when configuring an HTTP authentication adapter');
     }
     if (in_array('digest', $httpConfig['accept_schemes'])) {
         if (!isset($httpConfig['digest_domains']) || !isset($httpConfig['nonce_timeout'])) {
             throw new ServiceNotCreatedException('Both "digest_domains" and "nonce_timeout" are required when configuring an HTTP digest authentication adapter');
         }
     }
     $httpAdapter = new HttpAuth(array_merge($httpConfig, array('accept_schemes' => implode(' ', $httpConfig['accept_schemes']))));
     if (in_array('basic', $httpConfig['accept_schemes']) && isset($httpConfig['htpasswd'])) {
         $httpAdapter->setBasicResolver(new HttpAuth\ApacheResolver($httpConfig['htpasswd']));
     }
     if (in_array('digest', $httpConfig['accept_schemes']) && isset($httpConfig['htdigest'])) {
         $httpAdapter->setDigestResolver(new HttpAuth\FileResolver($httpConfig['htdigest']));
     }
     return $httpAdapter;
 }
예제 #3
0
 public function testAuthenticateReturnsAuthenticatedIdentityIfValidCredentialsProvidedInAuthorizationHeader()
 {
     $httpAuth = new HttpAuth(['accept_schemes' => 'basic', 'realm' => 'My Web Site', 'digest_domains' => '/', 'nonce_timeout' => 3600]);
     $httpAuth->setBasicResolver(new HttpAuth\ApacheResolver(__DIR__ . '/../TestAsset/htpasswd'));
     $adapter = new HttpAdapter($httpAuth, $this->authentication);
     $this->request->getHeaders()->addHeaderLine('Authorization: Basic dXNlcjp1c2Vy');
     $result = $adapter->authenticate($this->request, $this->response, $this->event);
     $this->assertInstanceOf('ZF\\MvcAuth\\Identity\\AuthenticatedIdentity', $result);
 }
 public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
 {
     $config = $container->get('Recurly\\ModuleConfig');
     $authConfig = $config['notification']['security']['authentication']['auth_adapter'];
     $authAdapter = new AuthAdapter($authConfig['config']);
     $basicResolver = new FileResolver();
     $basicResolver->setFile($authConfig['passwd_file']);
     $authAdapter->setBasicResolver($basicResolver);
     return $authAdapter;
 }
 /**
  * {@inheritdoc}
  */
 public function createService(ServiceLocatorInterface $basicServiceLocator)
 {
     if (empty($this->basicConfig)) {
         $this->basicConfig = $basicServiceLocator->get('Config');
     }
     $authBasicConfig = $this->basicConfig['authentication_basic']['adapter'];
     $authBasicAdapter = new HttpAdapter($authBasicConfig['config']);
     $basic = new FileResolver();
     $basic->setFile($authBasicConfig['basic']);
     $authBasicAdapter->setBasicResolver($basic);
     return $authBasicAdapter;
 }
 private function createBasicAuthAdapter(array $config)
 {
     if (empty($config['username']) || empty($config['password'])) {
         throw new \RuntimeException('Cannot setup Basic HTTP auth without both username and password');
     }
     $realm = isset($config['realm']) ? $config['realm'] : 'Password Required';
     $resolver = new BasicInMemoryResolver($config['username'], $config['password']);
     $options = ['realm' => $realm, 'accept_schemes' => 'basic'];
     $adapter = new BasicHttpAuth($options);
     $adapter->setBasicResolver($resolver);
     return $adapter;
 }
 /**
  * Genrate Authentication Adapter Object
  * @param ServiceLocatorInterface $serviceLocator service manager 
  * @return \Zend\Authentication\Adapter\Http
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $config = $serviceLocator->get('config');
     $authConfig = $config['book_app']['auth_adapter'];
     $basicResolver = new FileResolver();
     $basicResolver->setFile($authConfig['basic_passwd_file']);
     $digestResolver = new FileResolver();
     $digestResolver->setFile($authConfig['digest_passwd_file']);
     $authAdapter = new HttpAdapter($authConfig['config']);
     $authAdapter->setBasicResolver($basicResolver);
     $authAdapter->setDigestResolver($digestResolver);
     return $authAdapter;
 }
 public function setupHttpBasicAuth()
 {
     $httpAuth = new HttpAuth(['accept_schemes' => 'basic', 'realm' => 'My Web Site', 'digest_domains' => '/', 'nonce_timeout' => 3600]);
     $httpAuth->setBasicResolver(new HttpAuth\ApacheResolver(__DIR__ . '/../TestAsset/htpasswd'));
     $this->listener->setHttpAdapter($httpAuth);
 }
예제 #9
0
파일: ProxyTest.php 프로젝트: nieldm/zf2
 /**
  * Acts like a client sending the given Authenticate header value.
  *
  * @param  string $clientHeader Authenticate header value
  * @param  string $scheme       Which authentication scheme to use
  * @return array Containing the result, the response headers, and the status
  */
 public function _doAuth($clientHeader, $scheme)
 {
     // Set up stub request and response objects
     $response = new Response();
     $response->setStatusCode(200);
     $headers = new Headers();
     $headers->addHeaderLine('Proxy-Authorization', $clientHeader);
     $headers->addHeaderLine('User-Agent', 'PHPUnit');
     $request = new Request();
     $request->setUri('http://localhost/');
     $request->setMethod('GET');
     $request->setHeaders($headers);
     // Select an Authentication scheme
     switch ($scheme) {
         case 'basic':
             $use = $this->_basicConfig;
             break;
         case 'digest':
             $use = $this->_digestConfig;
             break;
         case 'both':
         default:
             $use = $this->_bothConfig;
     }
     // Create the HTTP Auth adapter
     $a = new \Zend\Authentication\Adapter\Http($use);
     $a->setBasicResolver($this->_basicResolver);
     $a->setDigestResolver($this->_digestResolver);
     // Send the authentication request
     $a->setRequest($request);
     $a->setResponse($response);
     $result = $a->authenticate();
     $return = array('result' => $result, 'status' => $response->getStatusCode(), 'headers' => $response->getHeaders());
     return $return;
 }
예제 #10
0
파일: ProxyTest.php 프로젝트: rexmac/zf2
 /**
  * Acts like a client sending the given Authenticate header value.
  *
  * @param  string $clientHeader Authenticate header value
  * @param  string $scheme       Which authentication scheme to use
  * @return array Containing the result, the response headers, and the status
  */
 public function _doAuth($clientHeader, $scheme)
 {
     // Set up stub request and response objects
     $request = $this->getMock('Zend\\Controller\\Request\\Http');
     $response = new HTTPResponse();
     $response->setHttpResponseCode(200);
     $response->headersSentThrowsException = false;
     // Set stub method return values
     $request->expects($this->any())->method('getRequestUri')->will($this->returnValue('/'));
     $request->expects($this->any())->method('getMethod')->will($this->returnValue('GET'));
     $request->expects($this->any())->method('getServer')->will($this->returnValue('PHPUnit'));
     $request->expects($this->any())->method('getHeader')->will($this->returnValue($clientHeader));
     // Select an Authentication scheme
     switch ($scheme) {
         case 'basic':
             $use = $this->_basicConfig;
             break;
         case 'digest':
             $use = $this->_digestConfig;
             break;
         case 'both':
         default:
             $use = $this->_bothConfig;
     }
     // Create the HTTP Auth adapter
     $a = new \Zend\Authentication\Adapter\Http($use);
     $a->setBasicResolver($this->_basicResolver);
     $a->setDigestResolver($this->_digestResolver);
     // Send the authentication request
     $a->setRequest($request);
     $a->setResponse($response);
     $result = $a->authenticate();
     $return = array('result' => $result, 'status' => $response->getHttpResponseCode(), 'headers' => $response->getHeaders());
     return $return;
 }
예제 #11
0
파일: Module.php 프로젝트: ifigenija/server
 /**
  * 
  * @param type $authService
  * @param type $em
  * @param \App\MvcEvent $e
  * @return type
  */
 public function tryHttpAuth($authService, $em, MvcEvent $e)
 {
     $resolver = new DoctrineResolver($em, '\\Aaa\\Entity\\User');
     $adapter = new Http(['realm' => 'Max', 'accept_schemes' => 'basic']);
     $adapter->setBasicResolver($resolver);
     $adapter->setRequest($e->getRequest());
     // zato da se ne pošiljajo http auth challenge nastavimo novi response, ki
     // ne vpliva na pravi response
     $adapter->setResponse(new Response());
     // shranim si doctrine adapter
     $originalAdapter = $authService->getAdapter();
     $authService->setAdapter($adapter);
     /* @var $authService AuthenticationService */
     $authResult = $authService->authenticate();
     if ($authResult->isValid()) {
         $identity = $authResult->getIdentity();
         $authService->getStorage()->write($identity);
     } else {
         $authService->setAdapter($originalAdapter);
         $identity = null;
     }
     return $identity;
 }
 public function testInvokeForBasicAuthHasNoIdentityWhenNotValid()
 {
     $httpAuth = new HttpAuth(array('accept_schemes' => 'basic', 'realm' => 'My Web Site', 'digest_domains' => '/', 'nonce_timeout' => 3600));
     $httpAuth->setBasicResolver(new HttpAuth\ApacheResolver(__DIR__ . '/../TestAsset/htpasswd'));
     $this->listener->setHttpAdapter($httpAuth);
     $this->request->getHeaders()->addHeaderLine('Authorization: Basic xxxxxxxxx');
     $this->listener->__invoke($this->mvcAuthEvent);
     $this->assertNull($this->mvcAuthEvent->getIdentity());
 }
예제 #13
0
 protected function getAuthService()
 {
     $config = array('accept_schemes' => 'basic', 'realm' => 'ref-pays-admin');
     //        if (null == $this->authService){
     $httpAuthAdapter = new Http($config);
     $authService = new AuthenticationService();
     $basicResolver = new FileResolver();
     $basicResolver->setFile(dirname(dirname(dirname(dirname(dirname(__DIR__))))) . '\\public\\files\\basicPasswd.txt');
     $httpAuthAdapter->setBasicResolver($basicResolver);
     $httpAuthAdapter->setRequest($this->getRequest());
     $httpAuthAdapter->setResponse($this->getResponse());
     $result = $httpAuthAdapter->authenticate();
     if (!$result->isValid()) {
         die(var_dump($result->getMessages()));
     }
     die('654645');
     $authService->setAdapter($httpAuthAdapter);
     $this->authService = $authService;
     //        }
     return $this->authService;
 }