1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace SocialConnect\OAuth2\Provider; |
9
|
|
|
|
10
|
|
|
use SocialConnect\Common\ArrayHydrator; |
11
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
12
|
|
|
use SocialConnect\Common\Entity\User; |
13
|
|
|
|
14
|
|
|
class Yandex extends \SocialConnect\OAuth2\AbstractProvider |
15
|
|
|
{ |
16
|
|
|
const NAME = 'yandex'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function getBaseUri() |
22
|
|
|
{ |
23
|
|
|
return 'https://login.yandex.ru/'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function getAuthorizeUri() |
30
|
|
|
{ |
31
|
|
|
return 'https://oauth.yandex.ru/authorize'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getRequestTokenUri() |
38
|
|
|
{ |
39
|
|
|
return 'https://oauth.yandex.ru/token'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function getName() |
46
|
|
|
{ |
47
|
|
|
return self::NAME; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void |
54
|
|
|
{ |
55
|
|
|
$query['format'] = 'json'; |
56
|
|
|
|
57
|
|
|
if ($accessToken) { |
58
|
|
|
$headers['Authorization'] = "OAuth {$accessToken->getToken()}"; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getIdentity(AccessTokenInterface $accessToken) |
66
|
|
|
{ |
67
|
|
|
$result = $this->request('GET', 'info', [], $accessToken); |
68
|
|
|
|
69
|
|
|
$hydrator = new ArrayHydrator([ |
70
|
|
|
'first_name' => 'firstname', |
71
|
|
|
'last_name' => 'lastname', |
72
|
|
|
'default_email' => 'email', |
73
|
|
|
'real_name' => 'fullname', |
74
|
|
|
'birthday' => static function ($value, User $user) { |
75
|
|
|
$user->setBirthday( |
76
|
|
|
new \DateTime($value) |
77
|
|
|
); |
78
|
|
|
}, |
79
|
|
|
'login' => 'username', |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
return $hydrator->hydrate(new User(), $result); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|