Skip to content

Commit

Permalink
add auth_type param to fix issue 1789 (#1790)
Browse files Browse the repository at this point in the history
I added   auth_type option to resolve the problem of duplicate requests in [1789 issue](#1789 "1789 issue")
```php
        'auth_type' => null //basic, digest, gssnegotiate, ntlm
```
  • Loading branch information
daalvand authored Aug 6, 2020
1 parent fe45203 commit b43380b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

/**
Expand Down Expand Up @@ -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']);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
22 changes: 21 additions & 1 deletion src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}

Expand Down Expand Up @@ -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;
}
}
}
5 changes: 5 additions & 0 deletions tests/ClientConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testFromSimpleDsn(): void
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null,
];

$this->assertEquals($expected, $configuration->getAll());
Expand All @@ -62,6 +63,7 @@ public function testFromDsnWithParameters(): void
'bigintConversion' => true,
'username' => 'user',
'password' => 'p4ss',
'auth_type' => 'basic',
'extra' => 'abc',
];

Expand All @@ -87,6 +89,7 @@ public function testFromEmptyArray(): void
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null,
];

$this->assertEquals($expected, $configuration->getAll());
Expand Down Expand Up @@ -114,6 +117,7 @@ public function testFromArray(): void
'bigintConversion' => false,
'username' => 'Jdoe',
'password' => null,
'auth_type' => null,
'extra' => 'abc',
];

Expand Down Expand Up @@ -147,6 +151,7 @@ public function testGet(): void
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null,
];

$this->assertEquals($expected, $configuration->get(''));
Expand Down
1 change: 1 addition & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function testConstructWithDsn(): void
'bigintConversion' => false,
'username' => 'user',
'password' => 'p4ss',
'auth_type' => 'basic',
'connectionStrategy' => 'Simple',
];

Expand Down

0 comments on commit b43380b

Please sign in to comment.