Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
GoogleOAuthService.php
1 <?php
2 /**
3  * GoogleOAuthService class file.
4  *
5  * Register application: https://code.google.com/apis/console/
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  * Google provider class.
16  * @package application.extensions.eauth.services
17  */
19 
20  protected $name = 'google_oauth';
21  protected $title = 'Google';
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 = 'https://www.googleapis.com/auth/userinfo.profile';
28  protected $providerOptions = array(
29  'authorize' => 'https://accounts.google.com/o/oauth2/auth',
30  'access_token' => 'https://accounts.google.com/o/oauth2/token',
31  );
32 
33  protected function fetchAttributes() {
34  $info = (array)$this->makeSignedRequest('https://www.googleapis.com/oauth2/v1/userinfo');
35 
36  $this->attributes['id'] = $info['id'];
37  $this->attributes['name'] = $info['name'];
38 
39  if (!empty($info['link']))
40  $this->attributes['url'] = $info['link'];
41 
42  /*if (!empty($info['gender']))
43  $this->attributes['gender'] = $info['gender'] == 'male' ? 'M' : 'F';
44 
45  if (!empty($info['picture']))
46  $this->attributes['photo'] = $info['picture'];
47 
48  $info['given_name']; // first name
49  $info['family_name']; // last name
50  $info['birthday']; // format: 0000-00-00
51  $info['locale']; // format: en*/
52  }
53 
54  protected function getCodeUrl($redirect_uri) {
55  $this->setState('redirect_uri', $redirect_uri);
56  $url = parent::getCodeUrl($redirect_uri);
57  if (isset($_GET['js']))
58  $url .= '&display=popup';
59  return $url;
60  }
61 
62  protected function getTokenUrl($code) {
63  return $this->providerOptions['access_token'];
64  }
65 
66  protected function getAccessToken($code) {
67  $params = array(
68  'client_id' => $this->client_id,
69  'client_secret' => $this->client_secret,
70  'grant_type' => 'authorization_code',
71  'code' => $code,
72  'redirect_uri' => $this->getState('redirect_uri'),
73  );
74  return $this->makeRequest($this->getTokenUrl($code), array('data' => $params));
75  }
76 
77  /**
78  * Save access token to the session.
79  * @param stdClass $token access token array.
80  */
81  protected function saveAccessToken($token) {
82  $this->setState('auth_token', $token->access_token);
83  $this->setState('expires', time() + $token->expires_in - 60);
84  $this->access_token = $token->access_token;
85  }
86 
87  /**
88  * Makes the curl request to the url.
89  * @param string $url url to request.
90  * @param array $options HTTP request options. Keys: query, data, referer.
91  * @param boolean $parseJson Whether to parse response in json format.
92  * @return string the response.
93  */
94  protected function makeRequest($url, $options = array(), $parseJson = true) {
95  $options['query']['alt'] = 'json';
96  return parent::makeRequest($url, $options, $parseJson);
97  }
98 
99  /**
100  * Returns the error info from json.
101  * @param stdClass $json the json response.
102  * @return array the error array with 2 keys: code and message. Should be null if no errors.
103  */
104  protected function fetchJsonError($json) {
105  if (isset($json->error)) {
106  return array(
107  'code' => $json->error->code,
108  'message' => $json->error->message,
109  );
110  }
111  else
112  return null;
113  }
114 }