Skip to content

Commit 6dc8190

Browse files
committed
add support for guzzle 6
1 parent 20e6a62 commit 6dc8190

23 files changed

+1036
-706
lines changed

.travis.yml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,41 @@ env:
77
global:
88
- MEMCACHE_HOST=127.0.0.1
99
- MEMCACHE_PORT=11211
10+
matrix:
11+
- GUZZLE_VERSION=~5.2
12+
- GUZZLE_VERSION=~6.0
1013

1114
sudo: false
1215

1316
cache:
1417
directories:
1518
- $HOME/.composer/cache
1619

20+
php:
21+
- 5.4
22+
- 5.5
23+
- 5.6
24+
- 7.0
25+
- hhvm
26+
27+
# Guzzle 6.0 is not compatible with PHP 5.4
1728
matrix:
18-
fast_finish: true
19-
include:
29+
exclude:
2030
- php: 5.4
21-
- php: 5.5
22-
- php: 5.6
23-
env: PHPCS=true
24-
- php: hhvm
31+
env: GUZZLE_VERSION=~6.0
2532

2633
before_install:
2734
- composer self-update
2835

2936
install:
3037
- composer install
38+
- composer require guzzlehttp/guzzle:$GUZZLE_VERSION
3139

3240
before_script:
33-
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "extension=memcache.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
34-
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "extension=memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
3541
- phpenv version-name | grep ^5.[34] && echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
3642
- phpenv version-name | grep ^5.[34] && echo "apc.enable_cli=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
3743

