GoogleOAuth2Service.php
6.4 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* GoogleOAuth2Service class file.
*
* Register application: https://code.google.com/apis/console/
*
* @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;
/**
* Google provider class.
*
* @package application.extensions.eauth.services
*/
class GoogleOAuth2Service extends Service
{
/**
* Defined scopes - More scopes are listed here:
* https://developers.google.com/oauthplayground/
*/
// Basic
const SCOPE_EMAIL = 'email';
const SCOPE_PROFILE = 'profile';
const SCOPE_USERINFO_EMAIL = 'https://www.googleapis.com/auth/userinfo.email';
const SCOPE_USERINFO_PROFILE = 'https://www.googleapis.com/auth/userinfo.profile';
// Google+
const SCOPE_GPLUS_ME = 'https://www.googleapis.com/auth/plus.me';
const SCOPE_GPLUS_LOGIN = 'https://www.googleapis.com/auth/plus.login';
// Google Drive
const SCOPE_DOCUMENTSLIST = 'https://docs.google.com/feeds/';
const SCOPE_SPREADSHEETS = 'https://spreadsheets.google.com/feeds/';
const SCOPE_GOOGLEDRIVE = 'https://www.googleapis.com/auth/drive';
const SCOPE_DRIVE_APPS = 'https://www.googleapis.com/auth/drive.appdata';
const SCOPE_DRIVE_APPS_READ_ONLY = 'https://www.googleapis.com/auth/drive.apps.readonly';
const SCOPE_GOOGLEDRIVE_FILES = 'https://www.googleapis.com/auth/drive.file';
const SCOPE_DRIVE_METADATA_READ_ONLY = 'https://www.googleapis.com/auth/drive.metadata.readonly';
const SCOPE_DRIVE_READ_ONLY = 'https://www.googleapis.com/auth/drive.readonly';
const SCOPE_DRIVE_SCRIPTS = 'https://www.googleapis.com/auth/drive.scripts';
// Adwords
const SCOPE_ADSENSE = 'https://www.googleapis.com/auth/adsense';
const SCOPE_ADWORDS = 'https://adwords.google.com/api/adwords/';
const SCOPE_GAN = 'https://www.googleapis.com/auth/gan'; // google affiliate network...?
// Google Analytics
const SCOPE_ANALYTICS = 'https://www.googleapis.com/auth/analytics';
const SCOPE_ANALYTICS_EDIT = 'https://www.googleapis.com/auth/analytics.edit';
const SCOPE_ANALYTICS_MANAGE_USERS = 'https://www.googleapis.com/auth/analytics.manage.users';
const SCOPE_ANALYTICS_READ_ONLY = 'https://www.googleapis.com/auth/analytics.readonly';
// Other services
const SCOPE_BOOKS = 'https://www.googleapis.com/auth/books';
const SCOPE_BLOGGER = 'https://www.googleapis.com/auth/blogger';
const SCOPE_CALENDAR = 'https://www.googleapis.com/auth/calendar';
const SCOPE_CONTACT = 'https://www.google.com/m8/feeds/';
const SCOPE_CHROMEWEBSTORE = 'https://www.googleapis.com/auth/chromewebstore.readonly';
const SCOPE_GMAIL = 'https://mail.google.com/mail/feed/atom';
const SCOPE_PICASAWEB = 'https://picasaweb.google.com/data/';
const SCOPE_SITES = 'https://sites.google.com/feeds/';
const SCOPE_URLSHORTENER = 'https://www.googleapis.com/auth/urlshortener';
const SCOPE_WEBMASTERTOOLS = 'https://www.google.com/webmasters/tools/feeds/';
const SCOPE_TASKS = 'https://www.googleapis.com/auth/tasks';
// Cloud services
const SCOPE_CLOUDSTORAGE = 'https://www.googleapis.com/auth/devstorage.read_write';
const SCOPE_CONTENTFORSHOPPING = 'https://www.googleapis.com/auth/structuredcontent'; // what even is this
const SCOPE_USER_PROVISIONING = 'https://apps-apis.google.com/a/feeds/user/';
const SCOPE_GROUPS_PROVISIONING = 'https://apps-apis.google.com/a/feeds/groups/';
const SCOPE_NICKNAME_PROVISIONING = 'https://apps-apis.google.com/a/feeds/alias/';
// Old
const SCOPE_ORKUT = 'https://www.googleapis.com/auth/orkut';
const SCOPE_GOOGLELATITUDE =
'https://www.googleapis.com/auth/latitude.all.best https://www.googleapis.com/auth/latitude.all.city';
const SCOPE_OPENID = 'openid';
// YouTube
const SCOPE_YOUTUBE_GDATA = 'https://gdata.youtube.com';
const SCOPE_YOUTUBE_ANALYTICS_MONETARY = 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly';
const SCOPE_YOUTUBE_ANALYTICS = 'https://www.googleapis.com/auth/yt-analytics.readonly';
const SCOPE_YOUTUBE = 'https://www.googleapis.com/auth/youtube';
const SCOPE_YOUTUBE_READ_ONLY = 'https://www.googleapis.com/auth/youtube.readonly';
const SCOPE_YOUTUBE_UPLOAD = 'https://www.googleapis.com/auth/youtube.upload';
const SCOPE_YOUTUBE_PATNER = 'https://www.googleapis.com/auth/youtubepartner';
const SCOPE_YOUTUBE_PARTNER_EDIT = 'https://www.googleapis.com/auth/youtubepartner-channel-edit';
// Google Glass
const SCOPE_GLASS_TIMELINE = 'https://www.googleapis.com/auth/glass.timeline';
const SCOPE_GLASS_LOCATION = 'https://www.googleapis.com/auth/glass.location';
protected $name = 'google_oauth';
protected $title = 'Google';
protected $type = 'OAuth2';
protected $jsArguments = ['popup' => ['width' => 500, 'height' => 450]];
protected $scopes = [self::SCOPE_USERINFO_PROFILE];
protected $providerOptions = [
'authorize' => 'https://accounts.google.com/o/oauth2/auth',
'access_token' => 'https://accounts.google.com/o/oauth2/token',
];
protected function fetchAttributes()
{
$info = $this->makeSignedRequest('https://www.googleapis.com/oauth2/v1/userinfo');
$this->attributes['id'] = $info['id'];
$this->attributes['name'] = $info['name'];
if (!empty($info['link'])) {
$this->attributes['url'] = $info['link'];
}
/*if (!empty($info['gender']))
$this->attributes['gender'] = $info['gender'] == 'male' ? 'M' : 'F';
if (!empty($info['picture']))
$this->attributes['photo'] = $info['picture'];
$info['given_name']; // first name
$info['family_name']; // last name
$info['birthday']; // format: 0000-00-00
$info['locale']; // format: en*/
}
/**
* 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)
{
if (!isset($options['query']['alt'])) {
$options['query']['alt'] = 'json';
}
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']['code'],
'message' => $response['error']['message'],
];
} else {
return null;
}
}
}