-
-
Notifications
You must be signed in to change notification settings - Fork 66
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 option novalidatecert to connect(); closes #63 #94
Conversation
Signed-off-by: Johan Cwiklinski <[email protected]>
Thanks @cedric-anne :)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First off, thanks for this patch! I'm sure a number of people will appreciate having it in place, and it will make testing locally, where you might be forced to use self-signed certs, far easier!
I think you can improve this substantially by moving the various functionalities around the $novalidatecert
property and setter into the ProtocolTrait
, as well as the functionality for creating the socket options. I've provided guidance in the comments below.
Additionally, it would be great if you could figure out a way to unit test this, as the new functionality is not covered at all. While I'm reasonably certain it will be fine, a unit test ensures we don't break it in the future.
src/Protocol/Imap.php
Outdated
@@ -87,8 +104,29 @@ public function connect($host, $port = null, $ssl = false) | |||
} | |||
} | |||
|
|||
$socket_options = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use camelCase
for variable names for consistency (this is part of our coding standard).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. I've also factorized this piece of code, since this was entirely duplicated in both Imap and Pop3 classes
src/Protocol/Imap.php
Outdated
{ | ||
$this->novalidatecert = $novalidatecert; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please call $this->setNoValidateCert($novalidatecert)
to ensure you get proper validation of the value (see my notes on that method as well).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/Protocol/Pop3.php
Outdated
{ | ||
$this->novalidatecert = $novalidatecert; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please call $this->setNoValidateCert($novalidatecert)
to ensure you get proper validation of the value (see my notes on that method as well).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done aswell
src/Protocol/Pop3.php
Outdated
public function setNoValidateCert($novalidatecert) | ||
{ | ||
|
||
if (is_bool($novalidatecert)) { | ||
$this->novalidatecert = $novalidatecert; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my notes on the same method in the Imap
class.
src/Protocol/Pop3.php
Outdated
$socket_options = []; | ||
|
||
if ($this->novalidatecert) { | ||
$socket_options = [ | ||
'ssl' => [ | ||
'verify_peer_name' => false, | ||
'verify_peer' => false, | ||
] | ||
]; | ||
} | ||
|
||
$socket_context = stream_context_create($socket_options); | ||
|
||
ErrorHandler::start(); | ||
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION); | ||
$this->socket = stream_socket_client( | ||
$host . ":" . $port, | ||
$errno, | ||
$errstr, | ||
self::TIMEOUT_CONNECTION, | ||
STREAM_CLIENT_CONNECT, | ||
$socket_context | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my notes on this in the Imap
class.
Since this code is reproduced in both classes, I'd argue that you should:
- Move the
$novalidatecert
property intoProtocolTrait
- Move the
setNoValidateCert()
method intoProtocolTrait
- Create a new method in
ProtocolTrait
:
/**
* @return array
*/
private function prepareSocketOptions()
{
return $this->novalidatecert
? [
'ssl' => [
'verify_peer_name' => false,
'verify_peer' => false,
],
]
: [];
}
Then, in the connect()
method of each, you can do:
$this->socket = stream_socket_client(
$host . ':' . $port,
$errno,
$errstr,
self::TIMEOUT_CONNECTION,
STREAM_CLIENT_CONNECT,
stream_context_create($this->prepareSocketOptions())
);
(This also means the comments about camelCase
names become irrelevant, as the variables are never created.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved then entire socket preparation, and splitted options preparation to another method
Hi @weierophinney thank you for the feedbacks :) I will fix according to your review comments. |
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
Tests are failing: |
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
Signed-off-by: Johan Cwiklinski <[email protected]>
I've added a very basic test; but I do not know if it is possible to do more; and I do not really have time to investigate on that. |
@trasher let's hope @weierophinney is happy with the changes. 3️⃣ pairs of 👀 reviewed the code now. |
Signed-off-by: Johan Cwiklinski <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I've made some minor changes locally (pushing momentarily) to cover some nitpicks I had but didn't want to bother you with.
🚢
Signed-off-by: Matthew Weier O'Phinney <[email protected]>
I rebased locally and made edits... but don't have rights to push back to the origin repo. Rest assured, though - your commits are in! |
Signed-off-by: Matthew Weier O'Phinney <[email protected]>
Description
Allow connection with self signed SSL certificates; see #63; port of existing zendframework/zend-mail#247.