Giter Club home page Giter Club logo

aws4-axios's Introduction

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

yarn npm
yarn add aws4-axios npm 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: "",
  }
);

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

โš ๏ธ ๐Ÿ’ป

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

aws4-axios's People

Contributors

allcontributors[bot] avatar dependabot-preview[bot] avatar dependabot[bot] avatar florianbepunkt avatar jamesmbourne avatar m-radzikowski avatar rubenvanrooij avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.