|
23 | 23 | use Exception;
|
24 | 24 | use ExpiredException;
|
25 | 25 | use Firebase\JWT\ExpiredException as ExpiredExceptionV3;
|
| 26 | +use Firebase\JWT\JWT; |
26 | 27 | use Firebase\JWT\Key;
|
27 | 28 | use Firebase\JWT\SignatureInvalidException;
|
28 | 29 | use Google\Auth\Cache\MemoryCacheItemPool;
|
|
31 | 32 | use GuzzleHttp\ClientInterface;
|
32 | 33 | use InvalidArgumentException;
|
33 | 34 | use LogicException;
|
| 35 | +use phpseclib3\Crypt\AES; |
34 | 36 | use phpseclib3\Crypt\PublicKeyLoader;
|
35 |
| -use phpseclib3\Crypt\RSA\PublicKey; // Firebase v2 |
| 37 | +use phpseclib3\Math\BigInteger; |
36 | 38 | use Psr\Cache\CacheItemPoolInterface;
|
37 | 39 |
|
38 | 40 | /**
|
@@ -219,101 +221,43 @@ private function getFederatedSignOnCerts()
|
219 | 221 |
|
220 | 222 | private function getJwtService()
|
221 | 223 | {
|
222 |
| - $jwtClass = 'JWT'; |
223 |
| - if (class_exists('\Firebase\JWT\JWT')) { |
224 |
| - $jwtClass = 'Firebase\JWT\JWT'; |
225 |
| - } |
226 |
| - |
227 |
| - if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { |
| 224 | + $jwt = new JWT(); |
| 225 | + if ($jwt::$leeway < 1) { |
228 | 226 | // Ensures JWT leeway is at least 1
|
229 | 227 | // @see https://github.com/google/google-api-php-client/issues/827
|
230 |
| - $jwtClass::$leeway = 1; |
| 228 | + $jwt::$leeway = 1; |
231 | 229 | }
|
232 | 230 |
|
233 |
| - // @phpstan-ignore-next-line |
234 |
| - return new $jwtClass(); |
| 231 | + return $jwt; |
235 | 232 | }
|
236 | 233 |
|
237 | 234 | private function getPublicKey($cert)
|
238 | 235 | {
|
239 |
| - $bigIntClass = $this->getBigIntClass(); |
240 |
| - $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); |
241 |
| - $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); |
| 236 | + $modulus = new BigInteger($this->jwt->urlsafeB64Decode($cert['n']), 256); |
| 237 | + $exponent = new BigInteger($this->jwt->urlsafeB64Decode($cert['e']), 256); |
242 | 238 | $component = ['n' => $modulus, 'e' => $exponent];
|
243 | 239 |
|
244 |
| - if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) { |
245 |
| - /** @var PublicKey $loader */ |
246 |
| - $loader = PublicKeyLoader::load($component); |
247 |
| - |
248 |
| - return $loader->toString('PKCS8'); |
249 |
| - } |
250 |
| - |
251 |
| - $rsaClass = $this->getRsaClass(); |
252 |
| - $rsa = new $rsaClass(); |
253 |
| - $rsa->loadKey($component); |
254 |
| - |
255 |
| - return $rsa->getPublicKey(); |
256 |
| - } |
257 |
| - |
258 |
| - private function getRsaClass() |
259 |
| - { |
260 |
| - if (class_exists('phpseclib3\Crypt\RSA')) { |
261 |
| - return 'phpseclib3\Crypt\RSA'; |
262 |
| - } |
263 |
| - |
264 |
| - if (class_exists('phpseclib\Crypt\RSA')) { |
265 |
| - return 'phpseclib\Crypt\RSA'; |
266 |
| - } |
| 240 | + $loader = PublicKeyLoader::load($component); |
267 | 241 |
|
268 |
| - return 'Crypt_RSA'; |
269 |
| - } |
270 |
| - |
271 |
| - private function getBigIntClass() |
272 |
| - { |
273 |
| - if (class_exists('phpseclib3\Math\BigInteger')) { |
274 |
| - return 'phpseclib3\Math\BigInteger'; |
275 |
| - } |
276 |
| - |
277 |
| - if (class_exists('phpseclib\Math\BigInteger')) { |
278 |
| - return 'phpseclib\Math\BigInteger'; |
279 |
| - } |
280 |
| - |
281 |
| - return 'Math_BigInteger'; |
282 |
| - } |
283 |
| - |
284 |
| - private function getOpenSslConstant() |
285 |
| - { |
286 |
| - if (class_exists('phpseclib3\Crypt\AES')) { |
287 |
| - return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; |
288 |
| - } |
289 |
| - |
290 |
| - if (class_exists('phpseclib\Crypt\RSA')) { |
291 |
| - return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; |
292 |
| - } |
293 |
| - |
294 |
| - if (class_exists('Crypt_RSA')) { |
295 |
| - return 'CRYPT_RSA_MODE_OPENSSL'; |
296 |
| - } |
297 |
| - |
298 |
| - throw new Exception('Cannot find RSA class'); |
| 242 | + return $loader->toString('PKCS8'); |
299 | 243 | }
|
300 | 244 |
|
301 | 245 | /**
|
302 |
| - * phpseclib calls "phpinfo" by default, which requires special |
303 |
| - * whitelisting in the AppEngine VM environment. This function |
304 |
| - * sets constants to bypass the need for phpseclib to check phpinfo |
305 |
| - * |
306 |
| - * @see phpseclib/Math/BigInteger |
307 |
| - * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 |
308 |
| - */ |
| 246 | + * phpseclib calls "phpinfo" by default, which requires special |
| 247 | + * whitelisting in the AppEngine VM environment. This function |
| 248 | + * sets constants to bypass the need for phpseclib to check phpinfo |
| 249 | + * |
| 250 | + * @see phpseclib/Math/BigInteger |
| 251 | + * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 |
| 252 | + */ |
309 | 253 | private function setPhpsecConstants()
|
310 | 254 | {
|
311 | 255 | if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) {
|
312 | 256 | if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) {
|
313 | 257 | define('MATH_BIGINTEGER_OPENSSL_ENABLED', true);
|
314 | 258 | }
|
315 | 259 | if (!defined('CRYPT_RSA_MODE')) {
|
316 |
| - define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); |
| 260 | + define('CRYPT_RSA_MODE', AES::ENGINE_OPENSSL); |
317 | 261 | }
|
318 | 262 | }
|
319 | 263 | }
|
|
0 commit comments