1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\Estimote; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use Speicher210\Estimote\Auth\Application as ApplicationAuthorization; |
8
|
|
|
|
9
|
|
|
class AuthorizationHelper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Check if an application authorization is valid. |
13
|
|
|
* |
14
|
|
|
* @param ApplicationAuthorization $applicationAuthorization The authorization code to check. |
15
|
|
|
* @return boolean |
16
|
|
|
*/ |
17
|
|
|
public function isApplicationAuthorizationValid(ApplicationAuthorization $applicationAuthorization) |
18
|
|
|
{ |
19
|
|
|
try { |
20
|
|
|
$client = new ClientAppAuth($applicationAuthorization); |
21
|
|
|
// We get the visits and filter so we get no data back. |
22
|
|
|
// We are only interested if the request is authorized or not. |
23
|
|
|
$response = $client->get( |
24
|
|
|
'analytics/visits', |
25
|
|
|
[ |
26
|
|
|
'query' => [ |
27
|
|
|
'from' => time(), |
28
|
|
|
'to' => time(), |
29
|
|
|
'granularity' => 'hourly', |
30
|
|
|
], |
31
|
|
|
] |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
return $response->getStatusCode() === 200; |
35
|
|
|
} catch (ClientException $e) { |
36
|
|
|
$response = $e->getResponse(); |
37
|
|
|
if ($response->getStatusCode() === 401 || $response->getStatusCode() === 403) { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
throw $e; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check if the username and password is valid. |
49
|
|
|
* |
50
|
|
|
* @param string $username The username. |
51
|
|
|
* @param string $password The password. |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
|
|
public function isUsernameAndPasswordValid($username, $password) |
55
|
|
|
{ |
56
|
|
|
$client = new GuzzleClient(['cookies' => true, 'allow_redirects' => true]); |
57
|
|
|
|
58
|
|
|
// Login into the portal. |
59
|
|
|
$client->post( |
60
|
|
|
'https://cloud.estimote.com/v1/login', |
61
|
|
|
[ |
62
|
|
|
'headers' => [ |
63
|
|
|
'Content-Type' => ' application/json', |
64
|
|
|
], |
65
|
|
|
'json' => array('username' => $username, 'password' => $password), |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$response = $client->get('https://cloud.estimote.com/v1/users/current'); |
70
|
|
|
|
71
|
|
|
return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function authorizeApplication($clientId, $clientSecret, $username, $password) |
75
|
|
|
{ |
76
|
|
|
$client = new GuzzleClient(['cookies' => true, 'allow_redirects' => true]); |
77
|
|
|
|
78
|
|
|
// Login into the portal |
79
|
|
|
$client->post( |
80
|
|
|
'https://cloud.estimote.com/v1/login', |
81
|
|
|
[ |
82
|
|
|
'headers' => [ |
83
|
|
|
'Content-Type' => 'application/json', |
84
|
|
|
], |
85
|
|
|
'json' => array('username' => $username, 'password' => $password), |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$url = 'https://cloud.estimote.com/v1/oauth2/client_details?response_type=code&client_id='.$clientId.'&redirect_uri=http://localhost'; |
90
|
|
|
$response = $client->get($url, ['allow_redirects' => false]); |
91
|
|
|
|
92
|
|
|
$json = \GuzzleHttp\json_decode($response->getBody(), true); |
93
|
|
|
|
94
|
|
|
$query = parse_url($json['redirect'], PHP_URL_QUERY); |
95
|
|
|
$output = array(); |
96
|
|
|
parse_str($query, $output); |
97
|
|
|
$code = $output['code']; |
98
|
|
|
|
99
|
|
|
$response = $client->post( |
100
|
|
|
'https://cloud.estimote.com/v1/oauth2/access_token', |
101
|
|
|
[ |
102
|
|
|
'headers' => ['Content-Type' => 'application/json'], |
103
|
|
|
'json' => [ |
104
|
|
|
'grant_type' => 'authorization_code', |
105
|
|
|
'code' => $code, |
106
|
|
|
'client_id' => $clientId, |
107
|
|
|
'client_secret' => $clientSecret, |
108
|
|
|
], |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$json = \GuzzleHttp\json_decode($response->getBody(), true); |
113
|
|
|
|
114
|
|
|
return $json['access_token']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getAccessToken($authorizationCode, $clientId, $clientSecret) |
118
|
|
|
{ |
119
|
|
|
$client = new GuzzleClient(); |
120
|
|
|
$response = $client->post( |
121
|
|
|
'https://cloud.estimote.com/v1/oauth2/access_token', |
122
|
|
|
[ |
123
|
|
|
'headers' => [ |
124
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
125
|
|
|
], |
126
|
|
|
'form_params' => [ |
127
|
|
|
'grant_type' => 'authorization_code', |
128
|
|
|
'code' => $authorizationCode, |
129
|
|
|
'client_id' => $clientId, |
130
|
|
|
'client_secret' => $clientSecret, |
131
|
|
|
], |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$json = \GuzzleHttp\json_decode($response->getBody(), true); |
136
|
|
|
|
137
|
|
|
return $json['access_token']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function generateApplicationForAccess($username, $password, $applicationName) |
141
|
|
|
{ |
142
|
|
|
$client = new GuzzleClient(['cookies' => true, 'allow_redirects' => true]); |
143
|
|
|
|
144
|
|
|
// Login into the portal. |
145
|
|
|
$client->post( |
146
|
|
|
'https://cloud.estimote.com/v1/login', |
147
|
|
|
[ |
148
|
|
|
'headers' => [ |
149
|
|
|
'Content-Type' => ' application/json', |
150
|
|
|
], |
151
|
|
|
'json' => array('username' => $username, 'password' => $password), |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$response = $client->post( |
156
|
|
|
'https://cloud.estimote.com/v1/applications', |
157
|
|
|
[ |
158
|
|
|
'json' => [ |
159
|
|
|
'name' => $applicationName, |
160
|
|
|
'description' => $applicationName, |
161
|
|
|
'template' => 'your-own-app', |
162
|
|
|
], |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
$json = \GuzzleHttp\json_decode($response->getBody(), true); |
167
|
|
|
|
168
|
|
|
return new ApplicationAuthorization($json['name'], $json['token']); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.