Passed
Push — master ( 528c64...1fe6c4 )
by Eugene
06:41
created

AuthenticateRequest::strxor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Request;
15
16
use Tarantool\Client\Keys;
17
use Tarantool\Client\RequestTypes;
18
19
final class AuthenticateRequest implements Request
20
{
21
    private $salt;
22
    private $username;
23
    private $password;
24
    private $scramble;
25 14
26
    public function __construct(string $salt, string $username, string $password = '')
27 14
    {
28 14
        $this->salt = $salt;
29 14
        $this->username = $username;
30 14
        $this->password = $password;
31
    }
32 14
33
    public function getType() : int
34 14
    {
35
        return RequestTypes::AUTHENTICATE;
36
    }
37 12
38
    public function getBody() : array
39 12
    {
40 12
        if (null === $this->scramble) {
41 12
            $hash1 = \sha1($this->password, true);
42 12
            $hash2 = \sha1($hash1, true);
43
            $this->scramble = $hash1 ^ \sha1($this->salt.$hash2, true);
44
        }
45 12
46 12
        return [
47
            Keys::TUPLE => ['chap-sha1', $this->scramble],
48
            Keys::USER_NAME => $this->username,
49
        ];
50 12
    }
51
}
52