Skip to content

Commit

Permalink
simplifies loading of json keys (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Jun 1, 2016
1 parent 854be44 commit 47c3c6b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
11 changes: 4 additions & 7 deletions src/ApplicationDefaultCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 13 additions & 24 deletions src/CredentialsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -77,26 +72,22 @@ 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
* - others: $HOME/.config/gcloud/application_default_credentials.json
*
* 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)];
Expand All @@ -108,23 +99,21 @@ 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);
}

/**
* Create a new Credentials instance.
*
* @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');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Credentials/ServiceAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function tearDown()

public function testIsNullIfEnvVarIsNotSet()
{
$this->assertNull(ServiceAccountCredentials::fromEnv('a scope'));
$this->assertNull(ServiceAccountCredentials::fromEnv());
}

/**
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testIsNullIfFileDoesNotExist()
{
putenv('HOME=' . __DIR__ . '/../not_exists_fixtures');
$this->assertNull(
ServiceAccountCredentials::fromWellKnownFile('a scope')
ServiceAccountCredentials::fromWellKnownFile()
);
}

Expand Down

0 comments on commit 47c3c6b

Please sign in to comment.