Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
MailruOAuthService.php
1 <?php
2 /**
3  * MailRuOAuthService class file.
4  *
5  * Register application: http://api.mail.ru/sites/my/add
6  *
7  * @author ChooJoy <choojoy.work@gmail.com>
8  * @link http://code.google.com/p/yii-eauth/
9  * @license http://www.opensource.org/licenses/bsd-license.php
10  */
11 
12 require_once dirname(dirname(__FILE__)) . '/EOAuth2Service.php';
13 
14 /**
15  * Mail.Ru provider class.
16  * @package application.extensions.eauth.services
17  */
19 
20  protected $name = 'mailru';
21  protected $title = 'Mail.ru';
22  protected $type = 'OAuth';
23  protected $jsArguments = array('popup' => array('width' => 580, 'height' => 400));
24 
25  protected $client_id = '';
26  protected $client_secret = '';
27  protected $scope = '';
28  protected $providerOptions = array(
29  'authorize' => 'https://connect.mail.ru/oauth/authorize',
30  'access_token' => 'https://connect.mail.ru/oauth/token',
31  );
32 
33  protected $uid = null;
34 
35  protected function fetchAttributes() {
36  $info = (array)$this->makeSignedRequest('http://www.appsmail.ru/platform/api', array(
37  'query' => array(
38  'uids' => $this->uid,
39  'method' => 'users.getInfo',
40  'app_id' => $this->client_id,
41  ),
42  ));
43 
44  $info = $info[0];
45 
46  $this->attributes['id'] = $info->uid;
47  $this->attributes['name'] = $info->first_name.' '.$info->last_name;
48  $this->attributes['url'] = $info->link;
49  }
50 
51  protected function getCodeUrl($redirect_uri) {
52  $this->setState('redirect_uri', $redirect_uri);
53 
54  $url = parent::getCodeUrl($redirect_uri);
55  if (isset($_GET['js']))
56  $url .= '&display=popup';
57 
58  return $url;
59  }
60 
61  protected function getTokenUrl($code) {
62  return $this->providerOptions['access_token'];
63  }
64 
65  protected function getAccessToken($code) {
66  $params = array(
67  'client_id' => $this->client_id,
68  'client_secret' => $this->client_secret,
69  'grant_type' => 'authorization_code',
70  'code' => $code,
71  'redirect_uri' => $this->getState('redirect_uri'),
72  );
73  return $this->makeRequest($this->getTokenUrl($code), array('data' => $params));
74  }
75 
76  /**
77  * Save access token to the session.
78  * @param stdClass $token access token object.
79  */
80  protected function saveAccessToken($token) {
81  $this->setState('auth_token', $token->access_token);
82  $this->setState('uid', $token->x_mailru_vid);
83  $this->setState('expires', time() + $token->expires_in - 60);
84  $this->uid = $token->x_mailru_vid;
85  $this->access_token = $token->access_token;
86  }
87 
88  /**
89  * Restore access token from the session.
90  * @return boolean whether the access token was successfuly restored.
91  */
92  protected function restoreAccessToken() {
93  if ($this->hasState('uid') && parent::restoreAccessToken()) {
94  $this->uid = $this->getState('uid');
95  return true;
96  }
97  else {
98  $this->uid = null;
99  return false;
100  }
101  }
102 
103  public function makeSignedRequest($url, $options = array(), $parseJson = true) {
104  if (!$this->getIsAuthenticated())
105  throw new CHttpException(401, Yii::t('eauth', 'Unable to complete the authentication because the required data was not received.', array('{provider}' => ucfirst($this->serviceName))));
106 
107  $options['query']['secure'] = 1;
108  $options['query']['session_key'] = $this->access_token;
109  $_params = '';
110  ksort($options['query']);
111  foreach ($options['query'] as $k => $v)
112  $_params .= $k . '=' . $v;
113  $options['query']['sig'] = md5($_params . $this->client_secret);
114 
115  $result = $this->makeRequest($url, $options);
116  return $result;
117  }
118 
119  /**
120  * Returns the error info from json.
121  * @param stdClass $json the json response.
122  * @return array the error array with 2 keys: code and message. Should be null if no errors.
123  */
124  protected function fetchJsonError($json) {
125  if (isset($json->error)) {
126  return array(
127  'code' => $json->error_code,
128  'message' => $json->error_description,
129  );
130  }
131  else
132  return null;
133  }
134 }