-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogle_Client.js
More file actions
433 lines (383 loc) · 11.8 KB
/
Google_Client.js
File metadata and controls
433 lines (383 loc) · 11.8 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Check for the required json and curl extensions, the Google APIs PHP Client
// won't function without them.
var array_merge = require('./helpers/array_merge');
var config = require("./config"),
Google_AssertionCredentials = require('./auth/Google_AssertionCredentials'),
Google_Signer = require('./auth/Google_Signer'),
Google_P12Signer = require('./auth/Google_P12Signer'),
//Google_IO = require('./io/Google_IO'),
Google_Auth = require('./auth/Google_Auth'),
util = require("util");
// If a local configuration file is found, merge it's values with the default configuration
/*
TODO: local_config
if (file_exists(dirname(__FILE__) . '/local_config.php')) {
defaultConfig = apiConfig;
require_once (dirname(__FILE__) . '/local_config.php');
apiConfig = array_merge(defaultConfig, apiConfig);
}
*/
/*
TODO: the rest of these::
// Include the top level classes, they each include their own dependencies
require_once 'service/Google_Model.php';
require_once 'service/Google_BatchRequest.php';
require_once 'cache/Google_Cache.php';
require_once('service/Google_MediaFileUpload.php');
*/
/**
* The Google API Client
* http://code.google.com/p/google-api-php-client/
*
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*/
var Google_Client = function(options){
/**
* @static
* @var Google_Auth auth
*/
//var a = config.authClass;
this.auth = require(config.authClass);//new a(),
this.auth = new this.auth(config);
/* apiConfig */
this.apiConfig = config,
/**
* @static
* @var Google_IO io
*/
this.io = require(config.ioClass);//new a(),
this.io = new this.io(config);
/**
* @static
* @var Google_Cache cache
*/
//cache = new config.cacheClass(),
/**
* @static
* @var boolean useBatch
*/
this.useBatch = false,
/** @var array scopes */
this.scopes = [],
/** @var bool useObjects */
this.useObjects = false;
// definitions of services that are discovered.
this.services = [];
// Used to track authenticated state, can't discover services after doing authenticate()
this.authenticated = false;
//global apiConfig;
//apiConfig = array_merge(apiConfig, config);
};
Google_Client.prototype = {
/**
* Add a service
*/
addService : function (service, version ) {
//global apiConfig;
if (this.authenticated) {
throw new Google_Exception('Cant add services after having authenticated');
}
this.services[service] = {}
if (typeof this.apiConfig.services[service] == 'undefined') {
// Merge the service descriptor with the default values
this.services[service] = array_merge(this.services[service], this.apiConfig.services[service]);
}
},
authenticate : function (code) {
if (arguments.length) code = null;
service = this.prepareService();
this.authenticated = true;
return this.auth.authenticate(service, code);
},
/**
* @return array
* @visible For Testing
*/
prepareService : function () {
service = array();
scopes = array();
if (this.scopes) {
scopes = this.scopes;
} else {
for(var key in this.services ) {
if (isset(this.services[key]['scope'])) {
if (typeof this.services[key]['scope'] == 'array') {
scopes = array_merge(this.services[key]['scope'], scopes);
} else {
scopes.push(this.services[key]['scope']);
}
} else {
scopes.push('https://www.googleapis.com/auth/' . key);
}
this.services[key]['discoveryURI'] = null;
this.services[key]['scope'] = null;
service = array_merge(service, this.services[key]);
}
}
service['scope'] = scopes.join(" ");
return service;
},
/**
* Set the OAuth 2.0 access token using the string that resulted from calling authenticate()
* or Google_Client#getAccessToken().
* @param string accessToken JSON encoded string containing in the following format:
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
* "expires_in":3600, "id_token":"TOKEN", "created":1320790426}
*/
setAccessToken : function (accessToken) {
if (typeof accessToken == 'undefined'|| 'null' == accessToken) {
accessToken = null;
}
this.auth.setAccessToken(accessToken);
},
/**
* Set the type of Auth class the client should use.
* @param string authClassName
*/
setAuthClass : function (authClassName) {
this.auth = new authClassName();
},
/**
* Construct the OAuth 2.0 authorization request URI.
* @return string
*/
createAuthUrl : function () {
service = this.prepareService();
return this.auth.createAuthUrl(service.scope);
},
/**
* Get the OAuth 2.0 access token.
* @return string accessToken JSON encoded string in the following format:
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
* "expires_in":3600,"id_token":"TOKEN", "created":1320790426}
*/
getAccessToken : function () {
token = this.auth.getAccessToken();
return (typeof token == 'undefined' || null == token) ? null : token;
},
/**
* Returns if the access_token is expired.
* @return bool Returns True if the access_token is expired.
*/
isAccessTokenExpired : function () {
return this.auth.isAccessTokenExpired();
},
/**
* Set the developer key to use, these are obtained through the API Console.
* @see http://code.google.com/apis/console-help/#generatingdevkeys
* @param string developerKey
*/
setDeveloperKey : function (developerKey) {
this.auth.setDeveloperKey(developerKey);
},
/**
* Set OAuth 2.0 "state" parameter to achieve per-request customization.
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2
* @param string state
*/
setState : function (state) {
this.auth.setState(state);
},
/**
* @param string accessType Possible values for access_type include:
* {@code "offline"} to request offline access from the user. (This is the default value)
* {@code "online"} to request online access from the user.
*/
setAccessType : function (accessType) {
this.auth.setAccessType(accessType);
},
/**
* @param string approvalPrompt Possible values for approval_prompt include:
* {@code "force"} to force the approval UI to appear. (This is the default value)
* {@code "auto"} to request auto-approval when possible.
*/
setApprovalPrompt : function (approvalPrompt) {
this.auth.setApprovalPrompt(approvalPrompt);
},
/**
* Set the application name, this is included in the User-Agent HTTP header.
* @param string applicationName
*/
setApplicationName : function (applicationName) {
this.apiConfig.application_name = applicationName;
},
/**
* Set the OAuth 2.0 Client ID.
* @param string clientId
*/
setClientId : function (clientId) {
this.apiConfig.oauth2_client_id = clientId;
this.auth.clientId = clientId;
},
/**
* Get the OAuth 2.0 Client ID.
*/
getClientId : function () {
return this.auth.clientId;
},
/**
* Set the OAuth 2.0 Client Secret.
* @param string clientSecret
*/
setClientSecret : function (clientSecret) {
this.apiConfig.oauth2_client_secret = clientSecret;
this.auth.clientSecret = clientSecret;
},
/**
* Get the OAuth 2.0 Client Secret.
*/
getClientSecret : function () {
return this.auth.clientSecret;
},
/**
* Set the OAuth 2.0 Redirect URI.
* @param string redirectUri
*/
setRedirectUri : function (redirectUri) {
this.apiConfig.oauth2_redirect_uri = redirectUri;
this.auth.redirectUri = redirectUri;
},
/**
* Get the OAuth 2.0 Redirect URI.
*/
getRedirectUri : function () {
return this.auth.redirectUri;
},
/**
* Fetches a fresh OAuth 2.0 access token with the given refresh token.
* @param string refreshToken
* @return void
*/
refreshToken : function (refreshToken) {
this.auth.refreshToken(refreshToken);
},
/**
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
* token, if a token isn't provided.
* @throws Google_AuthException
* @param string|null token The token (access token or a refresh token) that should be revoked.
* @return boolean Returns True if the revocation was successful, otherwise False.
*/
revokeToken : function (token) {
this.auth.revokeToken(token);
},
/**
* Verify an id_token. This method will verify the current id_token, if one
* isn't provided.
* @throws Google_AuthException
* @param string|null token The token (id_token) that should be verified.
* @return Google_LoginTicket Returns an apiLoginTicket if the verification was
* successful.
*/
verifyIdToken : function (token ) {
return this.auth.verifyIdToken(token);
},
/**
* @param Google_AssertionCredentials creds
* @return void
*/
setAssertionCredentials : function (/*Google_AssertionCredentials*/ creds) {
this.auth.setAssertionCredentials(creds);
},
getAssertionCredentials : function(){
return this.auth.getAssertionCredentials();
},
/**
* This function allows you to overrule the automatically generated scopes,
* so that you can ask for more or less permission in the auth flow
* Set this before you call authenticate() though!
* @param array scopes, ie: array('https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/moderator')
*/
setScopes : function (scopes) {
this.scopes = typeof(scopes) =='string' ? scopes.split(" ") : scopes;
},
/**
* Declare if objects should be returned by the api service classes.
*
* @param boolean useObjects True if objects should be returned by the service classes.
* False if associative arrays should be returned (default behavior).
* @experimental
*/
setUseObjects : function (useObjects) {
this.apiConfig.use_objects = useObjects;
},
/**
* Declare if objects should be returned by the api service classes.
*
* @param boolean useBatch True if the experimental batch support should
* be enabled. Defaults to False.
* @experimental
*/
setUseBatch : function (useBatch) {
this.useBatch = useBatch;
},
/**
* @static
* @return Google_Auth the implementation of apiAuth.
*/
getAuth : function () {
return this.auth;
},
/**
* @static
* @return Google_IO the implementation of apiIo.
*/
getIo : function () {
return this.io;
},
/**
* @return Google_Cache the implementation of apiCache.
*/
getCache : function () {
return this.cache;
}
};
// Exceptions that the Google PHP API Library can throw
var Exception = function(name,code,message){
this.name = name;
this.code = code;
this.message = message;
var errors = [];
};
Exception.prototype.getErrors = function(){ return this.errors; };
var Google_Exception = function(){};
var Google_AuthException = function(){};
var Google_CacheException = function(){};
var Google_IOException = function(){};
var Google_ServiceException = function(){};
Exception.prototype.toString = function() {
var name = this.name || 'unknown';
var message = this.message || 'no description';
return '[' + name + '] ' + message;
};
util.inherits(Google_Exception,Exception);
util.inherits(Google_AuthException,Exception);
util.inherits(Google_CacheException,Exception);
util.inherits(Google_IOException,Exception);
util.inherits(Google_ServiceException,Exception);
module.exports = {
'Google_Exception': Google_Exception,
'Google_AuthException' : Google_AuthException,
'Google_IOException' : Google_IOException,
'Google_ServiceException' : Google_ServiceException,
'Google_CacheException' : Google_CacheException,
'Google_Client' : Google_Client
};
//module.exports =
// Google_Client;