Socket
Socket
Sign inDemoInstall

aws4-axios

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws4-axios

Axios request interceptor for signing requests with AWSv4


Version published
Weekly downloads
82K
decreased by-15.24%
Maintainers
1
Weekly downloads
 
Created
Source

aws4-axios

All Contributors

npm version npm downloads

This is a request interceptor for the Axios HTTP request library to allow requests to be signed with an AWSv4 signature.

This may be useful for accessing AWS services protected with IAM auth such as an API Gateway.

Installation

yarnnpm
yarn add aws4-axiosnpm install --save aws4-axios

Usage

To add an interceptor to the default Axios client:

import axios from "axios";
import { aws4Interceptor } from "aws4-axios";

const interceptor = aws4Interceptor({
  region: "eu-west-2",
  service: "execute-api",
});

axios.interceptors.request.use(interceptor);

// Requests made using Axios will now be signed
axios.get("https://example.com/foo").then((res) => {
  // ...
});

Or you can add the interceptor to a specific instance of an Axios client:

import axios from "axios";
import { aws4Interceptor } from "aws4-axios";

const client = axios.create();

const interceptor = aws4Interceptor({
  region: "eu-west-2",
  service: "execute-api",
});

client.interceptors.request.use(interceptor);

// Requests made using Axios will now be signed
client.get("https://example.com/foo").then((res) => {
  // ...
});

You can also pass AWS credentials in explicitly (otherwise taken from process.env)

const interceptor = aws4Interceptor(
  {
    region: "eu-west-2",
    service: "execute-api",
  },
  {
    accessKeyId: "",
    secretAccessKey: "",
  }
);

You can also pass a custom CredentialsProvider factory instead

const customCredentialsProvider = {
  getCredentials: async () => {
    return Promise.resolve({
      accessKeyId: "custom-provider-access-key-id",
      secretAccessKey: "custom-provider-secret-access-key"
    })
  }
}

const interceptor = aws4Interceptor(
  {
    region: "eu-west-2",
    service: "execute-api",
  },
  customCredentialsProvider
);

Assuming the IAM Role

You can pass a parameter to assume the IAM Role with AWS STS and use the assumed role credentials to sign the request. This is useful when doing cross-account requests.

const interceptor = aws4Interceptor(
  {
    region: "eu-west-2",
    service: "execute-api",
    assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
  }
);

Obtained credentials are cached and refreshed as needed after they expire.

You can use expirationMarginSec parameter to set the number of seconds before the received credentials expiration time to invalidate the cache. This allows setting a safety margin. Default to 5 seconds.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Florian Bischoff

💻

Ruben van Rooij

💻

Roman

👀

Maciej Radzikowski

⚠️ 💻

Christopher Kruse

⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Keywords

FAQs

Package last updated on 04 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc