Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenscg committed Jul 24, 2018
2 parents 121262b + 09a0c02 commit 0c9ed84
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 73 deletions.
43 changes: 43 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Contributor Code of Conduct

As contributors and maintainers of this project,
and in the interest of fostering an open and welcoming community,
we pledge to respect all people who contribute through reporting issues,
posting feature requests, updating documentation,
submitting pull requests or patches, and other activities.

We are committed to making participation in this project
a harassment-free experience for everyone,
regardless of level of experience, gender, gender identity and expression,
sexual orientation, disability, personal appearance,
body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information,
such as physical or electronic
addresses, without explicit permission
* Other unethical or unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct.
By adopting this Code of Conduct,
project maintainers commit themselves to fairly and consistently
applying these principles to every aspect of managing this project.
Project maintainers who do not follow or enforce the Code of Conduct
may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior
may be reported by opening an issue
or contacting one or more of the project maintainers.

This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0,
available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

# Google APIs Client Library for PHP #

## Library maintenance
This client library is supported but in maintenance mode only. We are fixing necessary bugs and adding essential features to ensure this library continues to meet your needs for accessing Google APIs. Non-critical issues will be closed. Any issue may be reopened if it is causing ongoing problems.

## Description ##
The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server.

## Beta ##
This library is in Beta. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it. We will make an effort to support the public and protected surface of the library and maintain backwards compatibility in the future. While we are still in Beta, we reserve the right to make incompatible changes.
These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.

## Google Cloud Platform

For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/GoogleCloudPlatform/google-cloud-php) which is under active development.

