js-aws-sigv4

JavaScript implementation of AWS Signature Version 4

Usage no npm install needed!

<script type="module">
  import jsAwsSigv4 from 'https://cdn.skypack.dev/js-aws-sigv4';
</script>

README

js-aws-signature-v4

TypeScript implementation of AWS Signature Version 4

npm version CircleCI Maintainability codecov License: MIT

WARNING: At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

Introduction and Overview

This tiny library is being developed to generate AWS signature version 4 and pre-signed url. This library is designed to be 'universal', i.e., it works both on most browsers and on Node.js just by importing from npm/source code.

Installation

At your project directory, do either one of the following.

  • From npm/yarn:

    $ npm install --save js-aws-sigv4 // npm
    $ yarn add js-aws-sigv4 // yarn
    
  • From GitHub:

    $ git clone https://github.com/junkurihara/js-aws-signature-v4.git
    

Then you should import the package as follows.

import jsAwsSigV4 from 'js-aws-sigv4'; // for npm

Usage

Get signature itself

const signingKey = await jsAwsSigv4.getSigningKey(
  credential.secretAccessKey,
  dateIsoString(new Date()),
  region_name,
  's3'
);

const signature = await jsAwsSigv4.getSignature(
  signingKey,
  'stringToSign'
);

Get pre-signed URL

const uriPath = '/public/test.txt';
const headers = {'Content-Type' : 'application/json', 'X-Amz-Meta-Foo': 'barbaz', 'X-Amz-Meta-Foobar': 'bazbaz'};

const signedUrlGet = await jsAwsSigv4.getSignedUrl(
  {
    accessKeyId: credential.accessKeyId,
    secretAccessKey: credential.secretAccessKey,
    sessionToken: credential.sessionToken, // optional
    regionName: region_name
  },
  {
    method: 'GET',
    hostName: host_name,
    serviceName: 's3',
    uriPath,
    headers // optional
  },
);

License

Licensed under the MIT license, see LICENSE file.