|
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\Module; |
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Xmpp\Exception\Protocol\TlsException; |
|
19
|
|
|
use Kadet\Xmpp\Network\SecureStream; |
|
20
|
|
|
use Kadet\Xmpp\Stream\Features; |
|
21
|
|
|
use Kadet\Xmpp\Xml\XmlElement; |
|
22
|
|
|
use Kadet\Xmpp\XmppClient; |
|
23
|
|
|
use Kadet\Xmpp\XmppClientModule; |
|
24
|
|
|
use Kadet\Xmpp\Utils\filter as with; |
|
25
|
|
|
|
|
26
|
|
|
class StartTls extends XmppClientModule |
|
27
|
|
|
{ |
|
28
|
|
|
public function setClient(XmppClient $client) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::setClient($client); |
|
31
|
|
|
|
|
32
|
|
|
$client->on('features', function (Features $features) { |
|
33
|
|
|
return $this->handleFeatures($features); |
|
34
|
|
|
}, null, 10); |
|
35
|
|
|
|
|
36
|
|
|
$client->on('element', function (XmlElement $element) { |
|
37
|
|
|
$this->handleTls($element); |
|
38
|
|
|
}, with\xmlns(Features\StartTls::XMLNS)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function handleFeatures(Features $features) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($features->startTls) { |
|
44
|
|
|
if ($this->_client->getDecorated() instanceof SecureStream) { |
|
45
|
|
|
$this->_client->write(new Features\StartTls()); |
|
46
|
|
|
|
|
47
|
|
|
return false; // Stop processing |
|
48
|
|
|
} elseif ($features->startTls->required) { |
|
49
|
|
|
throw new TlsException('Encryption is not available, but server requires it.'); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->_client->getLogger()->warning('Server offers TLS encryption, but stream is not capable of it.'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function handleTls(XmlElement $response) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($response->localName === 'proceed') { |
|
61
|
|
|
// this function is called only by event, which can be only fired after instanceof check |
|
62
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
63
|
|
|
$this->_client->getDecorated()->encrypt(STREAM_CRYPTO_METHOD_TLS_CLIENT); |
|
|
|
|
|
|
64
|
|
|
$this->_client->restart(); |
|
65
|
|
|
} else { |
|
66
|
|
|
throw new TlsException('TLS negotiation failed.'); // XMPP does not provide any useful information why it happened |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
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: