diff --git a/src/CredentialsLoader.php b/src/CredentialsLoader.php index d5d59b6be..b71ee9044 100644 --- a/src/CredentialsLoader.php +++ b/src/CredentialsLoader.php @@ -80,10 +80,16 @@ public static function fromEnv() if (empty($path)) { return null; } + + if (is_string($path) && str_starts_with($path, '{')) { + return json_decode($path, true); + } + if (!file_exists($path)) { $cause = 'file ' . $path . ' does not exist'; throw new \DomainException(self::unableToReadEnv($cause)); } + $jsonKey = file_get_contents($path); return json_decode((string) $jsonKey, true); diff --git a/tests/ApplicationDefaultCredentialsTest.php b/tests/ApplicationDefaultCredentialsTest.php index 1db9b9e43..6a5b84bba 100644 --- a/tests/ApplicationDefaultCredentialsTest.php +++ b/tests/ApplicationDefaultCredentialsTest.php @@ -68,6 +68,16 @@ public function testLoadsOKIfEnvSpecifiedIsValid() ); } + public function testLoadsOKIfEnvSpecifiedIsAJsonString() + { + $json = file_get_contents(__DIR__ . '/fixtures' . '/private.json'); + + putenv(ServiceAccountCredentials::ENV_VAR . '=' . $json); + $this->assertNotNull( + ApplicationDefaultCredentials::getCredentials('a scope') + ); + } + public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet() { putenv('HOME=' . __DIR__ . '/fixtures');