Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add auth_type param to fix issue 1789 #1790

Merged
merged 6 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 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`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Sorry, didn't spot this before, but could you prefix it by * to make it a list? Same for line 16.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you are right
I changed it.

### Changed

### Deprecated
### Removed
### Fixed
fixed issue [1789](https://github.com/ruflin/Elastica/issues/1789)
### Security


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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the default value was CURLAUTH_ANY. I wonder if this change could break some setups?

Copy link
Contributor Author

@daalvand daalvand Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code and now the _getAuthType function by default returns CURLAUTH_ANY.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added changelogs

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