我正在使用“github.com/lestrrat/go-jwx/jwk”来验证Jason Web Tokens(JWT)。 https://github.com/lestrrat/go-jwx
JWK库具有从URL加载密钥的强大功能,但这将在无服务器环境中运行,因此我想缓存密钥而不是像这样获取密钥:
set, err := jwk.Fetch(keyUrl)
我正在试图弄清楚如何直接加载它们,或者从我已经拥有的键创建它们。
密钥是JSON格式。
{"keys":
[{"alg":"RS256","e":"AQAB","n":"jqm5oX5Vth4JW1gZQHywIki2beYCgBSL-
EYlefDUlI6SShtEKfi-vWYbFh2pNNUAE4NHuYpYP-
FG1uRSKs6WK2k6KMB2Hyx3hBkWyu7Aqo_pb1WItkPSZS-AWOMp4N-
我只是找不到如何做到这一点的例子。必须有办法
答案 0 :(得分:1)
如果其他人对此感到困惑,虽然我认为这不太可能,感谢Stephen和Jonah的耐心等待。
jwk.Parse正是我所需要的。甚至还有一个字符串选项。
set, err := jwk.ParseString(`{"keys":[{"alg":"RS256","e":"AQAB","n":"jqm5oX5Vth4JW1gZQHywIki2beYCgBSL-EYlefDUlI6SShtEKfi-vWYbFh2pNNUAE4NHuYpYP-FG1uRSKs6WK2k6KMB2Hyx3hBkWyu7Aqo_pb1WItkPSZS-AWOMp4N-ENPGyNQ4u2ill17ZwBKt1wgDvdaWpNaqPEcSaUNSXA7ZD5scFFnZt36r6crhgK7vHXChhXU3nlKEN2OwFdyEGdX01l-HxgPk0HPIfnUyYxuGI_CL-7aV_rf3KY4uw6yNfEO0jom0NZ-I8F1NzpbaDxj4hD4xlDZIQI9PxLq4nAtb4ZVQC1xhnRv-uufs9Wa4XFjs6jJSWGcWV1Z7K5HmMaw","kid":"tNtnDgeXepbf2NZlVjzKGdtPU9mnOidtw0puo3s-_8Y","kty":"RSA","use":"sig"},{"alg":"RS256","e":"AQAB","n":"o_CYYN5Lo9XuCRADNxqDTVolcWshv7LD7EmwFCvtbdHErvfOQPHyAFMLMDKracToe2Rg7cBihYC_wgHRItXAgxAM6P7mNT_DvbLpp2HtbMtmYLtWyGqHJsJQE9gNSOMsxEugxq9lpGjROiU2nZm7TZ-QwVV-F7dfBl0NBVy5oFbTCRUDxFcKOr_zh3xK-fRIsJbqbB4VPt8IhA_GjvjyoMlH0pfhoRqDMWk8vLXHhRxYlT5bZP1PDulFMGGO8KH7Cvh4D0cZE9mSiUFyVlDDzCbAH88a0TAGYkqERdVfHBYaTsCCseBqsvUoqbdzQwd2fOLf-N0jpSdNMNxHGNwl3w","kid":"ddPUDDyUpH2N5wGSXvnqQZy-OmTFSVu5PYansjpsoAs","kty":"RSA","use":"sig"}]}`)