-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSaslAuthenticator.php
169 lines (139 loc) · 4.88 KB
/
SaslAuthenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2016, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\Xmpp\Component;
use Fabiang\Sasl\Authentication\AuthenticationInterface;
use Fabiang\Sasl\Authentication\ChallengeAuthenticationInterface;
use Fabiang\Sasl\Exception\InvalidArgumentException;
use Fabiang\Sasl\Sasl;
use Kadet\Xmpp\Exception\Protocol\AuthenticationException;
use Kadet\Xmpp\Sasl\SaslFactory;
use Kadet\Xmpp\Stream\Features;
use Kadet\Xmpp\Xml\XmlElement;
use Kadet\Xmpp\XmppClient;
use Kadet\Xmpp\Utils\filter as with;
use function Kadet\Xmpp\Utils\filter\{
all, in
};
class SaslAuthenticator extends Component implements Authenticator
{
const XMLNS = 'urn:ietf:params:xml:ns:xmpp-sasl';
/**
* Client's password used in authorisation.
*
* @var string
*/
private $_password;
/**
* Factory used to create mechanisms
*
* @var Sasl
*/
private $_sasl;
/**
* Authentication constructor.
*
* @param string $password Client's password
* @param Sasl $sasl Factory used to create mechanisms
*/
public function __construct($password = null, Sasl $sasl = null)
{
$this->setPassword($password);
$this->_sasl = $sasl ?: new SaslFactory();
}
public function setClient(XmppClient $client)
{
parent::setClient($client);
$client->on('features', function (Features $features) {
return !$this->auth($features);
});
}
public function auth(Features $features)
{
if (!empty($features->mechanisms)) {
foreach ($features->mechanisms as $name) {
if($this->tryMechanism($name)) {
return true;
}
}
}
return false;
}
private function tryMechanism($name) {
try {
$mechanism = $this->_sasl->factory($name, [
'authcid' => $this->_client->jid->local,
'secret' => $this->_password,
'hostname' => $this->_client->jid->domain,
'service' => 'xmpp'
]);
$this->_client->getLogger()->debug('Starting auth using {name} mechanism.', ['name' => $name]);
$auth = new XmlElement('auth', self::XMLNS);
$auth->setAttribute('mechanism', $name);
$response = $mechanism instanceof ChallengeAuthenticationInterface
? $this->mechanismWithChallenge($mechanism)
: $this->mechanismWithoutChallenge($mechanism);
$auth->append($response ? base64_encode($response) : '=');
$this->_client->write($auth);
return true;
} catch (InvalidArgumentException $e) {
return false;
}
}
private function mechanismWithChallenge(ChallengeAuthenticationInterface $mechanism) {
try {
$response = $mechanism->createResponse();
} catch (InvalidArgumentException $e) {
$response = '';
}
$callback = $this->_client->on('element', function (XmlElement $challenge) use ($mechanism) {
$this->handleChallenge($challenge, $mechanism);
}, with\element('challenge', self::XMLNS));
$this->_client->once('element', function (XmlElement $result) use ($callback) {
$this->_client->removeListener('element', $callback);
$this->handleAuthResult($result);
}, $this->_resultPredicate());
return $response;
}
private function mechanismWithoutChallenge(AuthenticationInterface $mechanism)
{
$this->_client->once('element', function (XmlElement $result) {
$this->handleAuthResult($result);
}, $this->_resultPredicate());
return $mechanism->createResponse();
}
private function handleChallenge(XmlElement $challenge, AuthenticationInterface $mechanism)
{
$response = new XmlElement('response', self::XMLNS);
$response->append(base64_encode($mechanism->createResponse(base64_decode($challenge->innerXml))));
$this->_client->write($response);
}
private function handleAuthResult(XmlElement $result)
{
// todo: handle different scenarios
if ($result->localName === 'failure') {
throw new AuthenticationException('Unable to auth. '.trim($result->innerXml));
}
$this->_client->getLogger()->info('Successfully authorized as {name}.', ['name' => (string)$this->_client->jid]);
$this->_client->restart();
}
public function setPassword(string $password = null)
{
$this->_password = $password;
}
private function _resultPredicate()
{
return all(with\element\name(in('success', 'failure')), with\element\xmlns(self::XMLNS));
}
}