diff --git a/src/AccessToken.php b/src/AccessToken.php index 0afc4ca1e..b7eebb5fa 100644 --- a/src/AccessToken.php +++ b/src/AccessToken.php @@ -38,6 +38,7 @@ use SimpleJWT\JWT as SimpleJWT; use SimpleJWT\Keys\KeyFactory; use SimpleJWT\Keys\KeySet; +use TypeError; use UnexpectedValueException; /** @@ -399,6 +400,10 @@ private function checkAndInitializePhpsec() } } + /** + * @return string + * @throws TypeError If the key cannot be initialized to a string. + */ private function loadPhpsecPublicKey(string $modulus, string $exponent): string { if (class_exists(RSA::class) && class_exists(BigInteger2::class)) { @@ -421,7 +426,11 @@ private function loadPhpsecPublicKey(string $modulus, string $exponent): string $exponent ]), 256), ]); - return $key->toString('PKCS8'); + $formattedPublicKey = $key->toString('PKCS8'); + if (!is_string($formattedPublicKey)) { + throw new TypeError('Failed to initialize the key'); + } + return $formattedPublicKey; } /**