-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #768 from google/add-support-for-guzzle-6
Add support for guzzle 6
- Loading branch information
Showing
29 changed files
with
1,042 additions
and
717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\ClientInterface; | ||
|
||
class Google_AuthHandler_AuthHandlerFactory | ||
{ | ||
/** | ||
* Builds out a default http handler for the installed version of guzzle. | ||
* | ||
* @return Google_AuthHandler_Guzzle5AuthHandler|Google_AuthHandler_Guzzle6AuthHandler | ||
* @throws Exception | ||
*/ | ||
public static function build($cache = null) | ||
{ | ||
$version = ClientInterface::VERSION; | ||
|
||
switch ($version[0]) { | ||
case '5': | ||
return new Google_AuthHandler_Guzzle5AuthHandler($cache); | ||
case '6': | ||
return new Google_AuthHandler_Guzzle6AuthHandler($cache); | ||
default: | ||
throw new Exception('Version not supported'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
use Google\Auth\CacheInterface; | ||
use Google\Auth\CredentialsLoader; | ||
use Google\Auth\HttpHandler\HttpHandlerFactory; | ||
use Google\Auth\Subscriber\AuthTokenSubscriber; | ||
use Google\Auth\Subscriber\ScopedAccessTokenSubscriber; | ||
use Google\Auth\Subscriber\SimpleSubscriber; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\ClientInterface; | ||
|
||
/** | ||
* | ||
*/ | ||
class Google_AuthHandler_Guzzle5AuthHandler | ||
{ | ||
protected $cache; | ||
|
||
public function __construct(CacheInterface $cache = null) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
public function attachCredentials(ClientInterface $http, CredentialsLoader $credentials) | ||
{ | ||
// if we end up needing to make an HTTP request to retrieve credentials, we | ||
// can use our existing one, but we need to throw exceptions so the error | ||
// bubbles up. | ||
$authHttp = $this->createAuthHttp($http); | ||
$authHttpHandler = HttpHandlerFactory::build($authHttp); | ||
$subscriber = new AuthTokenSubscriber( | ||
$credentials, | ||
[], | ||
$this->cache, | ||
$authHttpHandler | ||
); | ||
|
||
$http->setDefaultOption('auth', 'google_auth'); | ||
$http->getEmitter()->attach($subscriber); | ||
|
||
return $http; | ||
} | ||
|
||
public function attachToken(ClientInterface $http, array $token, array $scopes) | ||
{ | ||
$tokenFunc = function ($scopes) use ($token) { | ||
return $token['access_token']; | ||
}; | ||
|
||
$subscriber = new ScopedAccessTokenSubscriber( | ||
$tokenFunc, | ||
$scopes, | ||
[], | ||
$this->cache | ||
); | ||
|
||
$http->setDefaultOption('auth', 'scoped'); | ||
$http->getEmitter()->attach($subscriber); | ||
|
||
return $http; | ||
} | ||
|
||
public function attachKey(ClientInterface $http, $key) | ||
{ | ||
$subscriber = new SimpleSubscriber(['key' => $key]); | ||
|
||
$http->setDefaultOption('auth', 'simple'); | ||
$http->getEmitter()->attach($subscriber); | ||
|
||
return $http; | ||
} | ||
|
||
private function createAuthHttp(ClientInterface $http) | ||
{ | ||
return new Client( | ||
[ | ||
'base_url' => $http->getBaseUrl(), | ||
'defaults' => [ | ||
'exceptions' => true, | ||
'verify' => $http->getDefaultOption('verify'), | ||
'proxy' => $http->getDefaultOption('proxy'), | ||
] | ||
] | ||
); | ||
} | ||
} |
Oops, something went wrong.