Skip to content

Commit b54cbbc

Browse files
author
Ian Barber
committed
Remove auto-scopes, and make request serialisable
Auto scope addition has been removed. AddService remains in client as a stub method until the templates are updated. Request has been decoupled from client, so should now be serializable without dependency. This required changing the deferred execution model slightly.
1 parent d742317 commit b54cbbc

22 files changed

Lines changed: 155 additions & 132 deletions

examples/fileupload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
$client->setClientId($client_id);
4747
$client->setClientSecret($client_secret);
4848
$client->setRedirectUri($redirect_uri);
49+
$client->addScope("https://www.googleapis.com/auth/drive");
4950
$service = new Google_Service_Drive($client);
5051

5152
if (isset($_REQUEST['logout'])) {

examples/idtoken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
$client->setClientId($client_id);
3434
$client->setClientSecret($client_secret);
3535
$client->setRedirectUri($redirect_uri);
36-
$client->setScopes('https://www.googleapis.com/auth/userinfo.email');
36+
$client->setScopes('email');
3737

3838
/************************************************
3939
If we're logging out we just need to clear our

examples/multi-api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
$client->setClientId($client_id);
4343
$client->setClientSecret($client_secret);
4444
$client->setRedirectUri($redirect_uri);
45+
$client->addScope("https://www.googleapis.com/auth/drive");
46+
$client->addScope("https://www.googleapis.com/auth/youtube");
4547

4648
/************************************************
4749
We are going to create both YouTube and Drive

examples/simple-query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
$client->setDefer(true);
7878
$optParams = array('filter' => 'free-ebooks');
7979
$request = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
80-
$results = $request->execute();
80+
$results = $client->execute($request);
8181

8282
echo "<h3>Results Of Deferred Call:</h3>";
8383
foreach ($results as $item) {

examples/simplefileupload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
$client->setClientId($client_id);
4747
$client->setClientSecret($client_secret);
4848
$client->setRedirectUri($redirect_uri);
49+
$client->addScope("https://www.googleapis.com/auth/drive")
4950
$service = new Google_Service_Drive($client);
5051

5152
if (isset($_REQUEST['logout'])) {

examples/user-example.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
$client->setClientId($client_id);
4242
$client->setClientSecret($client_secret);
4343
$client->setRedirectUri($redirect_uri);
44+
$client->addScope("https://www.googleapis.com/auth/urlshortener");
4445

4546
/************************************************
4647
When we create the service here, we pass the

src/Google/Auth/OAuth2.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public function authenticate($code)
9999
// fetch the access token
100100
$request = $this->client->getIo()->makeRequest(
101101
new Google_Http_Request(
102-
$this->client,
103102
self::OAUTH2_TOKEN_URI,
104103
'POST',
105104
array(),
@@ -302,7 +301,6 @@ public function refreshTokenWithAssertion($assertionCredentials = null)
302301
private function refreshTokenRequest($params)
303302
{
304303
$http = new Google_Http_Request(
305-
$this->client,
306304
self::OAUTH2_TOKEN_URI,
307305
'POST',
308306
array(),
@@ -343,7 +341,6 @@ public function revokeToken($token = null)
343341
$token = $this->token['access_token'];
344342
}
345343
$request = new Google_Http_Request(
346-
$this->client,
347344
self::OAUTH2_REVOKE_URI,
348345
'POST',
349346
array(),
@@ -409,7 +406,6 @@ public function retrieveCertsFromLocation($url)
409406
// This relies on makeRequest caching certificate responses.
410407
$request = $this->client->getIo()->makeRequest(
411408
new Google_Http_Request(
412-
$this->client,
413409
$url
414410
)
415411
);

src/Google/Client.php

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
class Google_Client
3737
{
3838
const LIBVER = "1.0.1-alpha";
39+
const USER_AGENT_SUFFIX = "google-api-php-client/";
3940
/**
4041
* @var Google_Auth_Abstract $auth
4142
*/
@@ -61,9 +62,6 @@ class Google_Client
6162
*/
6263
private $deferExecution = false;
6364

64-
// Scopes available for the added services
65-
protected $availableScopes = array();
66-
6765
/** @var array $scopes */
6866
// Scopes requested by the client
6967
protected $requestedScopes = array();
@@ -100,18 +98,6 @@ function_exists('date_default_timezone_set')) {
10098
$this->config = $config;
10199
}
102100

103-
/**
104-
* Adds the scopes available for a service
105-
*/
106-
public function addService($service, $version = false, $availableScopes = array())
107-
{
108-
if ($this->authenticated) {
109-
throw new Google_Exception('Cant add services after having authenticated');
110-
} else {
111-
$this->availableScopes[$service] = $availableScopes;
112-
}
113-
}
114-
115101
/**
116102
* Get a string containing the version of the library.
117103
*
@@ -121,6 +107,16 @@ public function getLibraryVersion()
121107
{
122108
return self::LIBVER;
123109
}
110+
111+
/**
112+
* Shim function until templates are updated.
113+
* @todo(ianbarber): remove this.
114+
* @deprecated
115+
*/
116+
public function addService($a, $b, $c)
117+
{
118+
return;
119+
}
124120

