MailruOAuth2Service.php
2.49 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
<?php
/**
* MailruOAuth2Service class file.
*
* Register application: http://api.mail.ru/sites/my/add
*
* @author Maxim Zemskov <nodge@yandex.ru>
* @link http://github.com/Nodge/yii2-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace common\components\nodge\eauth\src\services;
use nodge\eauth\oauth2\Service;
/**
* Mail.Ru provider class.
*
* @package application.extensions.eauth.services
*/
class MailruOAuth2Service extends Service
{
protected $name = 'mailru';
protected $title = 'Mail.ru';
protected $type = 'OAuth2';
protected $jsArguments = ['popup' => ['width' => 580, 'height' => 400]];
protected $scopes = [];
protected $providerOptions = [
'authorize' => 'https://connect.mail.ru/oauth/authorize',
'access_token' => 'https://connect.mail.ru/oauth/token',
];
protected $baseApiUrl = 'http://www.appsmail.ru/platform/api';
protected function fetchAttributes()
{
$tokenData = $this->getAccessTokenData();
$info = $this->makeSignedRequest('/', [
'query' => [
'uids' => $tokenData['params']['x_mailru_vid'],
'method' => 'users.getInfo',
'app_id' => $this->clientId,
],
]);
$info = $info[0];
$this->attributes['id'] = $info['uid'];
$this->attributes['name'] = $info['first_name'] . ' ' . $info['last_name'];
$this->attributes['url'] = $info['link'];
return true;
}
/**
* Returns the protected resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
*/
public function makeSignedRequest($url, $options = [], $parseResponse = true)
{
$token = $this->getAccessTokenData();
if (isset($token)) {
$options['query']['secure'] = 1;
$options['query']['session_key'] = $token['access_token'];
$params = '';
ksort($options['query']);
foreach ($options['query'] as $k => $v) {
$params .= $k . '=' . $v;
}
$options['query']['sig'] = md5($params . $this->clientSecret);
}
return parent::makeSignedRequest($url, $options, $parseResponse);
}
/**
* Returns the error array.
*
* @param array $response
* @return array the error array with 2 keys: code and message. Should be null if no errors.
*/
protected function fetchResponseError($response)
{
if (isset($response['error'])) {
return [
'code' => $response['error']['error_code'],
'message' => $response['error']['error_msg'],
];
} else {
return null;
}
}
}