Skip to content

Commit

Permalink
add support for guzzle 6
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Dec 28, 2015
1 parent 20e6a62 commit 6dc8190
Show file tree
Hide file tree
Showing 23 changed files with 1,036 additions and 706 deletions.
25 changes: 16 additions & 9 deletions .travis.yml
Expand Up @@ -7,34 +7,41 @@ env:
global:
- MEMCACHE_HOST=127.0.0.1
- MEMCACHE_PORT=11211
matrix:
- GUZZLE_VERSION=~5.2
- GUZZLE_VERSION=~6.0

sudo: false

cache:
directories:
- $HOME/.composer/cache

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

# Guzzle 6.0 is not compatible with PHP 5.4
matrix:
fast_finish: true
include:
exclude:
- php: 5.4
- php: 5.5
- php: 5.6
env: PHPCS=true
- php: hhvm
env: GUZZLE_VERSION=~6.0

before_install:
- composer self-update

install:
- composer install
- composer require guzzlehttp/guzzle:$GUZZLE_VERSION

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

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

6 changes: 4 additions & 2 deletions composer.json
Expand Up @@ -7,11 +7,13 @@
"license": "Apache-2.0",
"require": {
"php": ">=5.4",
"google/auth": "0.4",
"google/auth": "0.5",
"firebase/php-jwt": "~2.0|~3.0",
"monolog/monolog": "^1.17",
"phpseclib/phpseclib": "~2.0",
"guzzlehttp/guzzle": "5.2.*"
"guzzlehttp/guzzle": "~5.2|~6.0",
"guzzlehttp/psr7": "1.2.*",
"psr/http-message": "1.0.*"
},
"require-dev": {
"phpunit/phpunit": "~4",
Expand Down
30 changes: 18 additions & 12 deletions src/Google/AccessToken/Revoke.php
Expand Up @@ -16,8 +16,10 @@
* limitations under the License.
*/

use GuzzleHttp\Client;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;

/**
* Wrapper around Google Access Tokens which provides convenience functions
Expand All @@ -36,10 +38,6 @@ class Google_AccessToken_Revoke
*/
public function __construct(ClientInterface $http = null)
{
if (is_null($http)) {
$http = new Client();
}

$this->http = $http;
}

Expand All @@ -53,17 +51,25 @@ public function __construct(ClientInterface $http = null)
public function revokeToken(array $token)
{
if (isset($token['refresh_token'])) {
$tokenString = $token['refresh_token'];
$tokenString = $token['refresh_token'];
} else {
$tokenString = $token['access_token'];
$tokenString = $token['access_token'];
}

$request = $this->http->createRequest('POST', Google_Client::OAUTH2_REVOKE_URI);
$request->addHeader('Cache-Control', 'no-store');
$request->addHeader('Content-Type', 'application/x-www-form-urlencoded');
$request->getBody()->replaceFields(array('token' => $tokenString));
$body = Psr7\stream_for(http_build_query(array('token' => $tokenString)));
$request = new Request(
'POST',
Google_Client::OAUTH2_REVOKE_URI,
[
'Cache-Control' => 'no-store',
'Content-Type' => 'application/x-www-form-urlencoded',
],
$body
);

$httpHandler = HttpHandlerFactory::build($this->http);

$response = $this->http->send($request);
$response = $httpHandler($request);
if ($response->getStatusCode() == 200) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Google/AccessToken/Verify.php
Expand Up @@ -73,7 +73,7 @@ public function verifyIdToken($idToken, $audience = null)
}

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

if ($response->getStatusCode() == 200) {
return $response->json();
return json_decode((string) $response->getBody(), true);
}
throw new Google_Exception(
sprintf(
Expand Down
42 changes: 42 additions & 0 deletions src/Google/AuthHandler/AuthHandlerFactory.php
@@ -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');
}
}
}
86 changes: 86 additions & 0 deletions src/Google/AuthHandler/Guzzle5AuthHandler.php
@@ -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'),
]
]
);
}
}
90 changes: 90 additions & 0 deletions src/Google/AuthHandler/Guzzle6AuthHandler.php
@@ -0,0 +1,90 @@
<?php

use Google\Auth\CacheInterface;
use Google\Auth\CredentialsLoader;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\Middleware\AuthTokenMiddleware;
use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
use Google\Auth\Middleware\SimpleMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;

/**
*
*/
class Google_AuthHandler_Guzzle6AuthHandler
{
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);
$middleware = new AuthTokenMiddleware(
$credentials,
[],
$this->cache,
$authHttpHandler
);

$config = $http->getConfig();
$config['handler']->push($middleware);
$config['auth'] = 'google_auth';
$http = new Client($config);

return $http;
}

public function attachToken(ClientInterface $http, array $token, array $scopes)
{
$tokenFunc = function ($scopes) use ($token) {
return $token['access_token'];
};

$middleware = new ScopedAccessTokenMiddleware(
$tokenFunc,
$scopes,
[],
$this->cache
);

$config = $http->getConfig();
$config['handler']->push($middleware);
$config['auth'] = 'scoped';
$http = new Client($config);

return $http;
}

public function attachKey(ClientInterface $http, $key)
{
$middleware = new SimpleMiddleware(['key' => $key]);

$config = $http->getConfig();
$config['handler']->push($middleware);
$config['auth'] = 'simple';
$http = new Client($config);

return $http;
}

private function createAuthHttp(ClientInterface $http)
{
return new Client(
[
'base_uri' => $http->getConfig('base_uri'),
'exceptions' => true,
'verify' => $http->getConfig('verify'),
'proxy' => $http->getConfig('proxy'),
]
);
}
}

0 comments on commit 6dc8190

Please sign in to comment.