125121
/**
126122
* Attempt to exchange a code for an valid authentication token.
@@ -175,16 +171,14 @@ public function setAuthConfigFile($file)
175171
public function prepareScopes()
176172
{
177173
if (empty($this->requestedScopes)) {
178-
foreach ($this->availableScopes as $service => $serviceScopes) {
179-
array_push($this->requestedScopes, $serviceScopes[0]);
180-
}
174+
throw new Google_Auth_Exception("No scopes specified");
181175
}
182176
$scopes = implode(' ', $this->requestedScopes);
183177
return $scopes;
184178
}
185179

186180
/**
187-
* Set the OAuth 2.0 access token using the string that resulted from calling authenticate()
181+
* Set the OAuth 2.0 access token using the string that resulted from calling createAuthUrl()
188182
* or Google_Client#getAccessToken().
189183
* @param string $accessToken JSON encoded string containing in the following format:
190184
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
@@ -418,15 +412,33 @@ public function setAssertionCredentials(Google_Auth_AssertionCredentials $creds)
418412
}
419413

420414
/**
421-
* This function allows you to overrule the automatically generated scopes,
422-
* so that you can ask for more or less permission in the auth flow
423-
* Set this before you call authenticate() though!
424-
* @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.me',
415+
* Set the scopes to be requested. Must be called before createAuthUrl().
416+
* Will remove any previously configured scopes.
417+
* @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.login',
425418
* 'https://www.googleapis.com/auth/moderator')
426419
*/
427420
public function setScopes($scopes)
428421
{
429-
$this->requestedScopes = is_string($scopes) ? explode(" ", $scopes) : $scopes;
422+
$this->requestedScopes = array();
423+
$this->addScope($scopes);
424+
}
425+
426+
/**
427+
* This functions adds a scope to be requested as part of the OAuth2.0 flow.
428+
* Will append any scopes not previously requested to the scope parameter.
429+
* A single string will be treated as a scope to request. An array of strings
430+
* will each be appended.
431+
* @param $scope_or_scopes string|array e.g. "profile"
432+
*/
433+
public function addScope($scope_or_scopes)
434+
{
435+
if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) {
436+
$this->requestedScopes[] = $scope_or_scopes;
437+
} else if (is_array($scope_or_scopes)) {
438+
foreach ($scope_or_scopes as $scope) {
439+
$this->addScope($scope);
440+
}
441+
}
430442
}
431443

432444
/**
@@ -462,6 +474,28 @@ public function setDefer($defer)
462474
{
463475
$this->deferExecution = $defer;
464476
}
477+
478+
/**
479+
* Helper method to execute deferred HTTP requests.
480+
*
481+
* @returns object of the type of the expected class or array.
482+
*/
483+
public function execute($request)
484+
{
485+
if ($request instanceof Google_Http_Request) {
486+
$request->setUserAgent(
487+
$this->getApplicationName()
488+
. " " . self::USER_AGENT_SUFFIX
489+
. $this->getLibraryVersion()
490+
);
491+
$request->maybeMoveParametersToBody();
492+
return Google_Http_REST::execute($this, $request);
493+
} else if ($request instanceof Google_Http_Batch) {
494+
return $request->execute();
495+
} else {
496+
throw new Google_Exception("Do not know how to execute this type of object.");
497+
}
498+
}
465499

466500
/**
467501
* Whether or not to return raw requests

src/Google/Http/Batch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function execute()
7070
$body .= "\n--{$this->boundary}--";
7171

7272
$url = $this->base_path . '/batch';
73-
$httpRequest = new Google_Http_Request($this->client, $url, 'POST');
73+
$httpRequest = new Google_Http_Request($url, 'POST');
7474
$httpRequest->setRequestHeaders(
7575
array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)
7676
);
@@ -110,7 +110,7 @@ public function parseResponse(Google_Http_Request $response)
110110
$status = $status[1];
111111

112112
list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false);
113-
$response = new Google_Http_Request($this->client, "");
113+
$response = new Google_Http_Request("");
114114
$response->setResponseHttpCode($status);
115115
$response->setResponseHeaders($partHeaders);
116116
$response->setResponseBody($partBody);

src/Google/Http/MediaFileUpload.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public function nextChunk($chunk = false)
136136
);
137137

138138
$httpRequest = new Google_Http_Request(
139-
$this->client,
140139
$this->resumeUri,
141140
'PUT',
142141
$headers,
@@ -217,9 +216,8 @@ private function process()
217216

218217
private function transformToUploadUrl()
219218
{
220-
$base = $this->request->getBasePath();
221-
$url = str_replace($base, $base . "/upload", $this->request->getBaseUrl());
222-
$this->request->setBaseUrl($url);
219+
$base = $this->request->getBaseComponent();
220+
$this->request->setBaseComponent($base . '/upload');
223221
}
224222

225223
/**

0 commit comments

Comments
 (0)