如何从JWK密钥集中选择有效密钥进行Apple登录令牌验证?

问题描述 投票:0回答:1

我正在尝试使用API​​验证苹果identityToken。我正在使用firebase/php-jwt库。

我完成了以下代码。

$access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_keys = file_get_contents('https://appleid.apple.com/auth/keys');

$public_keys = JWK::parseKeySet(json_decode($auth_keys, true));
$keys = array_keys($public_keys);

$decoded = JWT::decode($access_token, $public_keys[$keys[0]], ['RS256']);
$decoded_array = (array) $decoded;

echo '<pre>' . print_r($decoded_array, true) . '</pre>';

当我第一次运行代码时,它成功运行。但第二次返回“签名验证失败”。所以我只是从$public_keys[$keys[0]]更改为$public_keys[$keys[1]],所以它可以正常工作。但是如果我尝试再次登录,则无法正常工作。

密钥选择有问题吗?我不知道该如何选择。我尝试了很多搜索,但是没有找到合适的解决方案,因此希望从这里获得帮助。

提前谢谢您

php firebase jwt php-jwt
1个回答
0
投票
$access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
list($headb64, $bodyb64, $cryptob64) = explode('.', $access_token);
$header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64));

$kid = $header->kid;
© www.soinside.com 2019 - 2024. All rights reserved.