diff --git a/src/Core/AccessToken.php b/src/Core/AccessToken.php index 7b09444..89743e8 100644 --- a/src/Core/AccessToken.php +++ b/src/Core/AccessToken.php @@ -122,7 +122,7 @@ protected function set_expires(DateTime|DateInterval|int|null $expires = null):v // clamp max expiry if($this->expires > $max){ - $this->expires = $max; + $this->expires = $max; // @codeCoverageIgnore } } diff --git a/src/Core/OAuthProvider.php b/src/Core/OAuthProvider.php index a49df03..689c2b0 100644 --- a/src/Core/OAuthProvider.php +++ b/src/Core/OAuthProvider.php @@ -484,7 +484,7 @@ final protected function getMeResponseData(string $endpoint, array|null $params $this->handleMeResponseError($response); /** @noinspection PhpUnreachableStatementInspection this is here because phpstan silly */ - return []; + return []; // @codeCoverageIgnore } /** diff --git a/src/Core/PKCETrait.php b/src/Core/PKCETrait.php index 11ab981..b74abfb 100644 --- a/src/Core/PKCETrait.php +++ b/src/Core/PKCETrait.php @@ -61,10 +61,6 @@ final public function setCodeChallenge(array $params, string $challengeMethod):a */ final public function setCodeVerifier(array $params):array{ - if(!$this instanceof PKCE){ - throw new ProviderException('PKCE challenge not supported'); - } - if(!isset($params['grant_type'], $params['code']) || $params['grant_type'] !== 'authorization_code'){ throw new ProviderException('invalid authorization request body'); } @@ -120,7 +116,7 @@ final public function generateChallenge(string $verifier, string $challengeMetho $verifier = match($challengeMethod){ PKCE::CHALLENGE_METHOD_S256 => hash('sha256', $verifier, true), // no other hash methods yet - default => throw new ProviderException('invalid PKCE challenge method'), + default => throw new ProviderException('invalid PKCE challenge method'), // @codeCoverageIgnore }; return sodium_bin2base64($verifier, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING); diff --git a/src/Core/TokenInvalidateTrait.php b/src/Core/TokenInvalidateTrait.php index ee78878..af0dfae 100644 --- a/src/Core/TokenInvalidateTrait.php +++ b/src/Core/TokenInvalidateTrait.php @@ -38,7 +38,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul // @link https://datatracker.ietf.org/doc/html/rfc7009#section-2.1 if(!in_array($type, ['access_token', 'refresh_token'], true)){ - throw new ProviderException(sprintf('invalid token type "%s"', $type)); + throw new ProviderException(sprintf('invalid token type "%s"', $type)); // @codeCoverageIgnore } $tokenToInvalidate = ($token ?? $this->storage->getAccessToken($this->name)); @@ -59,7 +59,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul // ok, let's see if we got a response body // @link https://datatracker.ietf.org/doc/html/rfc7009#section-2.2.1 if(str_contains($response->getHeaderLine('content-type'), 'json')){ - $json = MessageUtil::decodeJSON($response); + $json = MessageUtil::decodeJSON($response, true); if(isset($json['error'])){ throw new ProviderException($json['error']); diff --git a/src/Core/Utilities.php b/src/Core/Utilities.php index 7a8edd2..f32f942 100644 --- a/src/Core/Utilities.php +++ b/src/Core/Utilities.php @@ -101,7 +101,7 @@ public static function encrypt(string $data, string $keyHex, int $format = self: self::ENCRYPT_FORMAT_BINARY => $nonce.$box, self::ENCRYPT_FORMAT_BASE64 => sodium_bin2base64($nonce.$box, SODIUM_BASE64_VARIANT_ORIGINAL), self::ENCRYPT_FORMAT_HEX => sodium_bin2hex($nonce.$box), - default => throw new InvalidArgumentException('invalid format'), + default => throw new InvalidArgumentException('invalid format'), // @codeCoverageIgnore }; sodium_memzero($data); @@ -125,7 +125,7 @@ public static function decrypt(string $encrypted, string $keyHex, int $format = self::ENCRYPT_FORMAT_BINARY => $encrypted, self::ENCRYPT_FORMAT_BASE64 => sodium_base642bin($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL), self::ENCRYPT_FORMAT_HEX => sodium_hex2bin($encrypted), - default => throw new InvalidArgumentException('invalid format'), + default => throw new InvalidArgumentException('invalid format'), // @codeCoverageIgnore }; $nonce = substr($bin, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); diff --git a/src/Providers/BigCartel.php b/src/Providers/BigCartel.php index e486e95..3582bd8 100644 --- a/src/Providers/BigCartel.php +++ b/src/Providers/BigCartel.php @@ -72,7 +72,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul return true; } - return false; + return false; // @codeCoverageIgnore } /** diff --git a/src/Providers/DeviantArt.php b/src/Providers/DeviantArt.php index 8086e85..aeb7e2d 100644 --- a/src/Providers/DeviantArt.php +++ b/src/Providers/DeviantArt.php @@ -105,7 +105,7 @@ public function invalidateAccessToken(AccessToken|null $token = null, string|nul return true; } - return false; + return false; // @codeCoverageIgnore } } diff --git a/src/Storage/FileStorage.php b/src/Storage/FileStorage.php index 9d7a9ad..bf8a35b 100644 --- a/src/Storage/FileStorage.php +++ b/src/Storage/FileStorage.php @@ -203,7 +203,7 @@ protected function loadFile(string $key, string $provider):string|null{ $contents = file_get_contents($path); if($contents === false){ - throw new OAuthStorageException('file_get_contents() error'); + throw new OAuthStorageException('file_get_contents() error'); // @codeCoverageIgnore } return $contents; diff --git a/tests/Providers/Unit/BigCartelTest.php b/tests/Providers/Unit/BigCartelTest.php index 737338b..2637825 100644 --- a/tests/Providers/Unit/BigCartelTest.php +++ b/tests/Providers/Unit/BigCartelTest.php @@ -11,7 +11,7 @@ namespace chillerlan\OAuthTest\Providers\Unit; -use chillerlan\OAuth\Core\{AccessToken, TokenInvalidate}; +use chillerlan\OAuth\Core\AccessToken; use chillerlan\OAuth\Providers\BigCartel; use chillerlan\OAuthTest\Attributes\Provider; @@ -22,11 +22,6 @@ final class BigCartelTest extends OAuth2ProviderUnitTestAbstract{ public function testTokenInvalidate():void{ - - if(!$this->provider instanceof TokenInvalidate){ - $this::markTestSkipped('TokenInvalidate N/A'); - } - // BigCartel expects the account id set in the token and responds with a 204 $this->setMockResponse($this->responseFactory->createResponse(204)); @@ -37,4 +32,8 @@ public function testTokenInvalidate():void{ $this::assertFalse($this->storage->hasAccessToken($this->provider->getName())); } + public function testTokenInvalidateFailed():void{ + $this::markTestIncomplete(); + } + } diff --git a/tests/Providers/Unit/DeviantArtTest.php b/tests/Providers/Unit/DeviantArtTest.php index 526116c..d5526e6 100644 --- a/tests/Providers/Unit/DeviantArtTest.php +++ b/tests/Providers/Unit/DeviantArtTest.php @@ -50,4 +50,8 @@ public function testTokenInvalidate():void{ $this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken); } + public function testTokenInvalidateFailedWithException():void{ + $this->markTestSkipped('N/A'); + } + } diff --git a/tests/Providers/Unit/OAuth1Test.php b/tests/Providers/Unit/OAuth1Test.php index dce8b10..2f0aeab 100644 --- a/tests/Providers/Unit/OAuth1Test.php +++ b/tests/Providers/Unit/OAuth1Test.php @@ -26,4 +26,8 @@ public function testMeUnknownErrorException():void{ $this->markTestSkipped('N/A'); } + public function testTokenInvalidateFailedWithException():void{ + $this->markTestSkipped('N/A'); + } + } diff --git a/tests/Providers/Unit/OAuth2Test.php b/tests/Providers/Unit/OAuth2Test.php index c090d0b..ca894a4 100644 --- a/tests/Providers/Unit/OAuth2Test.php +++ b/tests/Providers/Unit/OAuth2Test.php @@ -26,4 +26,8 @@ public function testMeUnknownErrorException():void{ $this->markTestSkipped('N/A'); } + public function testTokenInvalidateFailedWithException():void{ + $this->markTestSkipped('N/A'); + } + } diff --git a/tests/Providers/Unit/OAuthProviderUnitTestAbstract.php b/tests/Providers/Unit/OAuthProviderUnitTestAbstract.php index 3c7fc9f..91af1fa 100644 --- a/tests/Providers/Unit/OAuthProviderUnitTestAbstract.php +++ b/tests/Providers/Unit/OAuthProviderUnitTestAbstract.php @@ -213,7 +213,7 @@ public function testTokenInvalidate():void{ $token = $this->getTestToken(); - $this->provider->storeAccessToken($this->getTestToken()); + $this->provider->storeAccessToken($token); $this::assertTrue($this->storage->hasAccessToken($this->provider->getName())); $this::assertTrue($this->provider->invalidateAccessToken()); @@ -231,6 +231,44 @@ public function testTokenInvalidate():void{ $this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken); } + public function testTokenInvalidateFailed():void{ + + if(!$this->provider instanceof TokenInvalidate){ + $this::markTestSkipped('TokenInvalidate N/A'); + } + + $token = $this->getTestToken(); + + $this->provider->storeAccessToken($token); + + $this->setMockResponse($this->responseFactory->createResponse(404)); + + $this::assertFalse($this->provider->invalidateAccessToken()); + } + + public function testTokenInvalidateFailedWithException():void{ + + if(!$this->provider instanceof TokenInvalidate){ + $this::markTestSkipped('TokenInvalidate N/A'); + } + + $this->expectException(ProviderException::class); + $this->expectExceptionMessage('whatever'); + + $token = $this->getTestToken(); + + $this->provider->storeAccessToken($token); + + $response = $this->responseFactory + ->createResponse(404) + ->withHeader('Content-Type', 'application/json') + ->withBody($this->streamFactory->createStream('{"error":"whatever"}')); + + $this->setMockResponse($response); + + $this->provider->invalidateAccessToken(); + } + public function testTokenInvalidateNoTokenException():void{ if(!$this->provider instanceof TokenInvalidate){ diff --git a/tests/Providers/Unit/StripeTest.php b/tests/Providers/Unit/StripeTest.php index d848ea0..4dd961d 100644 --- a/tests/Providers/Unit/StripeTest.php +++ b/tests/Providers/Unit/StripeTest.php @@ -24,4 +24,12 @@ public function testTokenInvalidate():void{ $this::markTestIncomplete(); } + public function testTokenInvalidateFailed():void{ + $this::markTestIncomplete(); + } + + public function testTokenInvalidateFailedWithException():void{ + $this::markTestIncomplete(); + } + } diff --git a/tests/Providers/Unit/VimeoTest.php b/tests/Providers/Unit/VimeoTest.php index f73d546..4695a91 100644 --- a/tests/Providers/Unit/VimeoTest.php +++ b/tests/Providers/Unit/VimeoTest.php @@ -50,4 +50,8 @@ public function testTokenInvalidate():void{ $this::assertSame('still here', $this->provider->getStorage()->getAccessToken($this->provider->getName())->accessToken); } + public function testTokenInvalidateFailedWithException():void{ + $this->markTestSkipped('N/A'); + } + }