我找到了一个关于如何 verify Cognito access tokens with Python 的示例。我如何使用 NodeJS 做同样的事情? SDK没有这个功能吗?
到目前为止我已经
authorizeCognitoJwt(token) {
const COGNITO_POOL_ID = 'ap-southeast-1_xxx'
const COGNITO_JWT_SET = {
'keys': [
{
'alg': 'RS256',
'e': 'AQAB',
'kid': 'ChkV+...=',
'kty': 'RSA',
'n': 'tkjexS...johc5Q',
'use': 'sig'
},
{
'alg': 'RS256',
'e': 'AQAB',
'kid': 'Ve...Eb8dw6Y=',
'kty': 'RSA',
'n': 'hW19H...0c9Q',
'use': 'sig'
}
]
}
const decodedJwt = jwt.decode(token, {complete: true})
console.log(decodedJwt)
if (decodedJwt.payload.iss !== `https://cognito-idp.us-east-1.amazonaws.com/${COGNITO_POOL_ID}`) {
return 'INVALID_ISSUER'
}
if (decodedJwt.payload.token_use !== 'access') {
return 'INVALID_TOKEN_USE'
}
var jwtKey = COGNITO_JWT_SET.keys.find(k => k.kid === decodedJwt.header.kid)
if (!jwtKey) {
return 'INVALID_TOKEN_KID'
}
var verifiedKey = jwt.verify(token, /* how do I get the key? */)
return 'VALID'
}
但我不知道如何从 COGNITO_JWT_SET
获取 key
最佳答案
您可以使用 this 获取 COGNITO_JWT_SET
网址。
请参阅博客文章 Integrating Amazon Cognito User Pools with API Gateway在 AWS 移动博客中获取包含代码的完整示例。
关于node.js - 如何在 NodeJS 上验证 AWS Cognito 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45778457/