Skip to content

Commit

Permalink
chore: remove support for previous firebase namespace (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Mar 24, 2022
1 parent 888ee55 commit 31e5d24
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 54 deletions.
4 changes: 1 addition & 3 deletions src/AccessToken.php
Expand Up @@ -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);
}

Expand Down
23 changes: 8 additions & 15 deletions src/OAuth2.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}

/**
Expand Down
8 changes: 2 additions & 6 deletions tests/Credentials/ServiceAccountCredentialsTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
39 changes: 9 additions & 30 deletions tests/OAuth2Test.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand All @@ -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(
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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
Expand Down Expand Up @@ -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']);
}
Expand All @@ -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']);
}
Expand All @@ -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);
}
}

0 comments on commit 31e5d24

Please sign in to comment.