3844
script:
3945
- vendor/bin/phpunit
40-
- if [[ "$PHPCS" == "true" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi
46+
- if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi
47+

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
"license": "Apache-2.0",
88
"require": {
99
"php": ">=5.4",
10-
"google/auth": "0.4",
10+
"google/auth": "0.5",
1111
"firebase/php-jwt": "~2.0|~3.0",
1212
"monolog/monolog": "^1.17",
1313
"phpseclib/phpseclib": "~2.0",
14-
"guzzlehttp/guzzle": "5.2.*"
14+
"guzzlehttp/guzzle": "~5.2|~6.0",
15+
"guzzlehttp/psr7": "1.2.*",
16+
"psr/http-message": "1.0.*"
1517
},
1618
"require-dev": {
1719
"phpunit/phpunit": "~4",

src/Google/AccessToken/Revoke.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
* limitations under the License.
1717
*/
1818

19-
use GuzzleHttp\Client;
19+
use Google\Auth\HttpHandler\HttpHandlerFactory;
2020
use GuzzleHttp\ClientInterface;
21+
use GuzzleHttp\Psr7;
22+
use GuzzleHttp\Psr7\Request;
2123

2224
/**
2325
* Wrapper around Google Access Tokens which provides convenience functions
@@ -36,10 +38,6 @@ class Google_AccessToken_Revoke
3638
*/
3739
public function __construct(ClientInterface $http = null)
3840
{
39-
if (is_null($http)) {
40-
$http = new Client();
41-
}
42-
4341
$this->http = $http;
4442
}
4543

@@ -53,17 +51,25 @@ public function __construct(ClientInterface $http = null)
5351
public function revokeToken(array $token)
5452
{
5553
if (isset($token['refresh_token'])) {
56-
$tokenString = $token['refresh_token'];
54+
$tokenString = $token['refresh_token'];
5755
} else {
58-
$tokenString = $token['access_token'];
56+
$tokenString = $token['access_token'];
5957
}
6058

61-
$request = $this->http->createRequest('POST', Google_Client::OAUTH2_REVOKE_URI);
62-
$request->addHeader('Cache-Control', 'no-store');
63-
$request->addHeader('Content-Type', 'application/x-www-form-urlencoded');
64-
$request->getBody()->replaceFields(array('token' => $tokenString));
59+
$body = Psr7\stream_for(http_build_query(array('token' => $tokenString)));
60+
$request = new Request(
61+
'POST',
62+
Google_Client::OAUTH2_REVOKE_URI,
63+
[
64+
'Cache-Control' => 'no-store',
65+
'Content-Type' => 'application/x-www-form-urlencoded',
66+
],
67+
$body
68+
);
69+
70+
$httpHandler = HttpHandlerFactory::build($this->http);
6571

66-
$response = $this->http->send($request);
72+
$response = $httpHandler($request);
6773
if ($response->getStatusCode() == 200) {
6874
return true;
6975
}

src/Google/AccessToken/Verify.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function verifyIdToken($idToken, $audience = null)
7373
}
7474

7575
// Check signature
76-
$certs = $this->getFederatedSignonCerts();
76+
$certs = $this->getFederatedSignOnCerts();
7777
foreach ($certs as $cert) {
7878
$modulus = new BigInteger($this->jwt->urlsafeB64Decode($cert['n']), 256);
7979
$exponent = new BigInteger($this->jwt->urlsafeB64Decode($cert['e']), 256);
@@ -152,7 +152,7 @@ private function retrieveCertsFromLocation($url)
152152
$response = $this->http->get($url);
153153

154154
if ($response->getStatusCode() == 200) {
155-
return $response->json();
155+
return json_decode((string) $response->getBody(), true);
156156
}
157157
throw new Google_Exception(
158158
sprintf(
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use GuzzleHttp\Client;
19+
use GuzzleHttp\ClientInterface;
20+
21+
class Google_AuthHandler_AuthHandlerFactory
22+
{
23+
/**
24+
* Builds out a default http handler for the installed version of guzzle.
25+
*
26+
* @return Google_AuthHandler_Guzzle5AuthHandler|Google_AuthHandler_Guzzle6AuthHandler
27+
* @throws Exception
28+
*/
29+
public static function build($cache = null)
30+
{
31+
$version = ClientInterface::VERSION;
32+
33+
switch ($version[0]) {
34+
case '5':
35+
return new Google_AuthHandler_Guzzle5AuthHandler($cache);
36+
case '6':
37+
return new Google_AuthHandler_Guzzle6AuthHandler($cache);
38+
default:
39+
throw new Exception('Version not supported');
40+
}
41+
}
42+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
use Google\Auth\CacheInterface;
4+
use Google\Auth\CredentialsLoader;
5+
use Google\Auth\HttpHandler\HttpHandlerFactory;
6+
use Google\Auth\Subscriber\AuthTokenSubscriber;
7+
use Google\Auth\Subscriber\ScopedAccessTokenSubscriber;
8+
use Google\Auth\Subscriber\SimpleSubscriber;
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\ClientInterface;
11+
12+
/**
13+
*
14+
*/
15+
class Google_AuthHandler_Guzzle5AuthHandler
16+
{
17+
protected $cache;
18+
19+
public function __construct(CacheInterface $cache = null)
20+
{
21+
$this->cache = $cache;
22+
}
23+
24+
public function attachCredentials(ClientInterface $http, CredentialsLoader $credentials)
25+
{
26+
// if we end up needing to make an HTTP request to retrieve credentials, we
27+
// can use our existing one, but we need to throw exceptions so the error
28+
// bubbles up.
29+
$authHttp = $this->createAuthHttp($http);
30+
$authHttpHandler = HttpHandlerFactory::build($authHttp);
31+
$subscriber = new AuthTokenSubscriber(
32+
$credentials,
33+
[],
34+
$this->cache,
35+
$authHttpHandler
36+
);
37+
38+
$http->setDefaultOption('auth', 'google_auth');
39+
$http->getEmitter()->attach($subscriber);
40+
41+
return $http;
42+
}
43+
44+
public function attachToken(ClientInterface $http, array $token, array $scopes)
45+
{
46+
$tokenFunc = function ($scopes) use ($token) {
47+
return $token['access_token'];
48+
};
49+
50+
$subscriber = new ScopedAccessTokenSubscriber(
51+
$tokenFunc,
52+
$scopes,
53+
[],
54+
$this->cache
55+
);
56+
57+
$http->setDefaultOption('auth', 'scoped');
58+
$http->getEmitter()->attach($subscriber);
59+
60+
return $http;
61+
}
62+
63+
public function attachKey(ClientInterface $http, $key)
64+
{
65+
$subscriber = new SimpleSubscriber(['key' => $key]);
66+
67+
$http->setDefaultOption('auth', 'simple');
68+
$http->getEmitter()->attach($subscriber);
69+
70+
return $http;
71+
}
72+
73+
private function createAuthHttp(ClientInterface $http)
74+
{
75+
return new Client(
76+
[
77+
'base_url' => $http->getBaseUrl(),
78+
'defaults' => [
79+
'exceptions' => true,
80+
'verify' => $http->getDefaultOption('verify'),
81+
'proxy' => $http->getDefaultOption('proxy'),
82+
]
83+
]
84+
);
85+
}
86+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
use Google\Auth\CacheInterface;
4+
use Google\Auth\CredentialsLoader;
5+
use Google\Auth\HttpHandler\HttpHandlerFactory;
6+
use Google\Auth\Middleware\AuthTokenMiddleware;
7+
use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
8+
use Google\Auth\Middleware\SimpleMiddleware;
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\ClientInterface;
11+
12+
/**
13+
*
14+
*/
15+
class Google_AuthHandler_Guzzle6AuthHandler
16+
{
17+
protected $cache;
18+
19+
public function __construct(CacheInterface $cache = null)
20+
{
21+
$this->cache = $cache;
22+
}
23+
24+
public function attachCredentials(ClientInterface $http, CredentialsLoader $credentials)
25+
{
26+
// if we end up needing to make an HTTP request to retrieve credentials, we
27+
// can use our existing one, but we need to throw exceptions so the error
28+
// bubbles up.
29+
$authHttp = $this->createAuthHttp($http);
30+
$authHttpHandler = HttpHandlerFactory::build($authHttp);
31+
$middleware = new AuthTokenMiddleware(
32+
$credentials,
33+
[],
34+
$this->cache,
35+
$authHttpHandler
36+
);
37+
38+
$config = $http->getConfig();
39+
$config['handler']->push($middleware);
40+
$config['auth'] = 'google_auth';
41+
$http = new Client($config);
42+
43+
return $http;
44+
}
45+
46+
public function attachToken(ClientInterface $http, array $token, array $scopes)
47+
{
48+
$tokenFunc = function ($scopes) use ($token) {
49+
return $token['access_token'];
50+
};
51+
52+
$middleware = new ScopedAccessTokenMiddleware(
53+
$tokenFunc,
54+
$scopes,
55+
[],
56+
$this->cache
57+
);
58+
59+
$config = $http->getConfig();
60+
$config['handler']->push($middleware);
61+
$config['auth'] = 'scoped';
62+
$http = new Client($config);
63+
64+
return $http;
65+
}
66+
67+
public function attachKey(ClientInterface $http, $key)
68+
{
69+
$middleware = new SimpleMiddleware(['key' => $key]);
70+
71+
$config = $http->getConfig();
72+
$config['handler']->push($middleware);
73+
$config['auth'] = 'simple';
74+
$http = new Client($config);
75+
76+
return $http;
77+
}
78+
79+
private function createAuthHttp(ClientInterface $http)
80+
{
81+
return new Client(
82+
[
83+
'base_uri' => $http->getConfig('base_uri'),
84+
'exceptions' => true,
85+
'verify' => $http->getConfig('verify'),
86+
'proxy' => $http->getConfig('proxy'),
87+
]
88+
);
89+
}
90+
}

0 commit comments

Comments
 (0)