Google_Auth_AppIdentity

Authentication via the Google App Engine App Identity service.

Defined (1)

The class is defined in the following location(s).

/lib/vendor/google/apiclient/src/Google/Auth/AppIdentity.php  
  1. class Google_Auth_AppIdentity extends Google_Auth_Abstract 
  2. const CACHE_PREFIX = "Google_Auth_AppIdentity::"; 
  3. const CACHE_LIFETIME = 1500; 
  4. private $key = null; 
  5. private $client; 
  6. private $token = false; 
  7. private $tokenScopes = false; 
  8.  
  9. public function __construct(Google_Client $client, $config = null) 
  10. $this->client = $client; 
  11.  
  12. /** 
  13. * Retrieve an access token for the scopes supplied. 
  14. */ 
  15. public function authenticateForScope($scopes) 
  16. if ($this->token && $this->tokenScopes == $scopes) { 
  17. return $this->token; 
  18. $memcache = new Memcached(); 
  19. $this->token = $memcache->get(self::CACHE_PREFIX . $scopes); 
  20. if (!$this->token) { 
  21. $this->token = AppIdentityService::getAccessToken($scopes); 
  22. if ($this->token) { 
  23. $memcache_key = self::CACHE_PREFIX; 
  24. if (is_string($scopes)) { 
  25. $memcache_key .= $scopes; 
  26. } else if (is_array($scopes)) { 
  27. $memcache_key .= implode(":", $scopes); 
  28. $memcache->set($memcache_key, $this->token, self::CACHE_LIFETIME); 
  29. $this->tokenScopes = $scopes; 
  30. return $this->token; 
  31.  
  32. /** 
  33. * Perform an authenticated / signed apiHttpRequest. 
  34. * This function takes the apiHttpRequest, calls apiAuth->sign on it 
  35. * (which can modify the request in what ever way fits the auth mechanism) 
  36. * and then calls apiCurlIO::makeRequest on the signed request 
  37. * @param Google_Http_Request $request 
  38. * @return Google_Http_Request The resulting HTTP response including the 
  39. * responseHttpCode, responseHeaders and responseBody. 
  40. */ 
  41. public function authenticatedRequest(Google_Http_Request $request) 
  42. $request = $this->sign($request); 
  43. return $this->io->makeRequest($request); 
  44.  
  45. public function sign(Google_Http_Request $request) 
  46. if (!$this->token) { 
  47. // No token, so nothing to do. 
  48. return $request; 
  49. // Add the OAuth2 header to the request 
  50. $request->setRequestHeaders( 
  51. array('Authorization' => 'Bearer ' . $this->token['access_token']) 
  52. ); 
  53.  
  54. return $request;