- Notifications
You must be signed in to change notification settings - Fork1
AWS Signature Version 4 for node.js and browser.
License
yugasun/aws4-sign
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
AWS Signature Version 4 for node.js and browser.
Notice: this project is rewrote fromaws4 byES6 for using on demand.
npm install aws4-sign --save# oryarn add aws4-sign
<scriptsrc="https://unpkg.com/aws4-sign"></script><script>varsigs=Aws4Sign.sign({service:'s3',path:'/../../whatever?X-Amz-Expires=1234',signQuery:true,},{accessKeyId:'a',secretAccessKey:'b'},);</script>
consthttp=require('http');const{ sign, RequestSigner}=require('aws4-sign');constopts={host:'sqs.us-east-1.amazonaws.com',path:'/?Action=ListQueues',};// assumes AWS credentials are available in process.envsign(opts,{accessKeyId:'<your-access-key-id>',secretAccessKey:'<your-secret-access-key>',});console.log(opts);/*{ host: 'sqs.us-east-1.amazonaws.com', path: '/?Action=ListQueues', headers: { Host: 'sqs.us-east-1.amazonaws.com', 'X-Amz-Date': '20121226T061030Z', Authorization: 'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/sqs/aws4_request, ...' }}*/// we can now use this to query AWS using the standard node.js http APIhttp.request(opts,function(res){res.pipe(process.stdout);}).end();// Generate CodeCommit Git access passwordconstsigner=newRequestSigner({service:'codecommit',host:'git-codecommit.us-east-1.amazonaws.com',method:'GIT',path:'/v1/repos/MyAwesomeRepo',});constpassword=signer.getDateTime()+'Z'+signer.signature();
This calculates and populates theAuthorization
header ofrequestOptions
,and any other necessary AWS headers and/or request options. ReturnsrequestOptions
as a convenience for chaining.
requestOptions
is an object holding the same options that the node.jshttp.requestfunction takes.
The following properties ofrequestOptions
are used in the signing orpopulated if they don't already exist:
name | default |
---|---|
hostname/host | will be determined fromservice andregion |
method | GET |
path | / |
body | '' |
service | will be calculated fromhostname orhost |
region | 'us-east-1' |
domain | 'amazonaws.com' |
headers['Host'] | will usehostname orhost or be calculated if not given |
headers['Content-Type'] | 'application/x-www-form-urlencoded; charset=utf-8' |
headers['Date'] | new Date() |
Your AWS credentials (which can be found in yourAWS console) can bespecified in one of two ways:
You can configcredentials
, like below:
aws4.sign(requestOptions,{secretAccessKey:'<your-secret-access-key>',accessKeyId:'<your-access-key-id>',sessionToken:'<your-session-token>',});
Or you can attach them toprocess.env
usingdotenv, create.env
file in your projectroot, then put below code before you use aws4 sign
require('dotenv').config();
ThesessionToken
property andAWS_SESSION_TOKEN
environment constiable areoptional for signing withIAM STS temporary credentials.
About
AWS Signature Version 4 for node.js and browser.