Skip to content

Commit

Permalink
Merge branch 'MDL-51531-master' of git://github.com/damyon/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Oct 7, 2015
2 parents 4a06e40 + 80af92f commit 99133d0
Show file tree
Hide file tree
Showing 128 changed files with 53,084 additions and 8,910 deletions.
34 changes: 29 additions & 5 deletions lib/google/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ See the examples/ directory for examples of the key client features.
```PHP
<?php

require_once 'google-api-php-client/autoload.php'; // or wherever autoload.php is located
require_once 'google-api-php-client/src/Google/autoload.php'; // or wherever autoload.php is located

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
Expand All @@ -42,6 +42,10 @@ See the examples/ directory for examples of the key client features.

```

### Service Specific Examples ###

YouTube: https://github.com/youtube/api-samples/tree/master/php

## Frequently Asked Questions ##

### What do I do if something isn't working? ###
Expand All @@ -54,6 +58,10 @@ If there is a specific bug with the library, please file a issue in the Github i

We accept contributions via Github Pull Requests, but all contributors need to be covered by the standard Google Contributor License Agreement. You can find links, and more instructions, in the documentation: https://developers.google.com/api-client-library/php/contribute

### I want an example of X! ###

If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above!

### Why do you still support 5.2? ###

When we started working on the 1.0.0 branch we knew there were several fundamental issues to fix with the 0.6 releases of the library. At that time we looked at the usage of the library, and other related projects, and determined that there was still a large and active base of PHP 5.2 installs. You can see this in statistics such as the PHP versions chart in the WordPress stats: http://wordpress.org/about/stats/. We will keep looking at the types of usage we see, and try to take advantage of newer PHP features where possible.
Expand All @@ -72,10 +80,26 @@ $opt_params = array(
);
```

### How do I set a field to null? ###

The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialised properties. To work around this, set the field you want to null to Google_Model::NULL_VALUE. This is a placeholder that will be replaced with a true null when sent over the wire.

## Code Quality ##

Copy the ruleset.xml in style/ into a new directory named GAPI/ in your
/usr/share/php/PHP/CodeSniffer/Standards (or appropriate equivalent directory),
and run code sniffs with:
Run the PHPUnit tests with PHPUnit. You can configure an API key and token in BaseTest.php to run all calls, but this will require some setup on the Google Developer Console.

phpunit tests/

### Coding Style

phpcs --standard=GAPI src/
To check for coding style violations, run

```
vendor/bin/phpcs src --standard=style/ruleset.xml -np
```

To automatically fix (fixable) coding style violations, run

```
vendor/bin/phpcbf src --standard=style/ruleset.xml
```
21 changes: 7 additions & 14 deletions lib/google/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@
* limitations under the License.
*/

function google_api_php_client_autoload($className) {
$classPath = explode('_', $className);
if ($classPath[0] != 'Google') {
return;
}
if (count($classPath) > 3) {
// Maximum class file path depth in this project is 3.
$classPath = array_slice($classPath, 0, 3);
}
$filePath = dirname(__FILE__) . '/src/' . implode('/', $classPath) . '.php';
if (file_exists($filePath)) {
require_once($filePath);
}
// PHP 5.2 compatibility: E_USER_DEPRECATED was added in 5.3
if (!defined('E_USER_DEPRECATED')) {
define('E_USER_DEPRECATED', E_USER_WARNING);
}

spl_autoload_register('google_api_php_client_autoload');
$error = "google-api-php-client's autoloader was moved to src/Google/autoload.php in 1.1.3. This ";
$error .= "redirect will be removed in 1.2. Please adjust your code to use the new location.";
trigger_error($error, E_USER_DEPRECATED);
require_once dirname(__FILE__) . '/src/Google/autoload.php';
2 changes: 1 addition & 1 deletion lib/google/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

// All Google API classes support autoload with this.
require_once($CFG->libdir . '/google/autoload.php');
require_once($CFG->libdir . '/google/src/Google/autoload.php');
// To be able to use our custom IO class.
require_once($CFG->libdir . '/google/curlio.php');

Expand Down
2 changes: 1 addition & 1 deletion lib/google/readme_moodle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Repository: https://github.com/google/google-api-php-client
Documentation: https://developers.google.com/api-client-library/php/
Global documentation: https://developers.google.com

Downloaded version: 1.1.2
Downloaded version: 1.1.5
4 changes: 3 additions & 1 deletion lib/google/src/Google/Auth/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/

require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}

/**
* Abstract class for the Authentication in the API client
Expand Down
33 changes: 24 additions & 9 deletions lib/google/src/Google/Auth/AppIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
*/
use google\appengine\api\app_identity\AppIdentityService;

require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}

/**
* Authentication via the Google App Engine App Identity service.
*/
class Google_Auth_AppIdentity extends Google_Auth_Abstract
{
const CACHE_PREFIX = "Google_Auth_AppIdentity::";
private $key = null;
private $client;
private $token = false;
private $tokenScopes = false;
Expand Down Expand Up @@ -58,18 +59,32 @@ public function authenticateForScope($scopes)

$this->token = $this->client->getCache()->get($cacheKey);
if (!$this->token) {
$this->token = AppIdentityService::getAccessToken($scopes);
if ($this->token) {
$this->client->getCache()->set(
$cacheKey,
$this->token
);
}
$this->retrieveToken($scopes, $cacheKey);
} else if ($this->token['expiration_time'] < time()) {
$this->client->getCache()->delete($cacheKey);
$this->retrieveToken($scopes, $cacheKey);
}

$this->tokenScopes = $scopes;
return $this->token;
}

/**
* Retrieve a new access token and store it in cache
* @param mixed $scopes
* @param string $cacheKey
*/
private function retrieveToken($scopes, $cacheKey)
{
$this->token = AppIdentityService::getAccessToken($scopes);
if ($this->token) {
$this->client->getCache()->set(
$cacheKey,
$this->token
);
}
}

/**
* Perform an authenticated / signed apiHttpRequest.
* This function takes the apiHttpRequest, calls apiAuth->sign on it
Expand Down
6 changes: 3 additions & 3 deletions lib/google/src/Google/Auth/AssertionCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}

/**
* Credentials object used for OAuth 2.0 Signed JWT assertion grants.
*
* @author Chirag Shah <chirags@google.com>
*/
class Google_Auth_AssertionCredentials
{
Expand Down
146 changes: 146 additions & 0 deletions lib/google/src/Google/Auth/ComputeEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* 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.
*/

if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}

/**
* Authentication via built-in Compute Engine service accounts.
* The instance must be pre-configured with a service account
* and the appropriate scopes.
* @author Jonathan Parrott <jon.wayne.parrott@gmail.com>
*/
class Google_Auth_ComputeEngine extends Google_Auth_Abstract
{
const METADATA_AUTH_URL =
'http://metadata/computeMetadata/v1/instance/service-accounts/default/token';
private $client;
private $token;

public function __construct(Google_Client $client, $config = null)
{
$this->client = $client;
}

/**
* Perform an authenticated / signed apiHttpRequest.
* This function takes the apiHttpRequest, calls apiAuth->sign on it
* (which can modify the request in what ever way fits the auth mechanism)
* and then calls apiCurlIO::makeRequest on the signed request
*
* @param Google_Http_Request $request
* @return Google_Http_Request The resulting HTTP response including the
* responseHttpCode, responseHeaders and responseBody.
*/
public function authenticatedRequest(Google_Http_Request $request)
{
$request = $this->sign($request);
return $this->client->getIo()->makeRequest($request);
}

/**
* @param string $token
* @throws Google_Auth_Exception
*/
public function setAccessToken($token)
{
$token = json_decode($token, true);
if ($token == null) {
throw new Google_Auth_Exception('Could not json decode the token');
}
if (! isset($token['access_token'])) {
throw new Google_Auth_Exception("Invalid token format");
}
$token['created'] = time();
$this->token = $token;
}

public function getAccessToken()
{
return json_encode($this->token);
}

/**
* Acquires a new access token from the compute engine metadata server.
* @throws Google_Auth_Exception
*/
public function acquireAccessToken()
{
$request = new Google_Http_Request(
self::METADATA_AUTH_URL,
'GET',
array(
'Metadata-Flavor' => 'Google'
)
);
$request->disableGzip();
$response = $this->client->getIo()->makeRequest($request);

if ($response->getResponseHttpCode() == 200) {
$this->setAccessToken($response->getResponseBody());
$this->token['created'] = time();
return $this->getAccessToken();
} else {
throw new Google_Auth_Exception(
sprintf(
"Error fetching service account access token, message: '%s'",
$response->getResponseBody()
),
$response->getResponseHttpCode()
);
}
}

/**
* Include an accessToken in a given apiHttpRequest.
* @param Google_Http_Request $request
* @return Google_Http_Request
* @throws Google_Auth_Exception
*/
public function sign(Google_Http_Request $request)
{
if ($this->isAccessTokenExpired()) {
$this->acquireAccessToken();
}

$this->client->getLogger()->debug('Compute engine service account authentication');

$request->setRequestHeaders(
array('Authorization' => 'Bearer ' . $this->token['access_token'])
);

return $request;
}

/**
* Returns if the access_token is expired.
* @return bool Returns True if the access_token is expired.
*/
public function isAccessTokenExpired()
{
if (!$this->token || !isset($this->token['created'])) {
return true;
}

// If the token is set to expire in the next 30 seconds.
$expired = ($this->token['created']
+ ($this->token['expires_in'] - 30)) < time();

return $expired;
}
}
4 changes: 3 additions & 1 deletion lib/google/src/Google/Auth/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/

require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}

class Google_Auth_Exception extends Google_Exception
{
Expand Down
4 changes: 3 additions & 1 deletion lib/google/src/Google/Auth/LoginTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/

require_once realpath(dirname(__FILE__) . '/../../../autoload.php');
if (!class_exists('Google_Client')) {
require_once dirname(__FILE__) . '/../autoload.php';
}

/**
* Class to hold information about an authenticated login.
Expand Down
Loading

0 comments on commit 99133d0

Please sign in to comment.