|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* XMPP Library |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Contact with author: |
|
10
|
|
|
* Xmpp: [email protected] |
|
11
|
|
|
* E-mail: [email protected] |
|
12
|
|
|
* |
|
13
|
|
|
* From Kadet with love. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp; |
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Xmpp\Exception\Protocol\TlsException; |
|
19
|
|
|
use Kadet\Xmpp\Network\SecureStream; |
|
20
|
|
|
use Kadet\Xmpp\Stream\Error; |
|
21
|
|
|
use Kadet\Xmpp\Stream\Features; |
|
22
|
|
|
use Kadet\Xmpp\Xml\XmlElement; |
|
23
|
|
|
use Kadet\Xmpp\Xml\XmlParser; |
|
24
|
|
|
use Kadet\Xmpp\Xml\XmlStream; |
|
25
|
|
|
use React\Stream\DuplexStreamInterface; |
|
26
|
|
|
use Kadet\Xmpp\Utils\filter as with; |
|
27
|
|
|
|
|
28
|
|
|
class XmppStream extends XmlStream |
|
29
|
|
|
{ |
|
30
|
|
|
const TLS_NAMESPACE = 'urn:ietf:params:xml:ns:xmpp-tls'; |
|
31
|
|
|
|
|
32
|
|
|
private $_attributes = []; |
|
33
|
|
|
private $_lang; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(XmlParser $parser, DuplexStreamInterface $transport, string $lang = 'en') |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct($parser, $transport); |
|
38
|
|
|
|
|
39
|
|
|
$this->parser->factory->register(Features::class, self::NAMESPACE_URI, 'features'); |
|
40
|
|
|
$this->parser->factory->register(Error::class, self::NAMESPACE_URI, 'error'); |
|
41
|
|
|
|
|
42
|
|
|
$this->_lang = $lang; |
|
43
|
|
|
|
|
44
|
|
|
$this->on('element', function (Features $element) { |
|
45
|
|
|
$this->handleFeatures($element); |
|
46
|
|
|
}, Features::class); |
|
47
|
|
|
|
|
48
|
|
|
$this->on('element', function (XmlElement $element) { |
|
49
|
|
|
$this->handleTls($element); |
|
50
|
|
|
}, with\xmlns(self::TLS_NAMESPACE)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function start(array $attributes = []) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->_attributes = $attributes; |
|
56
|
|
|
|
|
57
|
|
|
parent::start(array_merge([ |
|
58
|
|
|
'xmlns' => 'jabber:client', |
|
59
|
|
|
'version' => '1.0', |
|
60
|
|
|
'xml:lang' => $this->_lang |
|
61
|
|
|
], $attributes)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function restart() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->start($this->_attributes); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function handleFeatures(Features $element) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($element->startTls >= Features::TLS_AVAILABLE) { |
|
72
|
|
|
if ($this->decorated instanceof SecureStream) { |
|
73
|
|
|
$this->write(XmlElement::create('starttls', null, self::TLS_NAMESPACE)); |
|
74
|
|
|
|
|
75
|
|
|
return; // Stop processing |
|
76
|
|
|
} elseif ($element->startTls === Features::TLS_REQUIRED) { |
|
77
|
|
|
throw new TlsException('Encryption is not available, but server requires it.'); |
|
78
|
|
|
} else { |
|
79
|
|
|
$this->getLogger()->warning('Server offers TLS encryption, but stream is not capable of it.'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function handleTls(XmlElement $response) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($response->localName === 'proceed') { |
|
87
|
|
|
// this function is called only by event, which can be only fired after instanceof check |
|
88
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
89
|
|
|
$this->decorated->encrypt(STREAM_CRYPTO_METHOD_TLS_CLIENT); |
|
|
|
|
|
|
90
|
|
|
$this->restart(); |
|
91
|
|
|
} else { |
|
92
|
|
|
throw new TlsException('TLS negotiation failed.'); // XMPP does not provide any useful information why it happened |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: