/** * 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; }
/** * {@inheritdoc} */ public function createService(ServiceLocatorInterface $digestServiceLocator) { if (empty($this->digestConfig)) { $this->digestConfig = $digestServiceLocator->get('Config'); } $authDigestConfig = $this->digestConfig['authentication_digest']['adapter']; $authDigestAdapter = new HttpAdapter($authDigestConfig['config']); $digest = new FileResolver(); $digest->setFile($authDigestConfig['digest']); $authDigestAdapter->setDigestResolver($digest); return $authDigestAdapter; }
/** * 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 testUnsupportedScheme() { $response = new Response(); $headers = new Headers(); $request = new Request(); $headers->addHeaderLine('Authorization', 'NotSupportedScheme <followed by a space character'); $request->setHeaders($headers); $a = new Adapter\Http($this->_digestConfig); $a->setDigestResolver($this->_digestResolver)->setRequest($request)->setResponse($response); $result = $a->authenticate(); $this->assertEquals($result->getCode(), Authentication\Result::FAILURE_UNCATEGORIZED); }
public function testInvokeForDigestAuthAddsAuthorizationHeader() { $httpAuth = new HttpAuth(['accept_schemes' => 'digest', 'realm' => 'User Area', 'digest_domains' => '/', 'nonce_timeout' => 3600]); $httpAuth->setDigestResolver(new HttpAuth\FileResolver(__DIR__ . '/../TestAsset/htdigest')); $this->listener->setHttpAdapter($httpAuth); $this->listener->__invoke($this->mvcAuthEvent); $authHeaders = $this->response->getHeaders()->get('WWW-Authenticate'); $authHeader = $authHeaders[0]; $this->assertInstanceOf('Zend\\Http\\Header\\HeaderInterface', $authHeader); $this->assertRegexp('#^Digest realm="User Area", domain="/", ' . 'nonce="[a-f0-9]{32}", ' . 'opaque="e66aa41ca5bf6992a5479102cc787bc9", ' . 'algorithm="MD5", ' . 'qop="auth"$#', $authHeader->getFieldValue()); }
/** * 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; }
/** * 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; }
public function testUnsupportedScheme() { $response = $this->getMock('Zend\\Controller\\Response\\Http'); $request = $this->getMock('Zend\\Controller\\Request\\Http'); $request->expects($this->any())->method('getHeader')->will($this->returnValue('NotSupportedScheme <followed by a space caracter')); $a = new Adapter\Http($this->_digestConfig); $a->setDigestResolver($this->_digestResolver)->setRequest($request)->setResponse($response); $result = $a->authenticate(); $this->assertEquals($result->getCode(), Authentication\Result::FAILURE_UNCATEGORIZED); }