1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yandex PHP Library |
4
|
|
|
* |
5
|
|
|
* @copyright NIX Solutions Ltd. |
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @namespace |
11
|
|
|
*/ |
12
|
|
|
namespace Yandex\OAuth; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Exception\ClientException; |
15
|
|
|
use GuzzleHttp\Exception\RequestException; |
16
|
|
|
use Yandex\Common\AbstractServiceClient; |
17
|
|
|
use Yandex\OAuth\Exception\AuthRequestException; |
18
|
|
|
use Yandex\OAuth\Exception\AuthResponseException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class OAuthClient implements Yandex OAuth protocol |
22
|
|
|
* |
23
|
|
|
* @category Yandex |
24
|
|
|
* @package OAuth |
25
|
|
|
* |
26
|
|
|
* @author Eugene Zabolotniy <[email protected]> |
27
|
|
|
* @created 29.08.13 12:07 |
28
|
|
|
*/ |
29
|
|
|
class OAuthClient extends AbstractServiceClient |
30
|
|
|
{ |
31
|
|
|
/* |
32
|
|
|
* Authentication types constants |
33
|
|
|
* |
34
|
|
|
* The "code" type means that the application will use an intermediate code to obtain an access token. |
35
|
|
|
* The "token" type will result a user is redirected back to the application with an access token in a URL |
36
|
|
|
*/ |
37
|
|
|
const CODE_AUTH_TYPE = 'code'; |
38
|
|
|
const TOKEN_AUTH_TYPE = 'token'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $clientId = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $clientSecret = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $serviceDomain = 'oauth.yandex.ru'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $clientId |
57
|
|
|
* @param string $clientSecret |
58
|
|
|
*/ |
59
|
25 |
|
public function __construct($clientId = '', $clientSecret = '') |
60
|
|
|
{ |
61
|
25 |
|
$this->setClientId($clientId); |
62
|
25 |
|
$this->setClientSecret($clientSecret); |
63
|
25 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $clientId |
67
|
|
|
* |
68
|
|
|
* @return self |
69
|
|
|
*/ |
70
|
25 |
|
public function setClientId($clientId) |
71
|
|
|
{ |
72
|
25 |
|
$this->clientId = $clientId; |
73
|
|
|
|
74
|
25 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
2 |
|
public function getClientId() |
81
|
|
|
{ |
82
|
2 |
|
return $this->clientId; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $clientSecret |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
25 |
|
public function setClientSecret($clientSecret) |
91
|
|
|
{ |
92
|
25 |
|
$this->clientSecret = $clientSecret; |
93
|
|
|
|
94
|
25 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
2 |
|
public function getClientSecret() |
101
|
|
|
{ |
102
|
2 |
|
return $this->clientSecret; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $type |
107
|
|
|
* @param string $state optional string |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
public function getAuthUrl($type = self::CODE_AUTH_TYPE, $state = null) |
112
|
|
|
{ |
113
|
2 |
|
$url = $this->getServiceUrl('authorize') . '?response_type=' . $type . '&client_id=' . $this->clientId; |
114
|
2 |
|
if ($state) { |
|
|
|
|
115
|
1 |
|
$url .= '&state=' . $state; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $url; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sends a redirect to the Yandex authentication page. |
123
|
|
|
* |
124
|
|
|
* @param bool $exit indicates whether to stop the PHP script immediately or not |
125
|
|
|
* @param string $type a type of the authentication procedure |
126
|
|
|
* @param string $state optional string |
127
|
|
|
* @return bool|void |
128
|
|
|
*/ |
129
|
1 |
|
public function authRedirect($exit = true, $type = self::CODE_AUTH_TYPE, $state = null) |
130
|
|
|
{ |
131
|
1 |
|
header('Location: ' . $this->getAuthUrl($type, $state)); |
132
|
|
|
|
133
|
1 |
|
return $exit ? exit() : true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Exchanges a temporary code for an access token. |
138
|
|
|
* |
139
|
|
|
* @param $code |
140
|
|
|
* |
141
|
|
|
* @throws AuthRequestException on a known request error |
142
|
|
|
* @throws AuthResponseException on a response format error |
143
|
|
|
* @throws RequestException on an unknown request error |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
6 |
|
public function requestAccessToken($code) |
148
|
|
|
{ |
149
|
6 |
|
$client = $this->getClient(); |
150
|
|
|
|
151
|
|
|
try { |
152
|
6 |
|
$response = $client->request( |
153
|
6 |
|
'POST', |
154
|
6 |
|
'/token', |
155
|
|
|
[ |
156
|
|
|
'auth' => [ |
157
|
6 |
|
$this->clientId, |
158
|
6 |
|
$this->clientSecret |
159
|
|
|
], |
160
|
|
|
'form_params' => [ |
161
|
6 |
|
'grant_type' => 'authorization_code', |
162
|
6 |
|
'code' => $code, |
163
|
6 |
|
'client_id' => $this->clientId, |
164
|
6 |
|
'client_secret' => $this->clientSecret |
165
|
|
|
] |
166
|
|
|
] |
167
|
|
|
); |
168
|
2 |
|
} catch (ClientException $ex) { |
169
|
2 |
|
$result = $this->getDecodedBody($ex->getResponse()->getBody()); |
170
|
|
|
|
171
|
2 |
|
if (is_array($result) && isset($result['error'])) { |
172
|
|
|
// handle a service error message |
173
|
1 |
|
$message = 'Service responsed with error code "' . $result['error'] . '".'; |
174
|
|
|
|
175
|
1 |
|
if (isset($result['error_description']) && $result['error_description']) { |
176
|
1 |
|
$message .= ' Description "' . $result['error_description'] . '".'; |
177
|
|
|
} |
178
|
1 |
|
throw new AuthRequestException($message, 0, $ex); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// unknown error. not parsed error |
182
|
1 |
|
throw $ex; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
try { |
186
|
4 |
|
$result = $this->getDecodedBody($response->getBody()); |
187
|
1 |
|
} catch (\RuntimeException $ex) { |
188
|
1 |
|
throw new AuthResponseException('Server response can\'t be parsed', 0, $ex); |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
if (!is_array($result)) { |
192
|
1 |
|
throw new AuthResponseException('Server response has unknown format'); |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
if (!isset($result['access_token'])) { |
196
|
1 |
|
throw new AuthResponseException('Server response doesn\'t contain access token'); |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
$this->setAccessToken($result['access_token']); |
200
|
|
|
|
201
|
1 |
|
$lifetimeInSeconds = $result['expires_in']; |
202
|
|
|
|
203
|
1 |
|
$expireDateTime = new \DateTime(); |
204
|
1 |
|
$expireDateTime->add(new \DateInterval('PT'.$lifetimeInSeconds.'S')); |
205
|
|
|
|
206
|
1 |
|
$this->setExpiresIn($expireDateTime); |
207
|
|
|
|
208
|
1 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: