【发布时间】:2025-12-04 05:50:01
【问题描述】:
在 PHP 中,我正在尝试使用 AWS 的 RSA 公钥(我从 https://cognito-identity.amazonaws.com/.well-known/jwks_uri 处的模数/指数生成)验证 AWS 身份验证令牌(从 getOpenIdTokenForDeveloperIdentity 返回的 JWT)。关键以适当的页眉/页脚-----BEGIN RSA PUBLIC KEY----- 等开头。我查看了一些PHP 库,例如Emarref\Jwt\Jwt,但是我得到了错误:error:0906D06C:PEM routines:PEM_read_bio:no start line。这一切都归结为基本的 php 函数:openssl_verify。
我已经查看了 openssl-verify 的 php.net/manual,但我仍然不清楚参数详细信息。需要的算法是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());
}
}
【问题讨论】:
标签: php amazon-web-services rsa jwt php-openssl