From 8555cb063caa5571f80d9605969411b894ee6eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Mendoza?= Date: Thu, 22 Aug 2024 17:45:48 -0400 Subject: [PATCH 1/7] feat: Add a file system cache class (#571) Co-authored-by: Brent Shaffer --- README.md | 19 ++ src/Cache/FileSystemCacheItemPool.php | 230 ++++++++++++++++++++ tests/Cache/FileSystemCacheItemPoolTest.php | 220 +++++++++++++++++++ 3 files changed, 469 insertions(+) create mode 100644 src/Cache/FileSystemCacheItemPool.php create mode 100644 tests/Cache/FileSystemCacheItemPoolTest.php diff --git a/README.md b/README.md index 63bcfeaa2..ce23622af 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,25 @@ $memoryCache = new MemoryCacheItemPool; $middleware = ApplicationDefaultCredentials::getCredentials($scope, cache: $memoryCache); ``` +### FileSystemCacheItemPool Cache +The `FileSystemCacheItemPool` class is a `PSR-6` compliant cache that stores its +serialized objects on disk, caching data between processes and making it possible +to use data between different requests. + +```php +use Google\Auth\Cache\FileSystemCacheItemPool; +use Google\Auth\ApplicationDefaultCredentials; + +// Create a Cache pool instance +$cache = new FileSystemCacheItemPool(__DIR__ . '/cache'); + +// Pass your Cache to the Auth Library +$credentials = ApplicationDefaultCredentials::getCredentials($scope, cache: $cache); + +// This token will be cached and be able to be used for the next request +$token = $credentials->fetchAuthToken(); +``` + ### Integrating with a third party cache You can use a third party that follows the `PSR-6` interface of your choice. diff --git a/src/Cache/FileSystemCacheItemPool.php b/src/Cache/FileSystemCacheItemPool.php new file mode 100644 index 000000000..ee0651a4e --- /dev/null +++ b/src/Cache/FileSystemCacheItemPool.php @@ -0,0 +1,230 @@ + + */ + private array $buffer = []; + + /** + * Creates a FileSystemCacheItemPool cache that stores values in local storage + * + * @param string $path The string representation of the path where the cache will store the serialized objects. + */ + public function __construct(string $path) + { + $this->cachePath = $path; + + if (is_dir($this->cachePath)) { + return; + } + + if (!mkdir($this->cachePath)) { + throw new ErrorException("Cache folder couldn't be created."); + } + } + + /** + * {@inheritdoc} + */ + public function getItem(string $key): CacheItemInterface + { + if (!$this->validKey($key)) { + throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + } + + $item = new TypedItem($key); + + $itemPath = $this->cacheFilePath($key); + + if (!file_exists($itemPath)) { + return $item; + } + + $serializedItem = file_get_contents($itemPath); + + if ($serializedItem === false) { + return $item; + } + + $item->set(unserialize($serializedItem)); + + return $item; + } + + /** + * {@inheritdoc} + * + * @return iterable An iterable object containing all the + * A traversable collection of Cache Items keyed by the cache keys of + * each item. A Cache item will be returned for each key, even if that + * key is not found. However, if no keys are specified then an empty + * traversable MUST be returned instead. + */ + public function getItems(array $keys = []): iterable + { + $result = []; + + foreach ($keys as $key) { + $result[$key] = $this->getItem($key); + } + + return $result; + } + + /** + * {@inheritdoc} + */ + public function save(CacheItemInterface $item): bool + { + if (!$this->validKey($item->getKey())) { + return false; + } + + $itemPath = $this->cacheFilePath($item->getKey()); + $serializedItem = serialize($item->get()); + + $result = file_put_contents($itemPath, $serializedItem); + + // 0 bytes write is considered a successful operation + if ($result === false) { + return false; + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function hasItem(string $key): bool + { + return $this->getItem($key)->isHit(); + } + + /** + * {@inheritdoc} + */ + public function clear(): bool + { + $this->buffer = []; + + if (!is_dir($this->cachePath)) { + return false; + } + + $files = scandir($this->cachePath); + if (!$files) { + return false; + } + + foreach ($files as $fileName) { + if ($fileName === '.' || $fileName === '..') { + continue; + } + + if (!unlink($this->cachePath . '/' . $fileName)) { + return false; + } + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function deleteItem(string $key): bool + { + if (!$this->validKey($key)) { + throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + } + + $itemPath = $this->cacheFilePath($key); + + if (!file_exists($itemPath)) { + return true; + } + + return unlink($itemPath); + } + + /** + * {@inheritdoc} + */ + public function deleteItems(array $keys): bool + { + $result = true; + + foreach ($keys as $key) { + if (!$this->deleteItem($key)) { + $result = false; + } + } + + return $result; + } + + /** + * {@inheritdoc} + */ + public function saveDeferred(CacheItemInterface $item): bool + { + array_push($this->buffer, $item); + + return true; + } + + /** + * {@inheritdoc} + */ + public function commit(): bool + { + $result = true; + + foreach ($this->buffer as $item) { + if (!$this->save($item)) { + $result = false; + } + } + + return $result; + } + + private function cacheFilePath(string $key): string + { + return $this->cachePath . '/' . $key; + } + + private function validKey(string $key): bool + { + return (bool) preg_match('|^[a-zA-Z0-9_\.]+$|', $key); + } +} diff --git a/tests/Cache/FileSystemCacheItemPoolTest.php b/tests/Cache/FileSystemCacheItemPoolTest.php new file mode 100644 index 000000000..a3214587a --- /dev/null +++ b/tests/Cache/FileSystemCacheItemPoolTest.php @@ -0,0 +1,220 @@ +', ',', '/', ' ', + ]; + + public function setUp(): void + { + $this->pool = new FileSystemCacheItemPool($this->defaultCacheDirectory); + } + + public function tearDown(): void + { + $files = scandir($this->defaultCacheDirectory); + + foreach($files as $fileName) { + if ($fileName === '.' || $fileName === '..') { + continue; + } + + unlink($this->defaultCacheDirectory . '/' . $fileName); + } + + rmdir($this->defaultCacheDirectory); + } + + public function testInstanceCreatesCacheFolder() + { + $this->assertTrue(file_exists($this->defaultCacheDirectory)); + $this->assertTrue(is_dir($this->defaultCacheDirectory)); + } + + public function testSaveAndGetItem() + { + $item = $this->getNewItem(); + $item->expiresAfter(60); + $this->pool->save($item); + $retrievedItem = $this->pool->getItem($item->getKey()); + + $this->assertTrue($retrievedItem->isHit()); + $this->assertEquals($retrievedItem->get(), $item->get()); + } + + public function testHasItem() + { + $item = $this->getNewItem(); + $this->assertFalse($this->pool->hasItem($item->getKey())); + $this->pool->save($item); + $this->assertTrue($this->pool->hasItem($item->getKey())); + } + + public function testDeleteItem() + { + $item = $this->getNewItem(); + $this->pool->save($item); + + $this->assertTrue($this->pool->deleteItem($item->getKey())); + $this->assertFalse($this->pool->hasItem($item->getKey())); + } + + public function testDeleteItems() + { + $items = [ + $this->getNewItem(), + $this->getNewItem('NewItem2'), + $this->getNewItem('NewItem3') + ]; + + foreach ($items as $item) { + $this->pool->save($item); + } + + $itemKeys = array_map(fn ($item) => $item->getKey(), $items); + + $result = $this->pool->deleteItems($itemKeys); + $this->assertTrue($result); + } + + public function testGetItems() + { + $items = [ + $this->getNewItem(), + $this->getNewItem('NewItem2'), + $this->getNewItem('NewItem3') + ]; + + foreach ($items as $item) { + $this->pool->save($item); + } + + $keys = array_map(fn ($item) => $item->getKey(), $items); + array_push($keys, 'NonExistant'); + + $retrievedItems = $this->pool->getItems($keys); + + foreach ($items as $item) { + $this->assertTrue($retrievedItems[$item->getKey()]->isHit()); + } + + $this->assertFalse($retrievedItems['NonExistant']->isHit()); + } + + public function testClear() + { + $item = $this->getNewItem(); + $this->pool->save($item); + $this->assertLessThan(scandir($this->defaultCacheDirectory), 2); + $this->pool->clear(); + // Clear removes all the files, but scandir returns `.` and `..` as files + $this->assertEquals(count(scandir($this->defaultCacheDirectory)), 2); + } + + public function testSaveDeferredAndCommit() + { + $item = $this->getNewItem(); + $this->pool->saveDeferred($item); + $this->assertFalse($this->pool->getItem($item->getKey())->isHit()); + + $this->pool->commit(); + $this->assertTrue($this->pool->getItem($item->getKey())->isHit()); + } + + /** + * @dataProvider provideInvalidChars + */ + public function testGetItemWithIncorrectKeyShouldThrowAnException($char) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("The key '$char' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + $item = $this->getNewItem($char); + $this->pool->getItem($item->getKey()); + } + + /** + * @dataProvider provideInvalidChars + */ + public function testGetItemsWithIncorrectKeyShouldThrowAnException($char) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("The key '$char' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + $item = $this->getNewItem($char); + $this->pool->getItems([$item->getKey()]); + } + + /** + * @dataProvider provideInvalidChars + */ + public function testHasItemWithIncorrectKeyShouldThrowAnException($char) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("The key '$char' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + $item = $this->getNewItem($char); + $this->pool->hasItem($item->getKey()); + } + + /** + * @dataProvider provideInvalidChars + */ + public function testDeleteItemWithIncorrectKeyShouldThrowAnException($char) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("The key '$char' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + $item = $this->getNewItem($char); + $this->pool->deleteItem($item->getKey()); + } + + /** + * @dataProvider provideInvalidChars + */ + public function testDeleteItemsWithIncorrectKeyShouldThrowAnException($char) + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("The key '$char' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); + $item = $this->getNewItem($char); + $this->pool->deleteItems([$item->getKey()]); + } + + private function getNewItem(null|string $key = null): TypedItem + { + $item = new TypedItem($key ?? 'NewItem'); + $item->set('NewValue'); + + return $item; + } + + public function provideInvalidChars(): array + { + return array_map(fn ($char) => [$char], $this->invalidChars); + } +} From e0fa4ea774a65b0016735429f222ad5506f596af Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 26 Aug 2024 11:07:35 -0700 Subject: [PATCH 2/7] chore: replace cs and staticanalysis with reusable workflows (#573) --- .github/workflows/tests.yml | 28 ++++------------------------ .php-cs-fixer.dist.php | 25 ------------------------- 2 files changed, 4 insertions(+), 49 deletions(-) delete mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 61c1cf40a..2cbf61b44 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -51,31 +51,11 @@ jobs: run: vendor/bin/phpunit style: - runs-on: ubuntu-latest name: PHP Style Check - steps: - - uses: actions/checkout@v4 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - - name: Run Script - run: | - composer install - composer global require friendsofphp/php-cs-fixer:^3.0 - ~/.composer/vendor/bin/php-cs-fixer fix --dry-run --diff + uses: GoogleCloudPlatform/php-tools/.github/workflows/code-standards.yml@main staticanalysis: - runs-on: ubuntu-latest name: PHPStan Static Analysis - steps: - - uses: actions/checkout@v4 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - - name: Run Script - run: | - composer install - composer global require phpstan/phpstan:^1.8 - ~/.composer/vendor/bin/phpstan analyse --autoload-file tests/phpstan-autoload.php + uses: GoogleCloudPlatform/php-tools/.github/workflows/static-analysis.yml@main + with: + autoload-file: tests/phpstan-autoload.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index 1e6dd1eff..000000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,25 +0,0 @@ -setRules([ - '@PSR2' => true, - 'array_syntax' => ['syntax' => 'short'], - 'concat_space' => ['spacing' => 'one'], - 'no_unused_imports' => true, - 'ordered_imports' => true, - 'new_with_braces' => true, - 'whitespace_after_comma_in_array' => true, - 'method_argument_space' => [ - 'keep_multiple_spaces_after_comma' => true, // for wordpress constants - 'on_multiline' => 'ignore', // consider removing this someday - ], - 'return_type_declaration' => [ - 'space_before' => 'none' - ], - 'single_quote' => true, - ]) - ->setFinder( - PhpCsFixer\Finder::create() - ->in(__DIR__) - ) -; From c94ee3a8635550dba7c46bdb508401b298928b46 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 26 Aug 2024 11:09:35 -0700 Subject: [PATCH 3/7] chore(docs): use shared doctum workflow (#574) --- .github/workflows/docs.yml | 40 +++++++++++++------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 59674b926..e2741b146 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -6,32 +6,20 @@ on: tags: - "*" workflow_dispatch: + inputs: + tag: + description: 'Tag to release' + pull_request: + +permissions: + contents: write jobs: docs: - name: "Generate Project Documentation" - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 8.1 - - name: Install Dependencies - uses: nick-invision/retry@v3 - with: - timeout_minutes: 10 - max_attempts: 3 - command: composer config repositories.sami vcs https://${{ secrets.GITHUB_TOKEN }}@github.com/jdpedrie/sami.git && composer require sami/sami:v4.2 && git reset --hard HEAD - - name: Generate Documentation - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - run: .github/actions/docs/entrypoint.sh - - name: Deploy 🚀 - uses: JamesIves/github-pages-deploy-action@releases/v3 - with: - ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} - BRANCH: gh-pages - FOLDER: .docs + name: "Generate and Deploy Documentation" + uses: GoogleCloudPlatform/php-tools/.github/workflows/doctum.yml@main + with: + title: "Google Auth Library PHP Reference Documentation" + default_version: ${{ inputs.tag || github.head_ref || github.ref_name }} + dry_run: ${{ github.event_name == 'pull_request' }} + From 0c25599a91530b5847f129b271c536f75a7563f5 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 11:33:48 -0700 Subject: [PATCH 4/7] chore(main): release 1.42.0 (#566) --- CHANGELOG.md | 8 ++++++++ VERSION | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d3928a13..f456a2107 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ * [feat]: add support for Firebase v6.0 (#391) +## [1.42.0](https://github.com/googleapis/google-auth-library-php/compare/v1.41.0...v1.42.0) (2024-08-26) + + +### Features + +* Add a file system cache class ([#571](https://github.com/googleapis/google-auth-library-php/issues/571)) ([8555cb0](https://github.com/googleapis/google-auth-library-php/commit/8555cb063caa5571f80d9605969411b894ee6eb0)) +* Private key getters on service account credentials (https://github.com/googleapis/google-auth-library-php/pull/557) ([d2fa07b](https://github.com/googleapis/google-auth-library-php/commit/d2fa07b8a8edfa65c1bd732dac794c070e3451bc)) + ## [1.41.0](https://github.com/googleapis/google-auth-library-php/compare/v1.40.0...v1.41.0) (2024-07-10) diff --git a/VERSION b/VERSION index 7d47e5998..a50908ca3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.41.0 +1.42.0 From e10dc3fb43b6f76c717d10f553104431181a7686 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 26 Aug 2024 13:13:32 -0700 Subject: [PATCH 5/7] chore: cleanup docs job --- .github/actions/docs/entrypoint.sh | 11 ----------- .github/actions/docs/sami.php | 27 --------------------------- .github/workflows/docs.yml | 2 -- 3 files changed, 40 deletions(-) delete mode 100755 .github/actions/docs/entrypoint.sh delete mode 100644 .github/actions/docs/sami.php diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh deleted file mode 100755 index 84f1a3967..000000000 --- a/.github/actions/docs/entrypoint.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -l - -apt-get update -apt-get install -y git -git fetch origin -git reset --hard HEAD - -mkdir .docs -mkdir .cache - -php vendor/bin/sami.php update .github/actions/docs/sami.php diff --git a/.github/actions/docs/sami.php b/.github/actions/docs/sami.php deleted file mode 100644 index df537ceef..000000000 --- a/.github/actions/docs/sami.php +++ /dev/null @@ -1,27 +0,0 @@ -files() - ->name('*.php') - ->exclude('vendor') - ->exclude('tests') - ->in($projectRoot); - -$versions = GitVersionCollection::create($projectRoot) - ->addFromTags('v1.*') - ->add('main', 'main branch'); - -return new Sami($iterator, [ - 'title' => 'Google Auth Library for PHP API Reference', - 'build_dir' => $projectRoot . '/.docs/%version%', - 'cache_dir' => $projectRoot . '/.cache/%version%', - 'remote_repository' => new GitHubRemoteRepository('googleapis/google-auth-library-php', $projectRoot), - 'versions' => $versions -]); diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index e2741b146..84a1c6c5c 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,8 +1,6 @@ name: Generate Documentation on: push: - branches: - - main tags: - "*" workflow_dispatch: From 257ba33a4065c2c93ceb80f733eaacf6deeacd08 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 3 Oct 2024 09:43:10 -0600 Subject: [PATCH 6/7] chore: drop support for PHP 8.0 (#532) --- .github/workflows/tests.yml | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2cbf61b44..42578bfa7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,7 +10,7 @@ jobs: test: strategy: matrix: - php: [ "8.0", "8.1", "8.2", "8.3" ] + php: [ "8.1", "8.2", "8.3" ] os: [ ubuntu-latest ] include: - os: windows-latest @@ -40,7 +40,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: "8.0" + php-version: "8.1" - name: Install Dependencies uses: nick-invision/retry@v3 with: diff --git a/composer.json b/composer.json index 41a1d0532..72673f1f2 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "docs": "https://googleapis.github.io/google-auth-library-php/main/" }, "require": { - "php": "^8.0", + "php": "^8.1", "firebase/php-jwt": "^6.0", "guzzlehttp/guzzle": "^7.4.5", "guzzlehttp/psr7": "^2.4.5", From dd5f6efce2dca5189cb7bf4ca156e5755ec1bd85 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 4 Oct 2024 12:19:10 -0600 Subject: [PATCH 7/7] chore: updates from new cs rules (#577) --- .github/workflows/lint.yml | 18 +++++++++++++++ .github/workflows/tests.yml | 10 --------- src/CredentialSource/AwsNativeSource.php | 2 +- src/Credentials/GCECredentials.php | 4 ++-- src/Middleware/AuthTokenMiddleware.php | 3 ++- src/OAuth2.php | 13 ++++++----- tests/ApplicationDefaultCredentialsTest.php | 6 +++-- tests/Cache/FileSystemCacheItemPoolTest.php | 2 +- tests/CredentialSource/FileSourceTest.php | 1 - .../ExternalAccountCredentialsTest.php | 2 +- tests/Credentials/GCECredentialsTest.php | 2 +- tests/OAuth2Test.php | 22 +++++++++---------- tests/mocks/TestFileCacheItemPool.php | 1 - tests/phpstan-autoload.php | 2 +- 14 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 000000000..3921698b0 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,18 @@ +name: Lint +on: + push: + branches: [ main ] + pull_request: + +permissions: + contents: read +jobs: + style: + name: PHP Style Check + uses: GoogleCloudPlatform/php-tools/.github/workflows/code-standards.yml@main + + staticanalysis: + name: PHPStan Static Analysis + uses: GoogleCloudPlatform/php-tools/.github/workflows/static-analysis.yml@main + with: + autoload-file: tests/phpstan-autoload.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 42578bfa7..3da2d2dfc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,13 +49,3 @@ jobs: command: composer update --prefer-lowest - name: Run Script run: vendor/bin/phpunit - - style: - name: PHP Style Check - uses: GoogleCloudPlatform/php-tools/.github/workflows/code-standards.yml@main - - staticanalysis: - name: PHPStan Static Analysis - uses: GoogleCloudPlatform/php-tools/.github/workflows/static-analysis.yml@main - with: - autoload-file: tests/phpstan-autoload.php diff --git a/src/CredentialSource/AwsNativeSource.php b/src/CredentialSource/AwsNativeSource.php index 6d9244ba2..e99d0ee6f 100644 --- a/src/CredentialSource/AwsNativeSource.php +++ b/src/CredentialSource/AwsNativeSource.php @@ -103,7 +103,7 @@ public function fetchSubjectToken(callable $httpHandler = null): string $headers['x-goog-cloud-target-resource'] = $this->audience; // Format headers as they're expected in the subject token - $formattedHeaders= array_map( + $formattedHeaders = array_map( fn ($k, $v) => ['key' => $k, 'value' => $v], array_keys($headers), $headers, diff --git a/src/Credentials/GCECredentials.php b/src/Credentials/GCECredentials.php index 8b7547816..49030a845 100644 --- a/src/Credentials/GCECredentials.php +++ b/src/Credentials/GCECredentials.php @@ -426,12 +426,12 @@ private static function detectResidencyWindows(string $registryProductKey): bool try { $productName = $shell->regRead($registryProductKey); - } catch(com_exception) { + } catch (com_exception) { // This means that we tried to read a key that doesn't exist on the registry // which might mean that it is a windows instance that is not on GCE return false; } - + return 0 === strpos($productName, self::PRODUCT_NAME); } diff --git a/src/Middleware/AuthTokenMiddleware.php b/src/Middleware/AuthTokenMiddleware.php index 798766efa..3bbda7a23 100644 --- a/src/Middleware/AuthTokenMiddleware.php +++ b/src/Middleware/AuthTokenMiddleware.php @@ -132,7 +132,8 @@ private function addAuthHeaders(RequestInterface $request) ) { $token = $this->fetcher->fetchAuthToken(); $request = $request->withHeader( - 'authorization', 'Bearer ' . ($token['access_token'] ?? $token['id_token'] ?? '') + 'authorization', + 'Bearer ' . ($token['access_token'] ?? $token['id_token'] ?? '') ); } else { $headers = $this->fetcher->updateMetadata($request->getHeaders(), null, $this->httpHandler); diff --git a/src/OAuth2.php b/src/OAuth2.php index 4019e258a..2463854e0 100644 --- a/src/OAuth2.php +++ b/src/OAuth2.php @@ -724,7 +724,7 @@ public function getSubjectTokenFetcher(): ?ExternalAccountCredentialSourceInterf */ public function parseTokenResponse(ResponseInterface $resp) { - $body = (string)$resp->getBody(); + $body = (string) $resp->getBody(); if ($resp->hasHeader('Content-Type') && $resp->getHeaderLine('Content-Type') == 'application/x-www-form-urlencoded' ) { @@ -1009,13 +1009,13 @@ public function setRedirectUri($uri) if (!$this->isAbsoluteUri($uri)) { // "postmessage" is a reserved URI string in Google-land // @see https://developers.google.com/identity/sign-in/web/server-side-flow - if ('postmessage' !== (string)$uri) { + if ('postmessage' !== (string) $uri) { throw new InvalidArgumentException( 'Redirect URI must be absolute' ); } } - $this->redirectUri = (string)$uri; + $this->redirectUri = (string) $uri; } /** @@ -1127,7 +1127,7 @@ public function setGrantType($grantType) 'invalid grant type' ); } - $this->grantType = (string)$grantType; + $this->grantType = (string) $grantType; } } @@ -1460,7 +1460,7 @@ public function setExpiresIn($expiresIn) $this->issuedAt = null; } else { $this->issuedAt = time(); - $this->expiresIn = (int)$expiresIn; + $this->expiresIn = (int) $expiresIn; } } @@ -1768,7 +1768,8 @@ private function getFirebaseJwtKeys($publicKey, $allowedAlgs) throw new \InvalidArgumentException( 'To have multiple allowed algorithms, You must provide an' . ' array of Firebase\JWT\Key objects.' - . ' See https://github.com/firebase/php-jwt for more information.'); + . ' See https://github.com/firebase/php-jwt for more information.' + ); } $allowedAlg = array_pop($allowedAlgs); } else { diff --git a/tests/ApplicationDefaultCredentialsTest.php b/tests/ApplicationDefaultCredentialsTest.php index fa537691f..c1583ed06 100644 --- a/tests/ApplicationDefaultCredentialsTest.php +++ b/tests/ApplicationDefaultCredentialsTest.php @@ -168,7 +168,8 @@ public function testImpersonatedServiceAccountCredentials() ); $this->assertInstanceOf( 'Google\Auth\Credentials\ImpersonatedServiceAccountCredentials', - $creds); + $creds + ); $this->assertEquals('service_account_name@namespace.iam.gserviceaccount.com', $creds->getClientName()); @@ -179,7 +180,8 @@ public function testImpersonatedServiceAccountCredentials() $sourceCredentials = $sourceCredentialsProperty->getValue($creds); $this->assertInstanceOf( 'Google\Auth\Credentials\UserRefreshCredentials', - $sourceCredentials); + $sourceCredentials + ); } public function testUserRefreshCredentials() diff --git a/tests/Cache/FileSystemCacheItemPoolTest.php b/tests/Cache/FileSystemCacheItemPoolTest.php index a3214587a..86b3e4eb5 100644 --- a/tests/Cache/FileSystemCacheItemPoolTest.php +++ b/tests/Cache/FileSystemCacheItemPoolTest.php @@ -43,7 +43,7 @@ public function tearDown(): void { $files = scandir($this->defaultCacheDirectory); - foreach($files as $fileName) { + foreach ($files as $fileName) { if ($fileName === '.' || $fileName === '..') { continue; } diff --git a/tests/CredentialSource/FileSourceTest.php b/tests/CredentialSource/FileSourceTest.php index e2c79bde7..9cdcbb9cc 100644 --- a/tests/CredentialSource/FileSourceTest.php +++ b/tests/CredentialSource/FileSourceTest.php @@ -45,7 +45,6 @@ public function provideFetchSubjectToken() $file1 = tempnam(sys_get_temp_dir(), 'test1'); file_put_contents($file1, 'abc'); - $file2 = tempnam(sys_get_temp_dir(), 'test2'); file_put_contents($file2, json_encode(['token' => 'def'])); diff --git a/tests/Credentials/ExternalAccountCredentialsTest.php b/tests/Credentials/ExternalAccountCredentialsTest.php index 09cac05db..4d1f8ae0e 100644 --- a/tests/Credentials/ExternalAccountCredentialsTest.php +++ b/tests/Credentials/ExternalAccountCredentialsTest.php @@ -561,7 +561,7 @@ public function testUrlSourceCacheKey() $expectedKey = 'fakeUrl.scope1...'; $this->assertEquals($expectedKey, $cacheKey); } - + public function testExecutableSourceCacheKey() { $this->baseCreds['credential_source'] = [ diff --git a/tests/Credentials/GCECredentialsTest.php b/tests/Credentials/GCECredentialsTest.php index 7aca40510..f6d9c2266 100644 --- a/tests/Credentials/GCECredentialsTest.php +++ b/tests/Credentials/GCECredentialsTest.php @@ -138,7 +138,7 @@ public function testOnWindowsGceWithResidencyWithNoCom() $method = (new ReflectionClass(GCECredentials::class)) ->getMethod('detectResidencyWindows'); - + $method->setAccessible(true); $this->assertFalse($method->invoke(null, 'thisShouldBeFalse')); diff --git a/tests/OAuth2Test.php b/tests/OAuth2Test.php index e00ab647f..14263fce0 100644 --- a/tests/OAuth2Test.php +++ b/tests/OAuth2Test.php @@ -290,7 +290,7 @@ public function testRedirectUriPostmessageIsAllowed() ]); $this->assertEquals('postmessage', $o->getRedirectUri()); $url = $o->buildFullAuthorizationUri(); - $parts = parse_url((string)$url); + $parts = parse_url((string) $url); parse_str($parts['query'], $query); $this->assertArrayHasKey('redirect_uri', $query); $this->assertEquals('postmessage', $query['redirect_uri']); @@ -726,7 +726,7 @@ public function testGeneratesAuthorizationCodeRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('authorization_code', $fields['grant_type']); $this->assertEquals('an_auth_code', $fields['code']); } @@ -745,7 +745,7 @@ public function testGeneratesPasswordRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('password', $fields['grant_type']); $this->assertEquals('a_password', $fields['password']); $this->assertEquals('a_username', $fields['username']); @@ -764,7 +764,7 @@ public function testGeneratesRefreshTokenRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('refresh_token', $fields['grant_type']); $this->assertEquals('a_refresh_token', $fields['refresh_token']); } @@ -780,7 +780,7 @@ public function testClientSecretAddedIfSetForAuthorizationCodeRequests() $o = new OAuth2($testConfig); $o->setCode('an_auth_code'); $request = $o->generateCredentialsRequest(); - $fields = Query::parse((string)$request->getBody()); + $fields = Query::parse((string) $request->getBody()); $this->assertEquals('a_client_secret', $fields['client_secret']); } @@ -794,7 +794,7 @@ public function testClientSecretAddedIfSetForRefreshTokenRequests() $o = new OAuth2($testConfig); $o->setRefreshToken('a_refresh_token'); $request = $o->generateCredentialsRequest(); - $fields = Query::parse((string)$request->getBody()); + $fields = Query::parse((string) $request->getBody()); $this->assertEquals('a_client_secret', $fields['client_secret']); } @@ -809,7 +809,7 @@ public function testClientSecretAddedIfSetForPasswordRequests() $o->setUsername('a_username'); $o->setPassword('a_password'); $request = $o->generateCredentialsRequest(); - $fields = Query::parse((string)$request->getBody()); + $fields = Query::parse((string) $request->getBody()); $this->assertEquals('a_client_secret', $fields['client_secret']); } @@ -827,7 +827,7 @@ public function testGeneratesAssertionRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals(OAuth2::JWT_URN, $fields['grant_type']); $this->assertArrayHasKey('assertion', $fields); } @@ -846,7 +846,7 @@ public function testGeneratesExtendedRequests() $req = $o->generateCredentialsRequest(); $this->assertInstanceOf('Psr\Http\Message\RequestInterface', $req); $this->assertEquals('POST', $req->getMethod()); - $fields = Query::parse((string)$req->getBody()); + $fields = Query::parse((string) $req->getBody()); $this->assertEquals('my_value', $fields['my_param']); $this->assertEquals('urn:my_test_grant_type', $fields['grant_type']); } @@ -1289,7 +1289,7 @@ public function testStsCredentialsRequestMinimal() $request = $o->generateCredentialsRequest(); $this->assertEquals('POST', $request->getMethod()); $this->assertEquals($this->stsMinimal['tokenCredentialUri'], (string) $request->getUri()); - parse_str((string)$request->getBody(), $requestParams); + parse_str((string) $request->getBody(), $requestParams); $this->assertCount(4, $requestParams); $this->assertEquals(OAuth2::STS_URN, $requestParams['grant_type']); $this->assertEquals('xyz', $requestParams['subject_token']); @@ -1314,7 +1314,7 @@ public function testStsCredentialsRequestFull() $request = $o->generateCredentialsRequest(); $this->assertEquals('POST', $request->getMethod()); $this->assertEquals($this->stsMinimal['tokenCredentialUri'], (string) $request->getUri()); - parse_str((string)$request->getBody(), $requestParams); + parse_str((string) $request->getBody(), $requestParams); $this->assertCount(9, $requestParams); $this->assertEquals(OAuth2::STS_URN, $requestParams['grant_type']); diff --git a/tests/mocks/TestFileCacheItemPool.php b/tests/mocks/TestFileCacheItemPool.php index 42c8d5a5c..65fbc8a77 100644 --- a/tests/mocks/TestFileCacheItemPool.php +++ b/tests/mocks/TestFileCacheItemPool.php @@ -37,7 +37,6 @@ final class TestFileCacheItemPool implements CacheItemPoolInterface */ private $deferredItems; - public function __construct(string $cacheDir) { $this->cacheDir = $cacheDir; diff --git a/tests/phpstan-autoload.php b/tests/phpstan-autoload.php index 22a38a245..38e79cbcc 100644 --- a/tests/phpstan-autoload.php +++ b/tests/phpstan-autoload.php @@ -10,7 +10,7 @@ public function __construct(string $command) { //do nothing } - + public function regRead(string $key): string { // do nothing