-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathMailRu.php
More file actions
130 lines (109 loc) · 3.09 KB
/
MailRu.php
File metadata and controls
130 lines (109 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
declare(strict_types=1);
namespace SocialConnect\OAuth2\Provider;
use SocialConnect\Common\ArrayHydrator;
use SocialConnect\Provider\AccessTokenInterface;
use SocialConnect\Provider\Exception\InvalidAccessToken;
use SocialConnect\OAuth2\AccessToken;
use SocialConnect\Common\Entity\User;
class MailRu extends \SocialConnect\OAuth2\AbstractProvider
{
const NAME = 'mail-ru';
/**
* {@inheritdoc}
*/
public function getBaseUri()
{
return 'https://appsmail.ru/platform/api';
}
/**
* {@inheritdoc}
*/
public function getAuthorizeUri()
{
return 'https://connect.mail.ru/oauth/authorize';
}
/**
* {@inheritdoc}
*/
public function getRequestTokenUri()
{
return 'https://connect.mail.ru/oauth/token';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return self::NAME;
}
/**
* {@inheritdoc}
*/
public function parseToken(string $body)
{
if (empty($body)) {
throw new InvalidAccessToken('Provider response with empty body');
}
$result = json_decode($body, true);
if ($result) {
$token = new AccessToken($result);
$token->setUserId((string) $result['x_mailru_vid']);
return $token;
}
throw new InvalidAccessToken('Provider response with not valid JSON');
}
/**
* Copy/pasted from MailRU examples :)
*
* @param array $requestParameters
* @return string
*/
protected function makeSecureSignature(array $requestParameters)
{
ksort($requestParameters);
$params = '';
foreach ($requestParameters as $key => $value) {
$params .= "$key=$value";
}
return md5($params . $this->consumer->getSecret());
}
/**
* {@inheritDoc}
*/
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
{
$query['client_id'] = $this->consumer->getKey();
$query['format'] = 'json';
$query['secure'] = 1;
if ($accessToken) {
$query['session_key'] = $accessToken->getToken();
$query['sig'] = $this->makeSecureSignature($query);
}
}
/**
* {@inheritdoc}
*/
public function getIdentity(AccessTokenInterface $accessToken)
{
$query = [
'method' => 'users.getInfo',
];
$response = $this->request('GET', '', $query, $accessToken);
$hydrator = new ArrayHydrator([
'uid' => 'id',
'first_name' => 'firstname',
'last_name' => 'lastname',
'nick' => 'username',
'pic_big' => 'pictureURL',
'sex' => static function ($value, User $user) {
$user->setGender($value === 1 ? User::GENDER_FEMALE : User::GENDER_MALE);
}
]);
return $hydrator->hydrate(new User(), $response[0]);
}
}