1.0.9 • Published 3 years ago

hmac-sign-request v1.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

HMAC Request Sign

This is used to sign request with hmac256 using clientId and secret. This package also contains express middleware to validate such signed requests.

Usage

Add request headers

import {setAuth} from 'hmac-sign-request';
import axios from "axios";
(async () => {
  const res = await axios.get('https://somewhere.com',  {
    headers: setAuth({
      client: 'client',
      secret: 'secret',
    })
  });
  console.log(res);
})();

Sign request

import {signRequest} from 'hmac-sign-request';

let req: Request; // initialize it

signRequest(req, 'client', 'secret');
console.log(req.header('authorization'));
//HMAC-SHA256 credential=client t=1608218828237,v1=644a323b5586d369220fc5efbcaf8c4bae7d74782c44b7ff49945231f8cc9e84

Create signature from object

import {createSignature} from 'hmac-sign-request';

console.log(createSignature('secret', {some: 'object'}));
// t=1608218828237,v1=644a323b5586d369220fc5efbcaf8c4bae7d74782c44b7ff49945231f8cc9e84

Create signature from Request

import {createSignatureFromRequest} from 'hmac-sign-request';

console.log(createSignatureFromRequest(req, 'client', 'secret'));
// t=1608218828237,v1=644a323b5586d369220fc5efbcaf8c4bae7d74782c44b7ff49945231f8cc9e84

Using express middleware

To configure middleware behaviour use environment variables:

# [Optional] 
# Period in minutes when reloading of hmacAuthMiddleware known clients is called.
# Default is 10 minutes
AUTH_CLIENT_REFRESH=10
# [Optional]
# The period of time in minutes while request is considered valid. Based on t= part of the authorization header.
# Default is 5 minutes
AUTH_REQUEST_TTL=5

For hmacAuthMiddleware provide a function that will update known clients Map.

import express from 'express';
import {hmacAuthMiddleware} from 'hmac-sign-request';

const app = express();

app.use(hmacAuthMiddleware((clients) => {
  clients.set('client', 'secret');
}));
1.0.9

3 years ago

1.0.8

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago