GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AuthorizationHelper::authorizeApplication()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Speicher210\Estimote;
6
7
use GuzzleHttp\Client as GuzzleClient;
8
use GuzzleHttp\Exception\ClientException;
9
use Speicher210\Estimote\Auth\Application as ApplicationAuthorization;
10
11
/**
12
 * Helper class for dealing with Estimote authorization.
13
 */
14
class AuthorizationHelper
15
{
16
    /**
17
     * Check if an application authorization is valid.
18
     *
19
     * @param ApplicationAuthorization $applicationAuthorization The authorization code to check.
20
     * @return bool
21
     */
22
    public function isApplicationAuthorizationValid(ApplicationAuthorization $applicationAuthorization): bool
23
    {
24
        try {
25
            $client = new ClientAppAuth($applicationAuthorization);
26
            $response = $client->get('/v3/devices/newest_firmwares');
27
28
            return $response->getStatusCode() === 200;
29
        } catch (ClientException $e) {
30
            $response = $e->getResponse();
31
            if (\in_array($response->getStatusCode(), [401, 403], true)) {
32
                return false;
33
            }
34
35
            throw $e;
36
        }
37
    }
38
39
    /**
40
     * Check if the username and password is valid.
41
     *
42
     * @param string $username The username.
43
     * @param string $password The password.
44
     * @return bool
45
     */
46
    public function isUsernameAndPasswordValid(string $username, string $password): bool
47
    {
48
        $client = new GuzzleClient(['cookies' => true, 'allow_redirects' => true]);
49
50
        try {
51
            // Login into the portal.
52
            $client->post(
53
                'https://cloud.estimote.com/v1/login',
54
                [
55
                    'headers' => [
56
                        'Content-Type' => '	application/json'
57
                    ],
58
                    'json' => ['username' => $username, 'password' => $password]
59
                ]
60
            );
61
        } catch (ClientException $e) {
62
            $response = $e->getResponse();
63
            if (\in_array($response->getStatusCode(), [401, 403], true)) {
64
                return false;
65
            }
66
67
            throw $e;
68
        }
69
70
        $response = $client->get('https://cloud.estimote.com/v1/users/current');
71
72
        return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300;
73
    }
74
75
    public function authorizeApplication($clientId, $clientSecret, $username, $password): string
76
    {
77
        $client = new GuzzleClient(['cookies' => true, 'allow_redirects' => true]);
78
79
        // Login into the portal
80
        $client->post(
81
            'https://cloud.estimote.com/v1/login',
82
            [
83
                'headers' => [
84
                    'Content-Type' => 'application/json'
85
                ],
86
                'json' => ['username' => $username, 'password' => $password]
87
            ]
88
        );
89
90
        $url = 'https://cloud.estimote.com/v1/oauth2/client_details?response_type=code&client_id=' . $clientId . '&redirect_uri=http://localhost';
91
        $response = $client->get($url, ['allow_redirects' => false]);
92
93
        $json = \GuzzleHttp\json_decode($response->getBody(), true);
94
95
        $query = \parse_url($json['redirect'], \PHP_URL_QUERY);
96
        $output = [];
97
        \parse_str($query, $output);
98
        $code = $output['code'];
99
100
        $response = $client->post(
101
            'https://cloud.estimote.com/v1/oauth2/access_token',
102
            [
103
                'headers' => ['Content-Type' => 'application/json'],
104
                'json' => [
105
                    'grant_type' => 'authorization_code',
106
                    'code' => $code,
107
                    'client_id' => $clientId,
108
                    'client_secret' => $clientSecret
109
                ]
110
            ]
111
        );
112
113
        $json = \GuzzleHttp\json_decode($response->getBody(), true);
114
115
        return $json['access_token'];
116
    }
117
118
    public function getAccessToken($authorizationCode, $clientId, $clientSecret): string
119
    {
120
        $client = new GuzzleClient();
121
        $response = $client->post(
122
            'https://cloud.estimote.com/v1/oauth2/access_token',
123
            [
124
                'headers' => [
125
                    'Content-Type' => 'application/x-www-form-urlencoded'
126
                ],
127
                'form_params' => [
128
                    'grant_type' => 'authorization_code',
129
                    'code' => $authorizationCode,
130
                    'client_id' => $clientId,
131
                    'client_secret' => $clientSecret
132
                ]
133
            ]
134
        );
135
136
        $json = \GuzzleHttp\json_decode($response->getBody(), true);
137
138
        return $json['access_token'];
139
    }
140
141
    public function generateApplicationForAccess($username, $password, $applicationName): ApplicationAuthorization
142
    {
143
        $client = new GuzzleClient(['cookies' => true, 'allow_redirects' => true]);
144
145
        // Login into the portal.
146
        $client->post(
147
            'https://cloud.estimote.com/v1/login',
148
            [
149
                'headers' => [
150
                    'Content-Type' => '	application/json'
151
                ],
152
                'json' => ['username' => $username, 'password' => $password]
153
            ]
154
        );
155
156
        $response = $client->post(
157
            'https://cloud.estimote.com/v1/applications',
158
            [
159
                'json' => [
160
                    'name' => $applicationName,
161
                    'description' => $applicationName,
162
                    'template' => 'your-own-app'
163
                ]
164
            ]
165
        );
166
167
        $json = \GuzzleHttp\json_decode($response->getBody(), true);
168
169
        return new ApplicationAuthorization($json['name'], $json['token']);
170
    }
171
}
172