From 47c3c6bece495e58381a21fed13a735bd23a51cc Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Jun 2016 15:07:52 -0700 Subject: [PATCH] simplifies loading of json keys (#115) --- src/ApplicationDefaultCredentials.php | 11 ++---- src/CredentialsLoader.php | 37 +++++++------------ .../ServiceAccountCredentialsTest.php | 4 +- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/ApplicationDefaultCredentials.php b/src/ApplicationDefaultCredentials.php index c99c7d322..507b9c0ef 100644 --- a/src/ApplicationDefaultCredentials.php +++ b/src/ApplicationDefaultCredentials.php @@ -134,13 +134,10 @@ public static function getMiddleware( */ public static function getCredentials($scope = null, callable $httpHandler = null) { - $creds = CredentialsLoader::fromEnv($scope); - if (!is_null($creds)) { - return $creds; - } - $creds = CredentialsLoader::fromWellKnownFile($scope); - if (!is_null($creds)) { - return $creds; + $jsonKey = CredentialsLoader::fromEnv() + ?: CredentialsLoader::fromWellKnownFile(); + if (!is_null($jsonKey)) { + return CredentialsLoader::makeCredentials($scope, $jsonKey); } if (AppIdentityCredentials::onAppEngine()) { return new AppIdentityCredentials($scope); diff --git a/src/CredentialsLoader.php b/src/CredentialsLoader.php index 5509ffd8d..5ddeda5e9 100644 --- a/src/CredentialsLoader.php +++ b/src/CredentialsLoader.php @@ -19,8 +19,6 @@ use Google\Auth\Credentials\ServiceAccountCredentials; use Google\Auth\Credentials\UserRefreshCredentials; -use GuzzleHttp\Psr7; -use Psr\Http\Message\StreamInterface; /** * CredentialsLoader contains the behaviour used to locate and find default @@ -56,18 +54,15 @@ private static function isOnWindows() } /** - * Create a credentials instance from the path specified in the environment. + * Load a JSON key from the path specified in the environment. * - * Creates a credentials instance from the path specified in the environment + * Load a JSON key from the path specified in the environment * variable GOOGLE_APPLICATION_CREDENTIALS. Return null if * GOOGLE_APPLICATION_CREDENTIALS is not specified. * - * @param string|array scope the scope of the access request, expressed - * either as an Array or as a space-delimited String. - * - * @return ServiceAccountCredentials Credentials instance | null + * @return array JSON key | null */ - public static function fromEnv($scope = null) + public static function fromEnv() { $path = getenv(self::ENV_VAR); if (empty($path)) { @@ -77,13 +72,12 @@ public static function fromEnv($scope = null) $cause = 'file ' . $path . ' does not exist'; throw new \DomainException(self::unableToReadEnv($cause)); } - $keyStream = Psr7\stream_for(file_get_contents($path)); - - return static::makeCredentials($scope, $keyStream); + $jsonKey = file_get_contents($path); + return json_decode($jsonKey, true); } /** - * Create a credentials instance from a well known path. + * Load a JSON key from a well known path. * * The well known path is OS dependent: * - windows: %APPDATA%/gcloud/application_default_credentials.json @@ -91,12 +85,9 @@ public static function fromEnv($scope = null) * * If the file does not exists, this returns null. * - * @param string|array scope the scope of the access request, expressed - * either as an Array or as a space-delimited String. - * - * @return ServiceAccountCredentials Credentials instance | null + * @return array JSON key | null */ - public static function fromWellKnownFile($scope = null) + public static function fromWellKnownFile() { $rootEnv = self::isOnWindows() ? 'APPDATA' : 'HOME'; $path = [getenv($rootEnv)]; @@ -108,9 +99,8 @@ public static function fromWellKnownFile($scope = null) if (!file_exists($path)) { return; } - $keyStream = Psr7\stream_for(file_get_contents($path)); - - return static::makeCredentials($scope, $keyStream); + $jsonKey = file_get_contents($path); + return json_decode($jsonKey, true); } /** @@ -118,13 +108,12 @@ public static function fromWellKnownFile($scope = null) * * @param string|array scope the scope of the access request, expressed * either as an Array or as a space-delimited String. - * @param StreamInterface $jsonKeyStream read it to get the JSON credentials. + * @param array $jsonKey the JSON credentials. * * @return ServiceAccountCredentials|UserRefreshCredentials */ - public static function makeCredentials($scope, StreamInterface $jsonKeyStream) + public static function makeCredentials($scope, array $jsonKey) { - $jsonKey = json_decode($jsonKeyStream->getContents(), true); if (!array_key_exists('type', $jsonKey)) { throw new \InvalidArgumentException('json key is missing the type field'); } diff --git a/tests/Credentials/ServiceAccountCredentialsTest.php b/tests/Credentials/ServiceAccountCredentialsTest.php index 17b7722dc..d7066dac3 100644 --- a/tests/Credentials/ServiceAccountCredentialsTest.php +++ b/tests/Credentials/ServiceAccountCredentialsTest.php @@ -157,7 +157,7 @@ protected function tearDown() public function testIsNullIfEnvVarIsNotSet() { - $this->assertNull(ServiceAccountCredentials::fromEnv('a scope')); + $this->assertNull(ServiceAccountCredentials::fromEnv()); } /** @@ -198,7 +198,7 @@ public function testIsNullIfFileDoesNotExist() { putenv('HOME=' . __DIR__ . '/../not_exists_fixtures'); $this->assertNull( - ServiceAccountCredentials::fromWellKnownFile('a scope') + ServiceAccountCredentials::fromWellKnownFile() ); }