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

Added SSL context support and minor fixes #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/AuthTypes/DigestMD5.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class DigestMD5 extends Authentication

public function encodedCredentials(): string
{
$credentials = "\x00{$this->options->getUsername()}\x00{$this->options->getPassword()}";
$credentials = $this->options->getAuthZID()."\x00";
$credentials .= $this->options->getUsername()."\x00";
$credentials .= $this->options->getPassword();

return self::quote(sha1($credentials));
}
}
5 changes: 4 additions & 1 deletion src/AuthTypes/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class Plain extends Authentication

public function encodedCredentials(): string
{
$credentials = "\x00{$this->options->getUsername()}\x00{$this->options->getPassword()}";
$credentials = $this->options->getAuthZID()."\x00";
$credentials .= $this->options->getUsername()."\x00";
$credentials .= $this->options->getPassword();

return self::quote(base64_encode($credentials));
}
}
2 changes: 1 addition & 1 deletion src/Loggers/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function error($message)

protected function writeToLog($message, $type = ''): void
{
$prefix = date("Y.m.d H:m:s") . " " . session_id() . ($type ? " {$type}::" : " ");
$prefix = date("Y.m.d H:i:s") . " " . session_id() . ($type ? " {$type}::" : " ");
$this->writeToFile($this->log, $prefix . "$message\n");
}

Expand Down
83 changes: 81 additions & 2 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class Options
* Username to authenticate on XMPP server
*/
protected $username;
/**
* Authzid
*/
protected $authzid;
/**
* Realm to be used for the JID, instead of hostname
*/
protected $realm;
/**
* Password to authenticate on XMPP server
*/
Expand All @@ -43,6 +51,18 @@ class Options
* Use TLS if available
*/
protected $useTls = true;
/**
* SSL verify host
*/
protected $ssl_verify_host = true;
/**
* SSL verify peer
*/
protected $ssl_verify_peer = true;
/**
* SSL allow self signed certificates
*/
protected $ssl_allow_self_signed = true;
/**
* Auth type (Authentication/AuthTypes/)
* @var Authenticable $authType
Expand Down Expand Up @@ -107,6 +127,30 @@ public function setUsername(string $username): Options

return $this;
}

public function setAuthZID(string $authzid): Options
{
$this->authzid = trim($authzid);

return $this;
}

public function getAuthZID()
{
return $this->authzid;
}

public function setRealm(string $realm): Options
{
$this->realm = trim($realm);

return $this;
}

public function getRealm()
{
return $this->realm;
}

public function getPassword()
{
Expand Down Expand Up @@ -151,6 +195,39 @@ public function setProtocol(string $protocol)
return $this;
}

public function getSSLVerifyHost()
{
return $this->ssl_verify_host;
}

public function setSSLVerifyHost(bool $val): Options
{
$this->ssl_verify_host = $val;
return $this;
}

public function getSSLVerifyPeer()
{
return $this->ssl_verify_peer;
}

public function setSSLVerifyPeer(bool $val): Options
{
$this->ssl_verify_peer = $val;
return $this;
}

public function getSSLAllowSelfSigned()
{
return $this->ssl_allow_self_signed;
}

public function setSSLAllowSelfSigned(bool $val): Options
{
$this->ssl_allow_self_signed = $val;
return $this;
}

public function fullSocketAddress()
{
$protocol = $this->getProtocol();
Expand All @@ -164,17 +241,19 @@ public function fullJid()
{
$username = $this->getUsername();
$resource = $this->getResource();
$realm = $this->getRealm();
$host = $this->getHost();

return "$username@$host/$resource";
return "$username@".(($realm) ? $realm : $host)."/$resource";
}

public function bareJid()
{
$username = $this->getUsername();
$realm = $this->getRealm();
$host = $this->getHost();

return "$username@$host";
return "$username@".(($realm) ? $realm : $host);
}

public function setLogger(Loggable $logger)
Expand Down
17 changes: 16 additions & 1 deletion src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ class Socket
public function __construct(Options $options)
{
$this->responseBuffer = new Response();
$this->connection = stream_socket_client($options->fullSocketAddress());

$errno = null;
$errstr = null;
$timeout = ini_get("default_socket_timeout");
$flags = STREAM_CLIENT_CONNECT;

$context = stream_context_create();

stream_context_set_option($context, 'ssl', 'verify_host', $options->getSSLVerifyHost());
stream_context_set_option($context, 'ssl', 'verify_peer', $options->getSSLVerifyPeer());
stream_context_set_option($context, 'ssl', 'allow_self_signed', $options->getSSLAllowSelfSigned());

$this->connection = stream_socket_client($options->fullSocketAddress(), $errno, $errstr, $timeout, $flags, $context);

if ($errno)
die("ERROR(".$errno."): ".$errstr."\n");

if (!$this->isAlive($this->connection)) {
throw new DeadSocket();
Expand Down
4 changes: 2 additions & 2 deletions src/Xml/Stanzas/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public function authenticate()

if ($tlsSupported && ($tlsRequired || (!$tlsRequired && $options->usingTls()))) {
$this->startTls();
$this->socket->send(self::openXmlStream($options->getHost()));
$this->socket->send(self::openXmlStream((($options->getRealm()) ? $options->getRealm() : $options->getHost())));
}

$xml = $this->generateAuthXml($options->getAuthType());
$this->socket->send($xml);
$this->socket->send(self::openXmlStream($options->getHost()));
$this->socket->send(self::openXmlStream((($options->getRealm()) ? $options->getRealm() : $options->getHost())));
}

protected function startTls()
Expand Down
2 changes: 1 addition & 1 deletion src/XmppClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function disconnect()

protected function openStream()
{
$openStreamXml = self::openXmlStream($this->options->getHost());
$openStreamXml = self::openXmlStream((($this->options->getRealm()) ? $this->options->getRealm() : $this->options->getHost()));
$this->socket->send($openStreamXml);
}

Expand Down