Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit ba8b5c0

Browse files
authored
Merge pull request #16 from relaxedws/allow-self-signed-certificates
Make possible to avoid peer verification and allow self-signed certificates.
2 parents 5aa3a77 + 9b379ae commit ba8b5c0

File tree

6 files changed

+49
-32
lines changed

6 files changed

+49
-32
lines changed

lib/Doctrine/CouchDB/CouchDBClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public static function create(array $options)
113113
'password' => null,
114114
'ip' => null,
115115
'ssl' => false,
116+
'verify' => true,
116117
'path' => null,
117118
'logging' => false,
118119
'timeout' => 10,
@@ -133,6 +134,7 @@ public static function create(array $options)
133134
$options['password'],
134135
$options['ip'],
135136
$options['ssl'],
137+
$options['verify'],
136138
$options['path'],
137139
$options['timeout'],
138140
$options['headers']

lib/Doctrine/CouchDB/HTTP/AbstractHTTPClient.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class AbstractHTTPClient implements Client
2727
'port' => 5984,
2828
'ip' => '127.0.0.1',
2929
'ssl' => false,
30+
'verify' => true,
3031
'timeout' => 10,
3132
'keep-alive' => true,
3233
'username' => null,
@@ -47,17 +48,17 @@ abstract class AbstractHTTPClient implements Client
4748
* @param string $password
4849
* @param string $ip
4950
* @param bool $ssl
51+
* @param bool $verify
5052
* @param string $path
5153
* @param int $timeout
5254
* @param array $headers
53-
*
54-
* @return \Doctrine\CouchDB\HTTP\AbstractHTTPClient
5555
*/
56-
public function __construct($host = 'localhost', $port = 5984, $username = null, $password = null, $ip = null, $ssl = false, $path = null, $timeout = 10, array $headers = [])
56+
public function __construct($host = 'localhost', $port = 5984, $username = null, $password = null, $ip = null, $ssl = false, $verify = true, $path = null, $timeout = 10, array $headers = [])
5757
{
5858
$this->options['host'] = (string) $host;
5959
$this->options['port'] = (int) $port;
6060
$this->options['ssl'] = $ssl;
61+
$this->options['verify'] = $verify;
6162
$this->options['username'] = $username;
6263
$this->options['password'] = $password;
6364
$this->options['path'] = $path;
@@ -89,6 +90,7 @@ public function setOption($option, $value)
8990
switch ($option) {
9091
case 'keep-alive':
9192
case 'ssl':
93+
case 'verify':
9294
$this->options[$option] = (bool) $value;
9395
break;
9496

lib/Doctrine/CouchDB/HTTP/MultipartParserAndSender.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function __construct(AbstractHTTPClient $source, AbstractHTTPClient $targ
4444
$sourceOptions['password'],
4545
$sourceOptions['ip'],
4646
$sourceOptions['ssl'],
47+
$sourceOptions['verify'],
4748
$sourceOptions['path'],
4849
$sourceOptions['timeout'],
4950
$sourceOptions['headers']
@@ -57,6 +58,7 @@ public function __construct(AbstractHTTPClient $source, AbstractHTTPClient $targ
5758
$targetOptions['password'],
5859
$targetOptions['ip'],
5960
$targetOptions['ssl'],
61+
$targetOptions['verify'],
6062
$targetOptions['path'],
6163
$targetOptions['timeout'],
6264
$sourceOptions['headers']

lib/Doctrine/CouchDB/HTTP/SocketClient.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function getConnection(
7575
protected function checkConnection()
7676
{
7777
// Setting Connection scheme according ssl support
78+
$context_options = null;
7879
if ($this->options['ssl']) {
7980
if (!extension_loaded('openssl')) {
8081
// no openssl extension loaded.
@@ -88,24 +89,30 @@ protected function checkConnection()
8889
0
8990
);
9091
}
91-
92-
$host = 'ssl://'.$this->options['host'];
92+
$host = 'ssl://'.$this->options['host'].':'.$this->options['port'];
93+
if ($this->options['verify'] === false) {
94+
$context_options = [
95+
'ssl' => [
96+
'verify_peer' => false,
97+
],
98+
];
99+
}
93100
} else {
94-
$host = $this->options['ip'];
101+
$host = $this->options['ip'].':'.$this->options['port'];
95102
}
96103

97-
// If the connection could not be established, fsockopen sadly does not
98-
// only return false (as documented), but also always issues a warning.
99-
if (($this->connection === null) &&
100-
(($this->connection = @fsockopen($host, $this->options['port'], $errno, $errstr, $this->options['timeout'])) === false)) {
101-
// This is a bit hackisch...
102-
$this->connection = null;
103-
throw HTTPException::connectionFailure(
104-
$this->options['ip'],
105-
$this->options['port'],
106-
$errstr,
107-
$errno
108-
);
104+
// Try to establish the connection.
105+
if ($this->connection === null) {
106+
$context = stream_context_create($context_options);
107+
if (($this->connection = @stream_socket_client($host, $errno, $errstr, $this->options['timeout'], STREAM_CLIENT_CONNECT, $context)) === false) {
108+
$this->connection = null;
109+
throw HTTPException::connectionFailure(
110+
$this->options['ip'],
111+
$this->options['port'],
112+
$errstr,
113+
$errno
114+
);
115+
}
109116
}
110117
}
111118

lib/Doctrine/CouchDB/HTTP/StreamClient.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,26 @@ protected function checkConnection($method, $path, $data, $headers)
8989
}
9090
// Determine the correct scheme so SSL is handled too.
9191
$scheme = !empty($this->options['ssl']) ? 'https' : 'http';
92-
92+
$context_options = [
93+
'http' => [
94+
'method' => $method,
95+
'content' => $data,
96+
'ignore_errors' => true,
97+
'max_redirects' => 0,
98+
'user_agent' => 'Doctrine CouchDB ODM $Revision$',
99+
'timeout' => $this->options['timeout'],
100+
'header' => $stringHeader,
101+
],
102+
];
103+
if ($scheme === 'https' && ($this->options['verify'] === false)) {
104+
$context_options['ssl'] = ['verify_peer' => false];
105+
}
106+
$context = stream_context_create($context_options);
93107
$this->httpFilePointer = @fopen(
94108
$scheme . '://' . $basicAuth . $host . $path,
95109
'r',
96110
false,
97-
stream_context_create(
98-
[
99-
'http' => [
100-
'method' => $method,
101-
'content' => $data,
102-
'ignore_errors' => true,
103-
'max_redirects' => 0,
104-
'user_agent' => 'Doctrine CouchDB ODM $Revision$',
105-
'timeout' => $this->options['timeout'],
106-
'header' => $stringHeader,
107-
],
108-
]
109-
)
111+
$context
110112
);
111113
}
112114

tests/Doctrine/Tests/CouchDB/CouchDBClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testCreateClientFromUrl()
3636
'username' => 'foo',
3737
'password' => 'bar',
3838
'ssl' => true,
39+
'verify' => true,
3940
'timeout' => 10,
4041
'keep-alive' => true,
4142
'path' => null,
@@ -58,6 +59,7 @@ public function testCreateClientFromUrlWithPath()
5859
'username' => 'foo',
5960
'password' => 'bar',
6061
'ssl' => true,
62+
'verify' => true,
6163
'timeout' => 10,
6264
'keep-alive' => true,
6365
'path' => 'baz/qux',

0 commit comments

Comments
 (0)