2017-04-06 119 views
0

我试图弄清楚如何验证从AWS Cognito Identity authenticateUser调用中获取的用户IDToken。验证AWS Cognito针对JWK设置的JWT IDToken使用njwt

按照以下步骤找到:https://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我已经能够达到拥有用户ID令牌的地步,并且我已经解码了标头和正文。

给定一个IDToken这就是我的头和身体的样子:

头:

{ 
    typ: 'JWT', 
    alg: 'RS256', 
    kid: '...' 
} 

体:

{ 
    sub: 'abcd...', 
    aud: 'abcdefg...', 
    email_verified: true, 
    token_use: 'id', 
    auth_time: 1491491773, 
    iss: 'https://cognito-idp.us-east-1.amazonaws.com/us-east-...', 
    'cognito:username': 'username', 
    exp: 1491495373, 
    iat: 1491491773, 
    email: '[email protected]' 
} 

然后IDToken的第三部分是签名:

'T6tjQ...' // big long encoded string 

我卡在的部分实际上是对照我的签名密钥验证签名。我似乎无法让这部分工作。现在我试图使用njwt节点模块:https://www.npmjs.com/package/njwt

鉴于IDToken为3部分.分离base64编码字符串,如secretKey以下Javascript对象:

{ 
    alg: 'RS256', 
    e: '...', 
    kid: '...', // matches kid of IDToken 
    kty: 'RSA', 
    n: 'abcdefg...', // Appears to be some big long encoded string 
    use: 'sig' 
} 

这就是我与njwt节点模块尝试:

njwt.verify(IDToken, secretKey, 'RS256', function(err, verifiedJwt) 
{ 
    if (err) 
    { 
     console.log(err); 
    } 
    else 
    { 
     console.log('Verified'); 
    } 
}); 

当我尝试这种方式时,我得到:

TypeError: Not a buffer 
    at Verify.verify (crypto.js:426:24) 
    at .../node_modules/njwt/index.js:406:10 
    at Verifier.defaultKeyResolver (.../node_modules/njwt/index.js:72:10) 
    at Verifier.verify (.../node_modules/njwt/index.js:375:15) 
    at Object.jwtLib.verify (.../node_modules/njwt/index.js:457:21) 
    at repl:1:6 
    at REPLServer.self.eval (repl.js:110:21) 
    at repl.js:249:20 
    at REPLServer.self.eval (repl.js:122:7) 
    at Interface.<anonymous> (repl.js:239:12) 

所以我想也许我需要secretKey.n而不是secretKey通过像这样:

njwt.verify(IDToken, secretKey.n, 'RS256', function(err, verifiedJwt) 
{ 
    if (err) 
    { 
     console.log(err); 
    } 
    else 
    { 
     console.log('Verified'); 
    } 
}); 

当我尝试这样我得到:

139980866705216:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:696:Expecting: CERTIFICATE 

其次是我的console.log(err);

{ [JwtParseError: Signature verification failed] 
    name: 'JwtParseError', 
    userMessage: 'Signature verification failed', 
    message: 'Signature verification failed', 
    jwtString: 'abcdefg...', 
    parsedHeader: { 
    typ: 'JWT', 
    alg: 'RS256', 
    kid: '...' 
    }, 
    parsedBody: { 
    sub: 'abcd...', 
    aud: 'abcdefg...', 
    email_verified: true, 
    token_use: 'id', 
    auth_time: 1491491773, 
    iss: 'https://cognito-idp.us-east-1.amazonaws.com/us-east-...', 
    'cognito:username': 'username', 
    exp: 1491495373, 
    iat: 1491491773, 
    email: '[email protected]' 
    }, 
    innerError: undefined } 

我该如何通过secretKeysecretKey应该是什么,它应该是什么样子?说实话,我甚至不知道njwt.verify是什么意思。

回答

0

看起来问题是njwt.verify期待公共RSA密钥。我必须将我的JWK Set对象转换为公共RSA密钥。我通过使用jwk-to-pem节点模块来做到这一点。

鉴于同secretKey从这样一个问题:

var jwkToPem = require('jwk-to-pem'); 

var pem = jwkToPem(secretKey); 

njwt.verify(IDToken, pem, 'RS256', function(err, verifiedJwt) 
{ 
    if (err) 
    { 
     console.log(err); 
    } 
    else 
    { 
     console.log('Verified'); 
    } 
}); 

成功!