在PHP中,我试图使用AWS的RSA公钥(我从https://cognito-identity.amazonaws.com/.well-known/jwks_uri的模数/指数生成)来验证AWS auth令牌(从getOpenIdTokenForDeveloperIdentity返回的JWT)。关键开始于相应的页眉/页脚-----BEGIN RSA PUBLIC KEY-----
等。我已经看过几个PHP库,如Emarref\Jwt\Jwt
,但是我得到错误:error:0906D06C:PEM routines:PEM_read_bio:no start line
。这一切归结到基本的PHP功能:openssl_verify
。使用RSA公钥验证JWT签名PHP
我已经看过php.net/manual for openssl-verify,但我仍然不清楚参数的详细信息。所需的算法是RS512
。
我能够使用node.js验证JWT令牌没有问题(相同的密钥和令牌)。为此,我使用了库:https://github.com/auth0/node-jsonwebtoken
不知道为什么这在PHP中不起作用。我不能使用RSA公钥吗?
function verifyKey($public_key) {
$jwt = new Emarref\Jwt\Jwt();
$algorithm = new Emarref\Jwt\Algorithm\Rs512();
$factory = new Emarref\Jwt\Encryption\Factory();
$encryption = $factory->create($algorithm);
$encryption->setPublicKey($public_key);
$context = new Emarref\Jwt\Verification\Context($encryption);
$token = $jwt->deserialize($authToken);
try {
$jwt->verify($token, $context);
} catch (Emarref\Jwt\Exception\VerificationException $e) {
debug($e->getMessage());
}
}