TelegramLoginWidget::checkHash()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 24
rs 9.8333
cc 4
nc 3
nop 1
1
<?php
2
3
namespace pschocke\TelegramLoginWidget;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use pschocke\TelegramLoginWidget\Exceptions\HashValidationException;
8
use pschocke\TelegramLoginWidget\Exceptions\ResponseOutdatedException;
9
use pschocke\TelegramLoginWidget\Exceptions\TelegramException;
10
11
class TelegramLoginWidget
12
{
13
    /**
14
     * @param $response
15
     * @return bool|Collection
16
     */
17
    public function validate($response)
18
    {
19
        try {
20
            return $this->validateWithError($response);
21
        } catch (TelegramException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
22
        }
23
24
        return false;
25
    }
26
27
    /**
28
     * @param $response
29
     * @return Collection
30
     *
31
     * @throws HashValidationException
32
     * @throws ResponseOutdatedException
33
     */
34
    public function validateWithError($response): Collection
35
    {
36
        $response = $this->convertResponseToCollection($response);
37
38
        $response = $this->checkAndGetResponseData($response);
39
40
        return $this->checkHash($response);
41
    }
42
43
    /**
44
     * @param  Collection  $collection
45
     * @return Collection
46
     */
47
    private function checkAndGetResponseData(Collection $collection): Collection
48
    {
49
        $requiredAttributes = ['id', 'first_name', 'last_name', 'username', 'photo_url', 'auth_date', 'hash'];
50
51
        return $collection->only($requiredAttributes);
52
    }
53
54
    /**
55
     * @param  Collection  $collection
56
     * @return Collection
57
     *
58
     * @throws HashValidationException
59
     * @throws ResponseOutdatedException
60
     */
61
    private function checkHash(Collection $collection): Collection
62
    {
63
        $secret_key = hash('sha256', config('telegramloginwidget.bot-token'), true);
64
65
        $data = $collection->except('hash');
66
67
        $data_check_string = $data->map(function ($item, $key) {
68
            return $key.'='.$item;
69
        })
70
            ->values()
71
            ->sort()
72
            ->implode("\n");
73
74
        $hash = hash_hmac('sha256', $data_check_string, $secret_key);
75
76
        if (strcmp($hash, $collection->get('hash')) !== 0) {
77
            throw new HashValidationException;
78
        }
79
80
        if (config('telegramloginwidget.validate-auth-date') && time() - $collection->get('auth_date') > 86400) {
81
            throw new ResponseOutdatedException;
82
        }
83
84
        return $data;
85
    }
86
87
    /**
88
     * @param $response
89
     * @return Collection
90
     */
91
    private function convertResponseToCollection($response): Collection
92
    {
93
        if ($response instanceof Request) {
94
            return collect($response->all());
95
        }
96
97
        return Collection::wrap($response);
98
    }
99
}
100