我在哪里可以找到来自cognito的JWT的密钥

时间:2016-06-30 09:52:06

标签: amazon-web-services amazon-cognito aws-cognito

我正在为我的Web App尝试Cognito用户池的登录功能。我能够获得令牌,但我不知道在哪里可以找到解密它的秘密。我在其中一篇文章中读到,秘密是用户池中应用程序的秘密ID。但是,对于Javascript SDK,密码ID为空。这是否意味着我的秘密也应该是空白的?我尝试了这个但是我收到一条消息“错误:PEM_read_bio_PUBKEY失败”。

4 个答案:

答案 0 :(得分:11)

要纠正其他答案:RS256是一种非对称算法,需要公钥和私钥。另请参阅RS256 vs HS256: What's the difference?https://en.wikipedia.org/wiki/RSA_(cryptosystem)

正确的是,为了验证JWT,您不需要用于签名的私钥,只有AWS在sealed下提供的公钥。

答案 1 :(得分:1)

AWS使用RS256算法,该算法不需要保密但公钥可以解码。

您可以在这里找到游泳池的JWKS:https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json(参见http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html#amazon-cognito-identity-user-pools-using-id-and-access-tokens-in-web-api

这里描述了将JWK转换为公钥的过程:https://mobile.awsblog.com/post/Tx3JK25U7Z9EUIU/Integrating-Amazon-Cognito-User-Pools-with-API-Gateway(在“理解代码”部分下)。

答案 2 :(得分:1)

只想用代码片段来总结这个主题:

const jwkToPem = require('jwk-to-pem');
const requestify = require('requestify');

/**
 * Get cognito's secret key
 * @param {String} region
 * @param {String} userPoolId
 * @returns {Promise}
 */
function getPem(region, userPoolId) {
  const jwkUrl = `https://cognito-idp.${region}.amazonaws.com/${userPoolId}/.well-known/jwks.json`;

  return requestify.request(jwkUrl, { method: 'get', dataType: 'json'})
    .then(res => res.getBody()['keys'].shift())
    .then(jwk => jwkToPem(jwk))
  ;
}

答案 3 :(得分:0)

我也试图解决所有这类问题。

为此,我提出了如何做这些对我有用的事情的例子

1)Verifying a JWT token with a 'secret' - aka Issuers RSA public key

2)Using an Issuers public SSL certificate to verify JWT tokens and other signatures

相关问题