Passed
Pull Request — master (#37)
by Eugene
05:25
created

Authenticate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getBody() 0 10 1
A getType() 0 3 1
A strxor() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client\Request;
15
16
use Tarantool\Client\IProto;
17
use Tarantool\Client\RequestTypes;
18
19
final class Authenticate implements Request
20
{
21
    private $salt;
22
    private $username;
23
    private $password;
24
25
    public function __construct(string $salt, string $username, string $password = '')
26
    {
27
        $this->salt = $salt;
28
        $this->username = $username;
29
        $this->password = $password;
30
    }
31
32
    public function getType() : int
33
    {
34
        return RequestTypes::AUTHENTICATE;
35
    }
36
37
    public function getBody() : array
38
    {
39
        $hash1 = \sha1($this->password, true);
40
        $hash2 = \sha1($hash1, true);
41
        $scramble = \sha1($this->salt.$hash2, true);
42
        $scramble = self::strxor($hash1, $scramble);
43
44
        return [
45
            IProto::TUPLE => ['chap-sha1', $scramble],
46
            IProto::USER_NAME => $this->username,
47
        ];
48
    }
49
50
    private static function strxor(string $rhs, string $lhs) : string
51
    {
52
        $result = '';
53
54
        for ($i = 0; $i < 20; ++$i) {
55
            $result .= $rhs[$i] ^ $lhs[$i];
56
        }
57
58
        return $result;
59
    }
60
}
61