diff --git a/src/AuthTypes/DigestMD5.php b/src/AuthTypes/DigestMD5.php index 900101b..324ab27 100644 --- a/src/AuthTypes/DigestMD5.php +++ b/src/AuthTypes/DigestMD5.php @@ -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)); } } diff --git a/src/AuthTypes/Plain.php b/src/AuthTypes/Plain.php index b87d658..bd52336 100644 --- a/src/AuthTypes/Plain.php +++ b/src/AuthTypes/Plain.php @@ -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)); } } diff --git a/src/Loggers/Logger.php b/src/Loggers/Logger.php index f1dcb9d..09c80e0 100644 --- a/src/Loggers/Logger.php +++ b/src/Loggers/Logger.php @@ -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"); } diff --git a/src/Options.php b/src/Options.php index 670afb7..67eeb47 100644 --- a/src/Options.php +++ b/src/Options.php @@ -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 */ @@ -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 @@ -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() { @@ -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(); @@ -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) diff --git a/src/Socket.php b/src/Socket.php index 4852fd0..bd4dc4c 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -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(); diff --git a/src/Xml/Stanzas/Auth.php b/src/Xml/Stanzas/Auth.php index a6df778..211a823 100644 --- a/src/Xml/Stanzas/Auth.php +++ b/src/Xml/Stanzas/Auth.php @@ -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() diff --git a/src/XmppClient.php b/src/XmppClient.php index 54cc7c1..41a1521 100644 --- a/src/XmppClient.php +++ b/src/XmppClient.php @@ -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); }