# hmac-sign-request

> Sign requests with hmac

Latest version **1.0.9** (published 2021-07-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install hmac-sign-request
pnpm add hmac-sign-request
yarn add hmac-sign-request
bun add hmac-sign-request
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2021-07-20 |
| First published | 2021-07-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 27.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | anton.nagornyi |
| Keywords | sign, sign request, HMAC, HMAC256, express, middleware |

## Links

- npm: https://www.npmjs.com/package/hmac-sign-request
- Repository: https://github.com/anton-nagornyi/hmac-sign-request
- Homepage: https://github.com/anton-nagornyi/hmac-sign-request#readme
- Issues: https://github.com/anton-nagornyi/hmac-sign-request/issues
- npm.io page: https://npm.io/package/hmac-sign-request

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.9 (latest) — 2021-07-20
- 1.0.8 — 2021-07-13
- 1.0.6 — 2021-07-13
- 1.0.4 — 2021-07-13
- 1.0.3 — 2021-07-13
- 1.0.2 — 2021-07-07
- 1.0.1 — 2021-07-07
- 1.0.0 — 2021-07-07

## README

# 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
```typescript
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

```typescript
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

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

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

### Create signature from Request
```typescript
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:
```dotenv
# [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.
```typescript
import express from 'express';
import {hmacAuthMiddleware} from 'hmac-sign-request';

const app = express();

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

---
_Source: https://npm.io/package/hmac-sign-request · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
