Google_Auth_AppIdentity

Authentication via the Google App Engine App Identity service.

Defined (1)

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

/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. private $client; 
  4. private $token = false; 
  5. private $tokenScopes = false; 
  6.  
  7. public function __construct(Google_Client $client, $config = null) 
  8. $this->client = $client; 
  9.  
  10. /** 
  11. * Retrieve an access token for the scopes supplied. 
  12. */ 
  13. public function authenticateForScope($scopes) 
  14. if ($this->token && $this->tokenScopes == $scopes) { 
  15. return $this->token; 
  16.  
  17. $cacheKey = self::CACHE_PREFIX; 
  18. if (is_string($scopes)) { 
  19. $cacheKey .= $scopes; 
  20. } else if (is_array($scopes)) { 
  21. $cacheKey .= implode(":", $scopes); 
  22.  
  23. $this->token = $this->client->getCache()->get($cacheKey); 
  24. if (!$this->token) { 
  25. $this->retrieveToken($scopes, $cacheKey); 
  26. } else if ($this->token['expiration_time'] < time()) { 
  27. $this->client->getCache()->delete($cacheKey); 
  28. $this->retrieveToken($scopes, $cacheKey); 
  29.  
  30. $this->tokenScopes = $scopes; 
  31. return $this->token; 
  32.  
  33. /** 
  34. * Retrieve a new access token and store it in cache 
  35. * @param mixed $scopes 
  36. * @param string $cacheKey 
  37. */ 
  38. private function retrieveToken($scopes, $cacheKey) 
  39. $this->token = AppIdentityService::getAccessToken($scopes); 
  40. if ($this->token) { 
  41. $this->client->getCache()->set( 
  42. $cacheKey,  
  43. $this->token 
  44. ); 
  45.  
  46. /** 
  47. * Perform an authenticated / signed apiHttpRequest. 
  48. * This function takes the apiHttpRequest, calls apiAuth->sign on it 
  49. * (which can modify the request in what ever way fits the auth mechanism) 
  50. * and then calls apiCurlIO::makeRequest on the signed request 
  51. * @param Google_Http_Request $request 
  52. * @return Google_Http_Request The resulting HTTP response including the 
  53. * responseHttpCode, responseHeaders and responseBody. 
  54. */ 
  55. public function authenticatedRequest(Google_Http_Request $request) 
  56. $request = $this->sign($request); 
  57. return $this->client->getIo()->makeRequest($request); 
  58.  
  59. public function sign(Google_Http_Request $request) 
  60. if (!$this->token) { 
  61. // No token, so nothing to do. 
  62. return $request; 
  63.  
  64. $this->client->getLogger()->debug('App Identity authentication'); 
  65.  
  66. // Add the OAuth2 header to the request 
  67. $request->setRequestHeaders( 
  68. array('Authorization' => 'Bearer ' . $this->token['access_token']) 
  69. ); 
  70.  
  71. return $request;