简体   繁体   English

如何在NodeJS上验证AWS Cognito访问令牌

[英]How to verify AWS Cognito Access Token on NodeJS

I found an example on how to verify Cognito access tokens with Python . 我找到了一个有关如何使用Python验证Cognito访问令牌的示例。 How do I do the same with NodeJS? 如何使用NodeJS进行相同操作? Is there no SDK function to do this? 没有SDK函数可以执行此操作吗?

So far I have 到目前为止,我有

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'
}

But am stuck at how do I get keys from COGNITO_JWT_SET 但是我被困在如何从COGNITO_JWT_SET获取密钥

You can get the COGNITO_JWT_SET by using this URL. 您可以使用 URL获得COGNITO_JWT_SET

Refer the blog post Integrating Amazon Cognito User Pools with API Gateway in AWS Mobile Blog for a complete example with code. 有关带代码的完整示例,请参阅博客文章AWS Mobile Blog中的将Amazon Cognito用户池与API网关集成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM