From 31e5d24d5fa0eaf6adc7e596292dc4732f4b60c5 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 24 Mar 2022 14:22:45 -0700 Subject: [PATCH] chore: remove support for previous firebase namespace (#382) --- src/AccessToken.php | 4 +- src/OAuth2.php | 23 ++++------- .../ServiceAccountCredentialsTest.php | 8 +--- tests/OAuth2Test.php | 39 +++++-------------- 4 files changed, 20 insertions(+), 54 deletions(-) diff --git a/src/AccessToken.php b/src/AccessToken.php index 1ab9b1517..99d208ce4 100644 --- a/src/AccessToken.php +++ b/src/AccessToken.php @@ -444,9 +444,7 @@ private function setPhpsecConstants() */ protected function callJwtStatic($method, array $args = []) { - $class = class_exists('Firebase\JWT\JWT') - ? 'Firebase\JWT\JWT' - : 'JWT'; + $class = 'Firebase\JWT\JWT'; return call_user_func_array([$class, $method], $args); } diff --git a/src/OAuth2.php b/src/OAuth2.php index 9c7c659ed..5b6b4ec74 100644 --- a/src/OAuth2.php +++ b/src/OAuth2.php @@ -17,6 +17,7 @@ namespace Google\Auth; +use Firebase\JWT\JWT; use Google\Auth\HttpHandler\HttpClientCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use GuzzleHttp\Psr7\Query; @@ -1381,25 +1382,17 @@ private function coerceUri($uri) */ private function jwtDecode($idToken, $publicKey, $allowedAlgs) { - if (class_exists('Firebase\JWT\JWT')) { - return \Firebase\JWT\JWT::decode($idToken, $publicKey, $allowedAlgs); - } - - return \JWT::decode($idToken, $publicKey, $allowedAlgs); + return JWT::decode($idToken, $publicKey, $allowedAlgs); } private function jwtEncode($assertion, $signingKey, $signingAlgorithm, $signingKeyId = null) { - if (class_exists('Firebase\JWT\JWT')) { - return \Firebase\JWT\JWT::encode( - $assertion, - $signingKey, - $signingAlgorithm, - $signingKeyId - ); - } - - return \JWT::encode($assertion, $signingKey, $signingAlgorithm, $signingKeyId); + return JWT::encode( + $assertion, + $signingKey, + $signingAlgorithm, + $signingKeyId + ); } /** diff --git a/tests/Credentials/ServiceAccountCredentialsTest.php b/tests/Credentials/ServiceAccountCredentialsTest.php index e6d49fd43..af8896388 100644 --- a/tests/Credentials/ServiceAccountCredentialsTest.php +++ b/tests/Credentials/ServiceAccountCredentialsTest.php @@ -18,6 +18,7 @@ namespace Google\Auth\Tests\Credentials; use DomainException; +use Firebase\JWT\JWT; use Google\Auth\ApplicationDefaultCredentials; use Google\Auth\Credentials\ServiceAccountCredentials; use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials; @@ -799,12 +800,7 @@ public function testJwtAccessFromApplicationDefault() $token = str_replace('Bearer ', '', $metadata['authorization'][0]); $key = file_get_contents(__DIR__ . '/../fixtures3/key.pub'); - $class = 'JWT'; - if (class_exists('Firebase\JWT\JWT')) { - $class = 'Firebase\JWT\JWT'; - } - $jwt = new $class(); - $result = $jwt::decode($token, $key, ['RS256']); + $result = JWT::decode($token, $key, ['RS256']); $this->assertEquals($authUri, $result->aud); } diff --git a/tests/OAuth2Test.php b/tests/OAuth2Test.php index efdc1c739..5b9719279 100644 --- a/tests/OAuth2Test.php +++ b/tests/OAuth2Test.php @@ -18,6 +18,7 @@ namespace Google\Auth\Tests; use DomainException; +use Firebase\JWT\JWT; use Google\Auth\OAuth2; use GuzzleHttp\Psr7\Query; use GuzzleHttp\Psr7\Utils; @@ -449,7 +450,7 @@ public function testCanHS256EncodeAValidPayloadWithSigningKeyId() $testConfig['signingKeyId'] = 'example_key_id2'; $o = new OAuth2($testConfig); $payload = $o->toJwt(); - $roundTrip = $this->jwtDecode($payload, $keys, array('HS256')); + $roundTrip = JWT::decode($payload, $keys, array('HS256')); $this->assertEquals($roundTrip->iss, $testConfig['issuer']); $this->assertEquals($roundTrip->aud, $testConfig['audience']); $this->assertEquals($roundTrip->scope, $testConfig['scope']); @@ -467,7 +468,7 @@ public function testFailDecodeWithoutSigningKeyId() $payload = $o->toJwt(); try { - $this->jwtDecode($payload, $keys, array('HS256')); + JWT::decode($payload, $keys, array('HS256')); } catch (\Exception $e) { // Workaround: In old JWT versions throws DomainException $this->assertTrue( @@ -484,7 +485,7 @@ public function testCanHS256EncodeAValidPayload() $testConfig = $this->signingMinimal; $o = new OAuth2($testConfig); $payload = $o->toJwt(); - $roundTrip = $this->jwtDecode($payload, $testConfig['signingKey'], array('HS256')); + $roundTrip = JWT::decode($payload, $testConfig['signingKey'], array('HS256')); $this->assertEquals($roundTrip->iss, $testConfig['issuer']); $this->assertEquals($roundTrip->aud, $testConfig['audience']); $this->assertEquals($roundTrip->scope, $testConfig['scope']); @@ -499,7 +500,7 @@ public function testCanRS256EncodeAValidPayload() $o->setSigningAlgorithm('RS256'); $o->setSigningKey($privateKey); $payload = $o->toJwt(); - $roundTrip = $this->jwtDecode($payload, $publicKey, array('RS256')); + $roundTrip = JWT::decode($payload, $publicKey, array('RS256')); $this->assertEquals($roundTrip->iss, $testConfig['issuer']); $this->assertEquals($roundTrip->aud, $testConfig['audience']); $this->assertEquals($roundTrip->scope, $testConfig['scope']); @@ -516,20 +517,9 @@ public function testCanHaveAdditionalClaims() $o->setSigningAlgorithm('RS256'); $o->setSigningKey($privateKey); $payload = $o->toJwt(); - $roundTrip = $this->jwtDecode($payload, $publicKey, array('RS256')); + $roundTrip = JWT::decode($payload, $publicKey, array('RS256')); $this->assertEquals($roundTrip->target_audience, $targetAud); } - - private function jwtDecode() - { - $args = func_get_args(); - $class = 'JWT'; - if (class_exists('Firebase\JWT\JWT')) { - $class = 'Firebase\JWT\JWT'; - } - - return call_user_func_array("$class::decode", $args); - } } class OAuth2GenerateAccessTokenRequestTest extends TestCase @@ -931,7 +921,7 @@ public function testFailsIfAudienceIsMissing() 'iat' => $now, ]; $o = new OAuth2($testConfig); - $jwtIdToken = $this->jwtEncode($origIdToken, $this->privateKey, 'RS256'); + $jwtIdToken = JWT::encode($origIdToken, $this->privateKey, 'RS256'); $o->setIdToken($jwtIdToken); $o->verifyIdToken($this->publicKey, ['RS256']); } @@ -948,7 +938,7 @@ public function testFailsIfAudienceIsWrong() 'iat' => $now, ]; $o = new OAuth2($testConfig); - $jwtIdToken = $this->jwtEncode($origIdToken, $this->privateKey, 'RS256'); + $jwtIdToken = JWT::encode($origIdToken, $this->privateKey, 'RS256'); $o->setIdToken($jwtIdToken); $o->verifyIdToken($this->publicKey, ['RS256']); } @@ -965,20 +955,9 @@ public function testShouldReturnAValidIdToken() ]; $o = new OAuth2($testConfig); $alg = 'RS256'; - $jwtIdToken = $this->jwtEncode($origIdToken, $this->privateKey, $alg); + $jwtIdToken = JWT::encode($origIdToken, $this->privateKey, $alg); $o->setIdToken($jwtIdToken); $roundTrip = $o->verifyIdToken($this->publicKey, array($alg)); $this->assertEquals($origIdToken['aud'], $roundTrip->aud); } - - private function jwtEncode() - { - $args = func_get_args(); - $class = 'JWT'; - if (class_exists('Firebase\JWT\JWT')) { - $class = 'Firebase\JWT\JWT'; - } - - return call_user_func_array("$class::encode", $args); - } }