DigestMd5::challenge()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
 * Copyright 2014 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2014 Fabian Grutschus. All rights reserved.
33
 * @license   BSD
34
 * @link      http://github.com/fabiang/xmpp
35
 */
36
37
namespace Fabiang\Xmpp\EventListener\Stream\Authentication;
38
39
use Fabiang\Xmpp\EventListener\AbstractEventListener;
40
use Fabiang\Xmpp\Event\XMLEvent;
41
use Fabiang\Xmpp\Util\XML;
42
use Fabiang\Xmpp\Exception\Stream\AuthenticationErrorException;
43
44
/**
45
 * Handler for "digest md5" authentication mechanism.
46
 *
47
 * @package Xmpp\EventListener\Authentication
48
 */
49
class DigestMd5 extends AbstractEventListener implements AuthenticationInterface
50
{
51
52
    /**
53
     * Is event blocking stream.
54
     *
55
     * @var boolean
56
     */
57
    protected $blocking = false;
58
59
    /**
60
     *
61
     * @var string
62
     */
63
    protected $username;
64
65
    /**
66
     *
67
     * @var string
68
     */
69
    protected $password;
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 3
    public function attachEvents()
75
    {
76 3
        $input = $this->getInputEventManager();
77 3
        $input->attach('{urn:ietf:params:xml:ns:xmpp-sasl}challenge', [$this, 'challenge']);
78 3
        $input->attach('{urn:ietf:params:xml:ns:xmpp-sasl}success', [$this, 'success']);
79
80 3
        $output = $this->getOutputEventManager();
81 3
        $output->attach('{urn:ietf:params:xml:ns:xmpp-sasl}auth', [$this, 'auth']);
82 3
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 3
    public function authenticate($username, $password)
88
    {
89 3
        $this->setUsername($username)->setPassword($password);
90 3
        $auth = '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="DIGEST-MD5"/>';
91 3
        $this->getConnection()->send($auth);
92 3
    }
93
94
    /**
95
     * Authentication starts -> blocking.
96
     *
97
     * @return void
98
     */
99 3
    public function auth()
100
    {
101 3
        $this->blocking = true;
102 3
    }
103
104
    /**
105
     * Challenge string received.
106
     *
107
     * @param XMLEvent $event XML event
108
     * @return void
109
     */
110 9
    public function challenge(XMLEvent $event)
111
    {
112 9
        if ($event->isEndTag()) {
113 9
            list($element) = $event->getParameters();
114
115 9
            $challenge = XML::base64Decode($element->nodeValue);
116 9
            $values    = $this->parseCallenge($challenge);
117
118 9
            if (isset($values['nonce'])) {
119
                $send = '<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
120 3
                    . $this->response($values) . '</response>';
121 9
            } elseif (isset($values['rspauth'])) {
122 3
                $send = '<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>';
123 3
            } else {
124 3
                throw new AuthenticationErrorException("Error when receiving challenge: \"$challenge\"");
125
            }
126
127 6
            $this->getConnection()->send($send);
128 6
        }
129 6
    }
130
131
    /**
132
     * Generate response data.
133
     *
134
     * @param array $values
135
     */
136 3
    protected function response($values)
137
    {
138 3
        $values['cnonce'] = uniqid(mt_rand(), false);
139 3
        $values['nc']     = '00000001';
140 3
        $values['qop']    = 'auth';
141
142 3
        if (!isset($values['realm'])) {
143
            $values['realm'] = $this->getOptions()->getTo();
144
        }
145
146 3
        if (!isset($values['digest-uri'])) {
147 3
            $values['digest-uri'] = 'xmpp/' . $this->getOptions()->getTo();
148 3
        }
149
150 3
        $a1 = sprintf('%s:%s:%s', $this->getUsername(), $values['realm'], $this->getPassword());
151
152 3
        if ('md5-sess' === $values['algorithm']) {
153 3
            $a1 = pack('H32', md5($a1)) . ':' . $values['nonce'] . ':' . $values['cnonce'];
154 3
        }
155
156 3
        $a2 = "AUTHENTICATE:" . $values['digest-uri'];
157
158 3
        $password = md5($a1) . ':' . $values['nonce'] . ':' . $values['nc'] . ':'
159 3
            . $values['cnonce'] . ':' . $values['qop'] . ':' . md5($a2);
160 3
        $password = md5($password);
161
162 3
        $response = sprintf(
163 3
            'username="%s",realm="%s",nonce="%s",cnonce="%s",nc=%s,qop=%s,digest-uri="%s",response=%s,charset=utf-8',
164 3
            $this->getUsername(),
165 3
            $values['realm'],
166 3
            $values['nonce'],
167 3
            $values['cnonce'],
168 3
            $values['nc'],
169 3
            $values['qop'],
170 3
            $values['digest-uri'],
171
            $password
172 3
        );
173
174 3
        return XML::base64Encode($response);
175
    }
176
177
    /**
178
     * Parse challenge string and return its values as array.
179
     *
180
     * @param string $challenge
181
     * @return array
182
     */
183 3
    protected function parseCallenge($challenge)
184
    {
185 3
        if (!$challenge) {
186
            return [];
187
        }
188
189 3
        $matches = [];
190 3
        preg_match_all('#(\w+)\=(?:"([^"]+)"|([^,]+))#', $challenge, $matches);
191 3
        list(, $variables, $quoted, $unquoted) = $matches;
192
        // filter empty strings; preserve keys
193 3
        $quoted   = array_filter($quoted);
194 3
        $unquoted = array_filter($unquoted);
195
        // replace "unquoted" values into "quoted" array and combine variables array with it
196 3
        return array_combine($variables, array_replace($quoted, $unquoted));
197
    }
198
199
    /**
200
     * Handle success event.
201
     *
202
     * @return void
203
     */
204 3
    public function success()
205
    {
206 3
        $this->blocking = false;
207 3
    }
208
209
    /**
210
     * {@inheritDoc}
211
     */
212 3
    public function isBlocking()
213
    {
214 3
        return $this->blocking;
215
    }
216
217 3
    public function getUsername()
218
    {
219 3
        return $this->username;
220
    }
221
222 3
    public function setUsername($username)
223
    {
224 3
        $this->username = $username;
225 3
        return $this;
226
    }
227
228 3
    public function getPassword()
229
    {
230 3
        return $this->password;
231
    }
232
233 3
    public function setPassword($password)
234
    {
235 3
        $this->password = $password;
236 3
        return $this;
237
    }
238
}
239