AWS Cloufront:使用签名的Cookie返回拒绝访问

时间:2018-08-17 22:52:01

标签: node.js amazon-cloudfront

我可以使用Cloudfront签名的URL和自定义域来访问我的原始对象。 问题出在我尝试使用Cloudfront签名的Cookie时,然后从Cloudfront收到403 Access Denied响应。

<Error>
   <Code>AccessDenied</Code>
   <Message>Access denied</Message>
</Error>

我使用的完全相同:

  • OAI凭证
  • 密钥对ID
  • 私钥
  • 政策

以及我用于签名网址的cloudfront和origin配置。

下面是我使用的nodejs代码。我真的不知道怎么了。

任何帮助将不胜感激!

谢谢!

var moment = require('moment');

var AWS = require('aws-sdk');
var keyPairId = 'APKAIPZ5BRDMEXAMPLE';

var privateKey = '-----BEGIN RSA PRIVATE KEY-----\n' +
   'MIIEowIBAAKCAQEAsL+Tz/soZcDQLXcgJE89h5RHfZY+ddNIVI/3T0OvjOOFVRLp\n' +
   '0gN+06cwXWsvlmeEbnh5XdklA38H4p3wt5mxqip4YmuvbW6i+8UALHxPW0LTmiz0\n' +
   's4J6HB0eXhm3cbFV60i1DNat+W2q5miqlXqxKSdg+UbUGlFA5CbR\n' +
   '-----END RSA PRIVATE KEY-----';

var signer = new AWS.CloudFront.Signer(keyPairId, privateKey);
var expireTime = moment().add(3600, 'sec').unix();

exports.handler = (event, context, callback) => {

   var domain = 'mydomain.com';
   var resource = 'https://cdn.mydomain.com/*';
   var attributes = '; domain=' + domain + '; path=/; secure; httpOnly'

   var options = {policy : '{"Statement":[{"Resource":"' + resource + 
   '","Condition":{"DateLessThan":{"AWS:EpochTime":' + expireTime + 
    '}}}]}'};

    signer.getSignedCookie(options, function(err, data) {

        if (err) {
        //do somehing

        } else {

           context.done(null, {

            'Set-Cookie': 'CloudFront-Policy=' + data["CloudFront-Policy"] + attributes,
            'SEt-Cookie': 'CloudFront-Signature=' + data["CloudFront-Signature"] + attributes,
            'SeT-Cookie': 'CloudFront-Key-Pair-Id=' + data["CloudFront-Key-Pair-Id"] + attributes


       });

    }
});
};

1 个答案:

答案 0 :(得分:0)

而不是使用moment(),而是使用Math()函数来计算您的到期时间,这将达到目的!

var expireTime = Math.round((new Date()).getTime() / 1000) + 3600;