Skip to content

Commit e0753f9

Browse files
authored
feat: support phpseclib3 (#2019)
1 parent 8ed1dc8 commit e0753f9

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"google/apiclient-services": "~0.13",
1212
"firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0",
1313
"monolog/monolog": "^1.17|^2.0",
14-
"phpseclib/phpseclib": "~2.0",
14+
"phpseclib/phpseclib": "~2.0||^3.0.2",
1515
"guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0",
1616
"guzzlehttp/psr7": "^1.2"
1717
},

src/AccessToken/Verify.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Firebase\JWT\SignatureInvalidException;
2323
use GuzzleHttp\Client;
2424
use GuzzleHttp\ClientInterface;
25+
use phpseclib3\Crypt\PublicKeyLoader;
26+
use phpseclib3\Crypt\RSA\PublicKey;
2527
use Psr\Cache\CacheItemPoolInterface;
2628
use Google\Auth\Cache\MemoryCacheItemPool;
2729
use Google\Exception as GoogleException;
@@ -97,18 +99,10 @@ public function verifyIdToken($idToken, $audience = null)
9799
// Check signature
98100
$certs = $this->getFederatedSignOnCerts();
99101
foreach ($certs as $cert) {
100-
$bigIntClass = $this->getBigIntClass();
101-
$rsaClass = $this->getRsaClass();
102-
$modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256);
103-
$exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256);
104-
105-
$rsa = new $rsaClass();
106-
$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
107-
108102
try {
109103
$payload = $this->jwt->decode(
110104
$idToken,
111-
$rsa->getPublicKey(),
105+
$this->getPublicKey($cert),
112106
array('RS256')
113107
);
114108

@@ -229,8 +223,33 @@ private function getJwtService()
229223
return new $jwtClass;
230224
}
231225

226+
private function getPublicKey($cert)
227+
{
228+
$bigIntClass = $this->getBigIntClass();
229+
$modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256);
230+
$exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256);
231+
$component = array('n' => $modulus, 'e' => $exponent);
232+
233+
if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) {
234+
/** @var PublicKey $loader */
235+
$loader = PublicKeyLoader::load($component);
236+
237+
return $loader->toString('PKCS8');
238+
}
239+
240+
$rsaClass = $this->getRsaClass();
241+
$rsa = new $rsaClass();
242+
$rsa->loadKey($component);
243+
244+
return $rsa->getPublicKey();
245+
}
246+
232247
private function getRsaClass()
233248
{
249+
if (class_exists('phpseclib3\Crypt\RSA')) {
250+
return 'phpseclib3\Crypt\RSA';
251+
}
252+
234253
if (class_exists('phpseclib\Crypt\RSA')) {
235254
return 'phpseclib\Crypt\RSA';
236255
}
@@ -240,6 +259,10 @@ private function getRsaClass()
240259

241260
private function getBigIntClass()
242261
{
262+
if (class_exists('phpseclib3\Math\BigInteger')) {
263+
return 'phpseclib3\Math\BigInteger';
264+
}
265+
243266
if (class_exists('phpseclib\Math\BigInteger')) {
244267
return 'phpseclib\Math\BigInteger';
245268
}
@@ -249,6 +272,10 @@ private function getBigIntClass()
249272

250273
private function getOpenSslConstant()
251274
{
275+
if (class_exists('phpseclib3\Crypt\AES')) {
276+
return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL';
277+
}
278+
252279
if (class_exists('phpseclib\Crypt\RSA')) {
253280
return 'phpseclib\Crypt\RSA::MODE_OPENSSL';
254281
}

tests/Google/AccessToken/VerifyTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ private function getJwtService()
139139

140140
private function getOpenSslConstant()
141141
{
142+
if (class_exists('phpseclib3\Crypt\AES')) {
143+
return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL';
144+
}
145+
142146
if (class_exists('phpseclib\Crypt\RSA')) {
143147
return 'phpseclib\Crypt\RSA::MODE_OPENSSL';
144148
}

0 commit comments

Comments
 (0)