## Requirements ##
* [PHP 5.4.0 or higher](http://www.php.net/)

## Google Cloud Platform APIs
If you're looking to call the **Google Cloud Platform** APIs, you will want to use the [Google Cloud PHP](https://github.com/googlecloudplatform/google-cloud-php) library instead of this one.

## Developer Documentation ##
http://developers.google.com/api-client-library/php

Expand Down Expand Up @@ -125,6 +121,11 @@ foreach ($results as $item) {

> An example of this can be seen in [`examples/service-account.php`](examples/service-account.php).

Some APIs
(such as the [YouTube Data API](https://developers.google.com/youtube/v3/)) do
not support service accounts. Check with the specific API documentation if API
calls return unexpected 401 or 403 errors.

1. Follow the instructions to [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount)
1. Download the JSON credentials
1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
Expand Down Expand Up @@ -264,14 +265,21 @@ $response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me');
It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](http://www.php-fig.org/psr/psr-6/) compatible library to the client:

```php
$cache = new Stash\Pool(new Stash\Driver\FileSystem);
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;

$filesystemAdapter = new Local(__DIR__.'/');
$filesystem = new Filesystem($filesystemAdapter);

$cache = new FilesystemCachePool($filesystem);
$client->setCache($cache);
```

In this example we use [StashPHP](http://www.stashphp.com/). Add this to your project with composer:
In this example we use [PHP Cache](http://www.php-cache.com/). Add this to your project with composer:

```
composer require tedivm/stash
composer require cache/filesystem-adapter
```

### Updating Tokens ###
Expand Down
2 changes: 1 addition & 1 deletion examples/idtoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);

// store in the session also
$_SESSION['id_token_token'] = $token;

// redirect back to the example
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
return;
}

/************************************************
Expand Down
4 changes: 2 additions & 2 deletions src/Google/AccessToken/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ private function getJwtService()
$jwtClass = 'Firebase\JWT\JWT';
}

if (property_exists($jwtClass, 'leeway')) {
// adds 1 second to JWT leeway
if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) {
// Ensures JWT leeway is at least 1
// @see https://github.com/google/google-api-php-client/issues/827
$jwtClass::$leeway = 1;
}
Expand Down
22 changes: 16 additions & 6 deletions src/Google/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Ring\Client\StreamHandler;
use GuzzleHttp\Psr7;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -39,7 +38,7 @@
*/
class Google_Client
{
const LIBVER = "2.2.1";
const LIBVER = "2.2.2";
const USER_AGENT_SUFFIX = "google-api-php-client/";
const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke';
const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token';
Expand Down Expand Up @@ -213,7 +212,7 @@ public function refreshTokenWithAssertion()

/**
* Fetches a fresh access token with a given assertion token.
* @param $assertionCredentials optional.
* @param ClientInterface $authHttp optional.
* @return array access token
*/
public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null)
Expand Down Expand Up @@ -443,11 +442,16 @@ public function getAccessToken()
return $this->token;
}

/**
* @return string|null
*/
public function getRefreshToken()
{
if (isset($this->token['refresh_token'])) {
return $this->token['refresh_token'];
}

return null;
}

/**
Expand Down Expand Up @@ -482,13 +486,19 @@ public function isAccessTokenExpired()
return ($created + ($this->token['expires_in'] - 30)) < time();
}

/**
* @deprecated See UPGRADING.md for more information
*/
public function getAuth()
{
throw new BadMethodCallException(
'This function no longer exists. See UPGRADING.md for more information'
);
}

/**
* @deprecated See UPGRADING.md for more information
*/
public function setAuth($auth)
{
throw new BadMethodCallException(
Expand Down Expand Up @@ -755,7 +765,7 @@ public function getScopes()
}

/**
* @return array
* @return string|null
* @visible For Testing
*/
public function prepareScopes()
Expand Down Expand Up @@ -894,7 +904,7 @@ public function setAuthConfig($config)
/**
* Use when the service account has been delegated domain wide access.
*
* @param string subject an email address account to impersonate
* @param string $subject an email address account to impersonate
*/
public function setSubject($subject)
{
Expand Down Expand Up @@ -976,7 +986,7 @@ public function getCache()
}

/**
* @return Google\Auth\CacheInterface Cache implementation
* @param array $cacheConfig
*/
public function setCacheConfig(array $cacheConfig)
{
Expand Down
8 changes: 7 additions & 1 deletion src/Google/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,14 @@ protected function convertToArrayAndStripNulls($o)
*/
public function createRequestUri($restPath, $params)
{
// Override the default servicePath address if the $restPath use a /
if ('/' == substr($restPath, 0, 1)) {
$requestUrl = substr($restPath, 1);
} else {
$requestUrl = $this->servicePath . $restPath;
}

// code for leading slash
$requestUrl = $this->servicePath . $restPath;
if ($this->rootUrl) {
if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) {
$requestUrl = '/' . $requestUrl;
Expand Down
96 changes: 74 additions & 22 deletions tests/Google/AccessToken/RevokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@

class Google_AccessToken_RevokeTest extends BaseTest
{
public function testRevokeAccess()
public function testRevokeAccessGuzzle5()
{
$this->onlyGuzzle5();

$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';

$response = $this->getMock('Psr\Http\Message\ResponseInterface');
$response = $this->getMock('GuzzleHttp\Message\ResponseInterface');
$response->expects($this->exactly(3))
->method('getStatusCode')
->will($this->returnValue(200));
Expand All @@ -45,27 +47,77 @@ function ($request) use (&$token, $response) {
}
));

// adds support for extra "createRequest" step (required for Guzzle 5)
if ($this->isGuzzle5()) {
$requestToken = null;
$request = $this->getMock('GuzzleHttp\Message\RequestInterface');
$request->expects($this->exactly(3))
->method('getBody')
->will($this->returnCallback(
function () use (&$requestToken) {
return 'token='.$requestToken;
}));
$http->expects($this->exactly(3))
->method('createRequest')
$requestToken = null;
$request = $this->getMock('GuzzleHttp\Message\RequestInterface');
$request->expects($this->exactly(3))
->method('getBody')
->will($this->returnCallback(
function ($method, $url, $params) use (&$requestToken, $request) {
parse_str((string) $params['body'], $fields);
$requestToken = isset($fields['token']) ? $fields['token'] : null;

return $request;
}
));
}
function () use (&$requestToken) {
return 'token='.$requestToken;
}));
$http->expects($this->exactly(3))
->method('createRequest')
->will($this->returnCallback(
function ($method, $url, $params) use (&$requestToken, $request) {
parse_str((string) $params['body'], $fields);
$requestToken = isset($fields['token']) ? $fields['token'] : null;

return $request;
}
));

$t = array(
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
);

// Test with access token.
$revoke = new Google_AccessToken_Revoke($http);
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);

// Test with refresh token.
$revoke = new Google_AccessToken_Revoke($http);
$t = array(
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
);
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);

// Test with token string.
$revoke = new Google_AccessToken_Revoke($http);
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}

public function testRevokeAccessGuzzle6()
{
$this->onlyGuzzle6();

$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';

$response = $this->getMock('Psr\Http\Message\ResponseInterface');
$response->expects($this->exactly(3))
->method('getStatusCode')
->will($this->returnValue(200));
$http = $this->getMock('GuzzleHttp\ClientInterface');
$http->expects($this->exactly(3))
->method('send')
->will($this->returnCallback(
function ($request) use (&$token, $response) {
parse_str((string) $request->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;

return $response;
}
));

$t = array(
'access_token' => $accessToken,
Expand Down
Loading

0 comments on commit 0c9ed84

Please sign in to comment.