Completed
Push — master ( d06bab...02e31a )
by Eugene
03:46
created

AuthenticateRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 6
c 5
b 0
f 1
lcom 1
cbo 0
dl 0
loc 49
ccs 22
cts 22
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 4 1
A getBody() 0 19 2
A strxor() 0 10 2
1
<?php
2
3
namespace Tarantool\Client\Request;
4
5
use Tarantool\Client\IProto;
6
7
class AuthenticateRequest implements Request
8
{
9
    private $salt;
10
    private $username;
11
    private $password;
12
13 17
    public function __construct($salt, $username, $password = null)
14
    {
15 17
        $this->salt = $salt;
16 17
        $this->username = $username;
17 17
        $this->password = $password;
18 17
    }
19
20 17
    public function getType()
21
    {
22 17
        return self::TYPE_AUTHENTICATE;
23
    }
24
25 17
    public function getBody()
26
    {
27 17
        if (null === $this->password) {
28
            return [
29 4
                IProto::TUPLE => [],
30 4
                IProto::USER_NAME => $this->username,
31
            ];
32
        }
33
34 13
        $hash1 = sha1($this->password, true);
35 13
        $hash2 = sha1($hash1, true);
36 13
        $scramble = sha1($this->salt.$hash2, true);
37 13
        $scramble = self::strxor($hash1, $scramble);
38
39
        return [
40 13
            IProto::TUPLE => ['chap-sha1', $scramble],
41 13
            IProto::USER_NAME => $this->username,
42
        ];
43
    }
44
45 13
    private static function strxor($rhs, $lhs)
46
    {
47 13
        $result = '';
48
49 13
        for ($i = 0; $i < 20; $i++) {
50 13
            $result .= $rhs[$i] ^ $lhs[$i];
51
        }
52
53 13
        return $result;
54
    }
55
}
56