-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMailRu.php
115 lines (99 loc) · 2.54 KB
/
MailRu.php
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
<?php
/**
* Project: yii2-mailru-authclient
* Version: v1
* User: isudakoff
* Date: 19.05.17
* Time: 10:30
*/
namespace isudakoff\authclient;
use yii\authclient\InvalidResponseException;
use yii\authclient\OAuth2;
/**
* In order to use Mail.ru OAuth you must register your application at <http://api.mail.ru/sites/my/add>.
*
* @see http://api.mail.ru/sites/my/add/
* @see http://api.mail.ru/sites/my/
* @see http://api.mail.ru/docs/reference/rest/users-getinfo/
*
* @author Ilya Sudakov <[email protected]>
*/
class MailRu extends OAuth2
{
/**
* @inheritdoc
*/
public $authUrl = 'https://connect.mail.ru/oauth/authorize';
/**
* @inheritdoc
*/
public $tokenUrl = 'https://connect.mail.ru/oauth/token';
/**
* @inheritdoc
*/
public $apiBaseUrl = 'http://www.appsmail.ru/platform/api?method=';
/**
* @inheritdoc
*/
protected function initUserAttributes()
{
$request = $this->createApiRequest()->setMethod('GET')->setUrl('users.getInfo');
$response = $request->send();
$response->setFormat('json');
if ($response->isOk && $response->data && $response->data['0']) {
return $response->data['0'];
}
throw new InvalidResponseException($response);
}
/**
* @inheritdoc
*/
public function applyAccessTokenToRequest($request, $accessToken)
{
parent::applyAccessTokenToRequest($request, $accessToken);
$data = $request->getData();
$data['method'] = str_replace('/', '', $request->getUrl());
$data['uids'] = $accessToken->getParam('x_mailru_vid');
$data['app_id'] = $this->clientId;
$data['secure'] = 1;
$data['sig'] = $this->sig($data, $this->clientSecret);
$request->setUrl('');
$request->setData($data);
}
/**
* Generate signature for API mail.ru
*
* @return string
*/
public function sig(array $request_params, $secret_key) {
ksort($request_params);
$params = '';
foreach ($request_params as $key => $value) {
$params .= "$key=$value";
}
return md5($params . $secret_key);
}
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'mailru';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'MailRu';
}
/**
* @inheritdoc
*/
protected function defaultNormalizeUserAttributeMap()
{
return [
'id' => 'uid'
];
}
}