mhart / aws4

Signs and prepares Node.js requests using AWS Signature Version 4
MIT License
703 stars 176 forks source link

using supertest to hit apigateway and authenticating with this library #77

Closed Galileo1 closed 6 years ago

Galileo1 commented 6 years ago

I am trying to authenticate apigateway which executes lambda functions and for some reasonI keep on getting The request signature we calculated does not match the signature you provided.. This library works fine when I use one of the examples that uses request library but not when I use supertest library. Any help?

Example Using request that works

var http  = require('follow-redirects').http,
    https = require('follow-redirects').https,
    aws4  = require('aws4');

var jsonbody={
    "PhoneNumbers": [{
      "PhoneNumber": "6315442287",
      "CountryCode": "1",
      "TypeId": 1
    }]
  };

  var opts = { host: 'somehost.com',
               method: 'PUT',
               path: '/api/member/123456', 
               service: 'execute-api', 
               url: 'https://somehost.com/api/member/123456',
               region: 'us-east-1',
               headers: { 'content-type':'application/json',  },
               body: JSON.stringify(jsonbody)
     };

aws4.sign(opts);
function request(o) { https.request(o, function(res) { res.pipe(process.stdout) }).end(o.body || '') };
request(aws4.sign(opts, { accessKeyId: 'xxx', secretAccessKey: 'xxx}));

Example Using supertest that doesn't work

var opts = { host: 'somehost.com',
        method: 'PUT',
        path: '/api/member/1066484691', 
        service: 'execute-api', 
        url: 'https://somehost.com/api/member/1066484691',
        region: 'us-east-1',
        headers: {  'content-type':'application/json' },
        body: JSON.stringify(updateMembersInfoBody)
     };
 let opts1 = await sign(opts, { accessKeyId: 'xxx', secretAccessKey: 'xxx'});

 return request(endpoint)
                            .post("/")
                           .send(jsonBody)
                            .set('Authorization',opts1.headers.Authorization)
                            .set('X-Amz-Date', opts1.headers['X-Amz-Date'])
                            .then(response => response);
mhart commented 6 years ago

Sorry, this isn't an issue with aws4 – I can't help you there