Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
MoikrugOAuthService.php
1 <?php
2 /**
3  * MoikrugOAuthService class file.
4  *
5  * Register application: https://oauth.yandex.ru/client/my
6  *
7  * @author Maxim Zemskov <nodge@yandex.ru>
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  * Moikrug provider class.
16  * @package application.extensions.eauth.services
17  */
19 
20  protected $name = 'moikrug';
21  protected $title = 'Moikrug.ru';
22  protected $type = 'OAuth';
23  protected $jsArguments = array('popup' => array('width' => 500, 'height' => 450));
24 
25  protected $client_id = '';
26  protected $client_secret = '';
27  protected $scope = '';
28  protected $providerOptions = array(
29  'authorize' => 'https://oauth.yandex.ru/authorize',
30  'access_token' => 'https://oauth.yandex.ru/token',
31  );
32  protected $fields = '';
33 
34  protected function fetchAttributes() {
35  $info = (array)$this->makeSignedRequest('https://api.moikrug.ru/v1/my/');
36  $info = (array)$info[0];
37  $this->attributes['id'] = $info['id'];
38  $this->attributes['name'] = $info['name'];
39  $this->attributes['url'] = $info['link'];
40  //$this->attributes['photo'] = $info['avatar']['SnippetSquare'];
41  $this->attributes['gender'] = ($info['gender'] == 'male') ? 'M' : 'F';
42  }
43 
44  protected function getCodeUrl($redirect_uri) {
45  $url = parent::getCodeUrl($redirect_uri);
46  if (isset($_GET['js']))
47  $url .= '&display=popup';
48  return $url;
49  }
50 
51  protected function getTokenUrl($code) {
52  return $this->providerOptions['access_token'];
53  }
54 
55  protected function getAccessToken($code) {
56  $params = array(
57  'grant_type' => 'authorization_code',
58  'code' => $code,
59  'client_id' => $this->client_id,
60  'client_secret' => $this->client_secret,
61  );
62  return $this->makeRequest($this->getTokenUrl($code), array('data' => $params));
63  }
64 
65  /**
66  * Save access token to the session.
67  * @param stdClass $token access token array.
68  */
69  protected function saveAccessToken($token) {
70  $this->setState('auth_token', $token->access_token);
71  $this->setState('expires', time() + (isset($token->expires_in) ? $token->expires_in : 365*86400) - 60);
72  $this->access_token = $token->access_token;
73  }
74 
75  /**
76  * Returns the protected resource.
77  * @param string $url url to request.
78  * @param array $options HTTP request options. Keys: query, data, referer.
79  * @param boolean $parseJson Whether to parse response in json format.
80  * @return string the response.
81  * @see makeRequest
82  */
83  public function makeSignedRequest($url, $options = array(), $parseJson = true) {
84  if (!$this->getIsAuthenticated())
85  throw new CHttpException(401, 'Unable to complete the authentication because the required data was not received.');
86 
87  $options['query']['oauth_token'] = $this->access_token;
88  $result = $this->makeRequest($url, $options);
89  return $result;
90  }
91 }