Skip to content

Commit 6abb550

Browse files
committed
bump google auth to v0.4 and add JWT3.0 support as a result
1 parent 84992b7 commit 6abb550

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"license": "Apache-2.0",
88
"require": {
99
"php": ">=5.4",
10-
"google/auth": "v0.3",
10+
"google/auth": "~v0.4",
11+
"firebase/php-jwt": "~2.0|~3.0",
1112
"monolog/monolog": "^1.17",
1213
"phpseclib/phpseclib": "~2.0",
1314
"guzzlehttp/guzzle": "5.2.*"

src/Google/AccessToken/Verify.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function __construct(ClientInterface $http = null, CacheInterface $cache
5454

5555
$this->http = $http;
5656
$this->cache = $cache;
57+
$this->jwt = $this->getJwtService();
5758
}
5859

5960
/**
@@ -74,14 +75,14 @@ public function verifyIdToken($idToken, $audience = null)
7475
// Check signature
7576
$certs = $this->getFederatedSignonCerts();
7677
foreach ($certs as $cert) {
77-
$modulus = new BigInteger(JWT::urlsafeB64Decode($cert['n']), 256);
78-
$exponent = new BigInteger(JWT::urlsafeB64Decode($cert['e']), 256);
78+
$modulus = new BigInteger($this->jwt->urlsafeB64Decode($cert['n']), 256);
79+
$exponent = new BigInteger($this->jwt->urlsafeB64Decode($cert['e']), 256);
7980

8081
$rsa = new RSA();
8182
$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
8283

8384
try {
84-
$payload = JWT::decode(
85+
$payload = $this->jwt->decode(
8586
$idToken,
8687
$rsa->getPublicKey(),
8788
array('RS256')
@@ -185,4 +186,13 @@ private function getFederatedSignOnCerts()
185186

186187
return $certs['keys'];
187188
}
189+
190+
private function getJwtService()
191+
{
192+
if (class_exists('\Firebase\JWT\JWT')) {
193+
return new \Firebase\JWT\JWT;
194+
}
195+
196+
return new \JWT;
197+
}
188198
}

tests/Google/AccessToken/VerifyTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testValidateIdToken()
3333
{
3434
$this->checkToken();
3535

36+
$jwt = $this->getJwtService();
3637
$client = $this->getClient();
3738
$token = $client->getAccessToken();
3839
if ($client->isAccessTokenExpired()) {
@@ -41,7 +42,7 @@ public function testValidateIdToken()
4142
$segments = explode('.', $token['id_token']);
4243
$this->assertEquals(3, count($segments));
4344
// Extract the client ID in this case as it wont be set on the test client.
44-
$data = json_decode(JWT::urlSafeB64Decode($segments[1]));
45+
$data = json_decode($jwt->urlSafeB64Decode($segments[1]));
4546
$verify = new Google_AccessToken_Verify();
4647
$payload = $verify->verifyIdToken($token['id_token'], $data->aud);
4748
$this->assertTrue(isset($payload['sub']));
@@ -51,10 +52,19 @@ public function testValidateIdToken()
5152
// caching for this test to make sense. Not sure how to do that
5253
// at the moment.
5354
$client = $this->getClient();
54-
$data = json_decode(JWT::urlSafeB64Decode($segments[1]));
55+
$data = json_decode($jwt->urlSafeB64Decode($segments[1]));
5556
$verify = new Google_AccessToken_Verify();
5657
$payload = $verify->verifyIdToken($token['id_token'], $data->aud);
5758
$this->assertTrue(isset($payload['sub']));
5859
$this->assertTrue(strlen($payload['sub']) > 0);
5960
}
61+
62+
private function getJwtService()
63+
{
64+
if (class_exists('\Firebase\JWT\JWT')) {
65+
return new \Firebase\JWT\JWT;
66+
}
67+
68+
return new \JWT;
69+
}
6070
}

0 commit comments

Comments
 (0)