| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2014 Google Inc. |
| 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 |
/* |
| 19 |
* WARNING - this class depends on the Google App Engine PHP library |
| 20 |
* which is 5.3 and above only, so if you include this in a PHP 5.2 |
| 21 |
* setup or one without 5.3 things will blow up. |
| 22 |
*/ |
| 23 |
use google\appengine\api\app_identity\AppIdentityService; |
| 24 |
|
| 25 |
require_once ANALYTIFY_LIB_PATH . "Google/Auth/Abstract.php"; |
| 26 |
require_once ANALYTIFY_LIB_PATH . "Google/Http/Request.php"; |
| 27 |
|
| 28 |
/** |
| 29 |
* Authentication via the Google App Engine App Identity service. |
| 30 |
*/ |
| 31 |
class Analytify_Google_Auth_AppIdentity extends Analytify_Google_Auth_Abstract |
| 32 |
{ |
| 33 |
const CACHE_PREFIX = "Analytify_Google_Auth_AppIdentity::"; |
| 34 |
const CACHE_LIFETIME = 1500; |
| 35 |
private $key = null; |
| 36 |
private $client; |
| 37 |
private $token = false; |
| 38 |
private $tokenScopes = false; |
| 39 |
|
| 40 |
public function __construct(Analytify_Google_Client $client, $config = null) |
| 41 |
{ |
| 42 |
$this->client = $client; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Retrieve an access token for the scopes supplied. |
| 47 |
*/ |
| 48 |
public function authenticateForScope($scopes) |
| 49 |
{ |
| 50 |
if ($this->token && $this->tokenScopes == $scopes) { |
| 51 |
return $this->token; |
| 52 |
} |
| 53 |
$memcache = new Memcached(); |
| 54 |
$this->token = $memcache->get(self::CACHE_PREFIX . $scopes); |
| 55 |
if (!$this->token) { |
| 56 |
$this->token = AppIdentityService::getAccessToken($scopes); |
| 57 |
if ($this->token) { |
| 58 |
$memcache->set(self::CACHE_PREFIX . $scopes, $this->token, self::CACHE_LIFETIME); |
| 59 |
} |
| 60 |
} |
| 61 |
$this->tokenScopes = $scopes; |
| 62 |
return $this->token; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Perform an authenticated / signed apiHttpRequest. |
| 67 |
* This function takes the apiHttpRequest, calls apiAuth->sign on it |
| 68 |
* (which can modify the request in what ever way fits the auth mechanism) |
| 69 |
* and then calls apiCurlIO::makeRequest on the signed request |
| 70 |
* |
| 71 |
* @param Analytify_Google_Http_Request $request |
| 72 |
* @return Analytify_Google_Http_Request The resulting HTTP response including the |
| 73 |
* responseHttpCode, responseHeaders and responseBody. |
| 74 |
*/ |
| 75 |
public function authenticatedRequest(Analytify_Google_Http_Request $request) |
| 76 |
{ |
| 77 |
$request = $this->sign($request); |
| 78 |
return $this->io->makeRequest($request); |
| 79 |
} |
| 80 |
|
| 81 |
public function sign(Analytify_Google_Http_Request $request) |
| 82 |
{ |
| 83 |
if (!$this->token) { |
| 84 |
// No token, so nothing to do. |
| 85 |
return $request; |
| 86 |
} |
| 87 |
// Add the OAuth2 header to the request |
| 88 |
$request->setRequestHeaders( |
| 89 |
array('Authorization' => 'Bearer ' . $this->token['access_token']) |
| 90 |
); |
| 91 |
|
| 92 |
return $request; |
| 93 |
} |
| 94 |
} |
| 95 |
|