2222use Firebase \JWT \SignatureInvalidException ;
2323use GuzzleHttp \Client ;
2424use GuzzleHttp \ClientInterface ;
25+ use phpseclib3 \Crypt \PublicKeyLoader ;
26+ use phpseclib3 \Crypt \RSA \PublicKey ;
2527use Psr \Cache \CacheItemPoolInterface ;
2628use Google \Auth \Cache \MemoryCacheItemPool ;
2729use 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 }
0 commit comments