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
|
|
|
namespace Yandex\Passport; |
10
|
|
|
|
11
|
|
|
use GuzzleHttp\Exception\ClientException; |
12
|
|
|
use GuzzleHttp\Psr7\Response; |
13
|
|
|
use Yandex\Common\AbstractServiceClient; |
14
|
|
|
use Yandex\Common\Exception\UnauthorizedException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class PassportClient |
18
|
|
|
* |
19
|
|
|
* @category Yandex |
20
|
|
|
* @package Passport |
21
|
|
|
* |
22
|
|
|
* @author mrG1K <[email protected]> |
23
|
|
|
* @see https://tech.yandex.ru/passport/doc/dg/ |
24
|
|
|
*/ |
25
|
|
|
class PassportClient extends AbstractServiceClient |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
public $serviceDomain = 'login.yandex.ru/info'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $token |
32
|
|
|
* @return \Yandex\Passport\PassportModel |
33
|
|
|
* @throws \Yandex\Common\Exception\UnauthorizedException |
34
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
35
|
|
|
*/ |
36
|
6 |
|
public function getInfo($token) |
37
|
|
|
{ |
38
|
|
|
|
39
|
6 |
|
$response = $this->sendRequest('GET', '?json', [ |
40
|
|
|
'headers' => [ |
41
|
6 |
|
'Authorization' => 'OAuth ' . $token |
42
|
|
|
] |
43
|
|
|
]); |
44
|
|
|
|
45
|
6 |
|
$decodedResponseBody = $this->getDecodedBody($response->getBody()); |
46
|
|
|
|
47
|
6 |
|
return new PassportModel($decodedResponseBody); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sends a request |
53
|
|
|
* |
54
|
|
|
* @param string $method HTTP method |
55
|
|
|
* @param string $uri URI object or string. |
56
|
|
|
* @param array $options Request options to apply. |
57
|
|
|
* |
58
|
|
|
* @return Response |
59
|
|
|
* |
60
|
|
|
* @throws UnauthorizedException |
61
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
62
|
|
|
*/ |
63
|
|
|
protected function sendRequest($method, $uri, array $options = []) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$response = $this->getClient() |
67
|
|
|
->request($method, $uri, $options); |
68
|
|
|
} catch (ClientException $ex) { |
69
|
|
|
$result = $ex->getResponse(); |
70
|
|
|
$code = $result->getStatusCode(); |
71
|
|
|
$message = $result->getReasonPhrase(); |
72
|
|
|
|
73
|
|
|
if ($code === 401) { |
74
|
|
|
throw new UnauthorizedException($message); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $response; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|