Completed
Pull Request — master (#37)
by Eugene
09:47
created

Authenticate::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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