From b43380b17829389574a31de77ba50749b444b00f Mon Sep 17 00:00:00 2001 From: Mehdi Daalvand <23280949+daalvand@users.noreply.github.com> Date: Thu, 6 Aug 2020 13:26:58 +0430 Subject: [PATCH] add auth_type param to fix issue 1789 (#1790) I added auth_type option to resolve the problem of duplicate requests in [1789 issue](https://github.com/ruflin/Elastica/issues/1789 "1789 issue") ```php 'auth_type' => null //basic, digest, gssnegotiate, ntlm ``` --- CHANGELOG.md | 5 +++-- src/ClientConfiguration.php | 7 ++++++- src/Connection.php | 8 ++++++++ src/Transport/Http.php | 22 +++++++++++++++++++++- tests/ClientConfigurationTest.php | 5 +++++ tests/ClientTest.php | 1 + 6 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7d71db0f6..389c0ec623 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/ruflin/Elastica/compare/7.0.0...master) ### Backward Compatibility Breaks ### Added - +* Ability to specify the type of authentication manually by the `auth_type` parameter (in the client class config) was added (allowed values are `basic, digest, gssnegotiate, ntlm`) ### Changed ### Deprecated ### Removed ### Fixed +* fixed issue [1789](https://github.com/ruflin/Elastica/issues/1789) ### Security @@ -32,7 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * The `Terms` Query's constructor now requires the `field` and `terms` properties. ### Added -* Added `AbstractTermsAggregation::setIncludeAsExactMatch()` [#1766](https://github.com/ruflin/Elastica/pull/1766) +* Added `AbstractTermsAggregation::setIncludeAsExactMatch()` [#1766](https://github.com/ruflin/Elastica/pull/1766) * Added `AbstractTermsAggregation::setExcludeAsExactMatch()` [#1766](https://github.com/ruflin/Elastica/pull/1766) * Added `AbstractTermsAggregation::setIncludeWithPartitions()` [#1766](https://github.com/ruflin/Elastica/pull/1766) * Added `Elastica\Reindex->setPipeline(Elastica\Pipeline $pipeline): void`. The link between the reindex and the pipeline is solved when `run()` is called, and thus the pipeline given doesn't need to be created before calling `setPipeline()` [#1752](https://github.com/ruflin/Elastica/pull/1752) diff --git a/src/ClientConfiguration.php b/src/ClientConfiguration.php index ad149eaca7..64bf20ed5c 100644 --- a/src/ClientConfiguration.php +++ b/src/ClientConfiguration.php @@ -28,12 +28,13 @@ class ClientConfiguration 'transport' => null, 'persistent' => true, 'timeout' => null, - 'connections' => [], // host, port, path, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url) + 'connections' => [], // host, port, path, transport, compression, persistent, timeout, username, password, auth_type, config -> (curl, headers, url) 'roundRobin' => false, 'retryOnConflict' => 0, 'bigintConversion' => false, 'username' => null, 'password' => null, + 'auth_type' => null, //basic, digest, gssnegotiate, ntlm ]; /** @@ -82,6 +83,10 @@ public static function fromDsn(string $dsn): self $clientConfiguration->set('password', \urldecode($parsedDsn['pass'])); } + if (isset($parsedDsn['pass'], $parsedDsn['user'])) { + $clientConfiguration->set('auth_type', 'basic'); + } + if (isset($parsedDsn['port'])) { $clientConfiguration->set('port', $parsedDsn['port']); } diff --git a/src/Connection.php b/src/Connection.php index 3a155c4cf7..2fa21805ac 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -355,4 +355,12 @@ public function getPassword() { return $this->hasParam('password') ? $this->getParam('password') : null; } + + /** + * @return string AuthType + */ + public function getAuthType() + { + return $this->hasParam('auth_type') ? \strtolower($this->getParam('auth_type')) : null; + } } diff --git a/src/Transport/Http.php b/src/Transport/Http.php index 32f2f58208..183efde5d2 100644 --- a/src/Transport/Http.php +++ b/src/Transport/Http.php @@ -104,7 +104,7 @@ public function exec(Request $request, array $params): Response $username = $connection->getUsername(); $password = $connection->getPassword(); if (null !== $username && null !== $password) { - \curl_setopt($conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + \curl_setopt($conn, CURLOPT_HTTPAUTH, $this->_getAuthType()); \curl_setopt($conn, CURLOPT_USERPWD, "{$username}:{$password}"); } @@ -222,4 +222,24 @@ protected function _getConnection(bool $persistent = true) return self::$_curlConnection; } + + protected function _getAuthType() + { + switch ($this->_connection->getAuthType()) { + case 'digest': + return CURLAUTH_DIGEST; + break; + case 'gssnegotiate': + return CURLAUTH_GSSNEGOTIATE; + break; + case 'ntlm': + return CURLAUTH_NTLM; + break; + case 'basic': + return CURLAUTH_BASIC; + break; + default: + return CURLAUTH_ANY; + } + } } diff --git a/tests/ClientConfigurationTest.php b/tests/ClientConfigurationTest.php index 44d7459d9a..013612a7c2 100644 --- a/tests/ClientConfigurationTest.php +++ b/tests/ClientConfigurationTest.php @@ -39,6 +39,7 @@ public function testFromSimpleDsn(): void 'bigintConversion' => false, 'username' => null, 'password' => null, + 'auth_type' => null, ]; $this->assertEquals($expected, $configuration->getAll()); @@ -62,6 +63,7 @@ public function testFromDsnWithParameters(): void 'bigintConversion' => true, 'username' => 'user', 'password' => 'p4ss', + 'auth_type' => 'basic', 'extra' => 'abc', ]; @@ -87,6 +89,7 @@ public function testFromEmptyArray(): void 'bigintConversion' => false, 'username' => null, 'password' => null, + 'auth_type' => null, ]; $this->assertEquals($expected, $configuration->getAll()); @@ -114,6 +117,7 @@ public function testFromArray(): void 'bigintConversion' => false, 'username' => 'Jdoe', 'password' => null, + 'auth_type' => null, 'extra' => 'abc', ]; @@ -147,6 +151,7 @@ public function testGet(): void 'bigintConversion' => false, 'username' => null, 'password' => null, + 'auth_type' => null, ]; $this->assertEquals($expected, $configuration->get('')); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 20ff7baa3f..fde5378394 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -40,6 +40,7 @@ public function testConstructWithDsn(): void 'bigintConversion' => false, 'username' => 'user', 'password' => 'p4ss', + 'auth_type' => 'basic', 'connectionStrategy' => 'Simple', ];