/** * 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); } }
/** * Retrieve an access token for the scopes supplied. */ public function authenticateForScope($scopes) { if ($this->token && $this->tokenScopes == $scopes) { return $this->token; } $memcache = new Memcached(); $this->token = $memcache->get(self::CACHE_PREFIX . $scopes); if (!$this->token) { $this->token = AppIdentityService::getAccessToken($scopes); if ($this->token) { $memcache->set(self::CACHE_PREFIX . $scopes, $this->token, self::CACHE_LIFETIME); } } $this->tokenScopes = $scopes; return $this->token; }
/** * Retrieve an access token for the scopes supplied. */ public function authenticateForScope($scopes) { if ($this->token && $this->tokenScopes == $scopes) { return $this->token; } $cacheKey = self::CACHE_PREFIX; if (is_string($scopes)) { $cacheKey .= $scopes; } else { if (is_array($scopes)) { $cacheKey .= implode(":", $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->tokenScopes = $scopes; return $this->token; }
/** * Retrieve an access token for the scopes supplied. */ public function authenticateForScope($scopes) { if ($this->token && $this->tokenScopes == $scopes) { return $this->token; } $memcache = new Memcached(); $this->token = $memcache->get(self::CACHE_PREFIX . $scopes); if (!$this->token) { $this->token = AppIdentityService::getAccessToken($scopes); if ($this->token) { $memcache_key = self::CACHE_PREFIX; if (is_string($scopes)) { $memcache_key .= $scopes; } else { if (is_array($scopes)) { $memcache_key .= implode(":", $scopes); } } $memcache->set($memcache_key, $this->token, self::CACHE_LIFETIME); } } $this->tokenScopes = $scopes; return $this->token; }
/** * Get the OAuth Token HTTP header for the supplied scope. * * @param $scopes mixed The scopes to acquire the token for. * * @return array The HTTP authorization header for the scopes, using the * applications service account. False if the call failed. */ protected function getOAuthTokenHeader($scopes) { if ($this->anonymous) { return []; } try { $token = AppIdentityService::getAccessToken($scopes); return ["Authorization" => sprintf(self::OAUTH_TOKEN_FORMAT, $token['access_token'])]; } catch (AppIdentityException $e) { return false; } }
/** * Implements FetchAuthTokenInterface#fetchAuthToken. * * Fetches the auth tokens using the AppIdentityService if available. * As the AppIdentityService uses protobufs to fetch the access token, * the GuzzleHttp\ClientInterface instance passed in will not be used. * * @param callable $httpHandler callback which delivers psr7 request * @return array the auth metadata: * array(2) { * ["access_token"]=> * string(3) "xyz" * ["expiration_time"]=> * string(10) "1444339905" * } */ public function fetchAuthToken(callable $httpHandler = null) { if (!self::onAppEngine()) { return array(); } if (!class_exists('google\\appengine\\api\\app_identity\\AppIdentityService')) { throw new \Exception('This class must be run in App Engine, or you must include the AppIdentityService ' . 'mock class defined in tests/mocks/AppIdentityService.php'); } // AppIdentityService expects an array when multiple scopes are supplied $scope = is_array($this->scope) ? $this->scope : explode(' ', $this->scope); $token = AppIdentityService::getAccessToken($scope); return $token; }
public function testGetAccessTokenServiceNotAnnApp() { $req = new \google\appengine\GetAccessTokenRequest(); $scope = 'mail.google.com/send'; $req->addScope($scope); $exception = new \google\appengine\runtime\ApplicationError(ErrorCode::NOT_A_VALID_APP, "unknown scope"); $this->setExpectedException('\\google\\appengine\\api\\app_identity\\AppIdentityException', 'The application is not valid.'); $this->apiProxyMock->expectCall('app_identity_service', 'GetAccessToken', $req, $exception); $result = AppIdentityService::getAccessToken($scope); }
private function executeServiceErrorTest($error, $expected_response) { $req = new \google\appengine\GetAccessTokenRequest(); $scope = 'mail.google.com/invalid-scope'; $req->addScope($scope); $exception = new \google\appengine\runtime\ApplicationError($error, "not initialized"); $this->setExpectedException('\\google\\appengine\\api\\app_identity\\AppIdentityException', $expected_response); self::expectGetAccessTokenRequest(array($scope), false, $exception); $result = AppIdentityService::getAccessToken($scope); }