# signature-module

> Signature Module

Latest version **0.5.9** (published 2021-04-14) · 0 weekly downloads

## Install

```sh
npm install signature-module
pnpm add signature-module
yarn add signature-module
bun add signature-module
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.9 |
| Published | 2021-04-14 |
| First published | 2017-02-07 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 28.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | pantaflix |

## Links

- npm: https://www.npmjs.com/package/signature-module
- npm.io page: https://npm.io/package/signature-module

## Dependencies (6)

- [moment](https://npm.io/package/moment.md) ^2.22.1
- [request](https://npm.io/package/request.md) ^2.74.0
- [json-stable-stringify](https://npm.io/package/json-stable-stringify.md) ^1.0.1
- [@types/request-promise](https://npm.io/package/@types/request-promise.md) ^4.1.47
- [request-promise-native](https://npm.io/package/request-promise-native.md) ^1.0.5
- [@types/request-promise-native](https://npm.io/package/@types/request-promise-native.md) ^1.0.17

## Recent versions

- 0.5.9 (latest) — 2021-04-14
- 0.5.8 — 2021-03-15
- 0.5.7 — 2021-03-15
- 0.5.6 — 2021-02-05
- 0.5.5 — 2019-06-07
- 0.5.4 — 2018-05-22
- 0.5.3 — 2018-05-17
- 0.5.2 — 2018-05-17
- 0.5.1 — 2018-05-17
- 0.5.0 — 2018-05-17
- 0.4.4 — 2017-10-30
- 0.4.3 — 2017-08-15
- 0.4.2 — 2017-08-15
- 0.4.1 — 2017-08-15
- 0.4.0 — 2017-08-11
- … 7 more at https://npm.io/package/signature-module/versions

## README

### What is this repository for? ###

This JavaScript module provides tools to make the process of signing a request between servers easier.
It consist on two services:

SignatureMiddleware: Node.js Express middleware in charge of checking whether a signed request is valid.

SignedRequest: Service to send signed http requests. It appends the necessary parameters to it.

### How do I get set up? ###

1-
```
npm install signature-module --save
```


2- Include it once in your project with the sequelize instance (It is used for creating the necessary migrations)

```javascript
const signature = require('signature-module')({
    appId: 'appId',
    signatureKey: 'signatureKey'
});
```


3- Initialize the service desired

```javascript
const signatureMiddleware = signature.signatureMiddleware();

const signedRequest = signature.signedRequest('targetSecretKey');
```


### How do I use the signatureMiddleware ###

Use the signatureMiddleware with your Express server to validate any incomming request.

```javascript
server.use(signatureMiddleware());
```


### How do I use the signedRequest module ###

Use the signed request module like you would do with the request-promise module: https://github.com/request/request-promise. The module will append the necessary values to the request (signature, appId, timestamp)

GET:
```javascript
signedRequest.get({
    baseUrl: 'https://some.url.com',
    uri: `/some/uri`,
    json: true,
    qs: {
        param: 'value'
    }
})
.then(data => {})
.catch(error => {})
```

POST:
```javascript
signedRequest.post({
    baseUrl: 'https://some.url.com',
    uri: `/some/uri`,
    json: true,
    qs: {
        param: 'value'
    },
    body: {
        param1: 'value',
        param2: 'value'
    }
})
.then(data => {})
.catch(error => {})
```

### How does the communication between services work ###

Service A
AppId: 'ServiceA';
IncomingSignatureKey: 'IncomingSignatureA';

Service B
AppId: 'ServiceB';
IncomingSignatureKey: 'IncomingSignatureB';

To make a request from A to B Service A needs to add three extra query parameters to the request:

- appId: App Id of the sender service, in this case service A: 'ServiceA'.
- timestamp: Unix timestamp of the moment of the request
- signature: Hash containing all the request parameters signed with the Service B incomming signature key. Structure: 'stringifiedQueryParams;stringifiedBody'; Query params and body should be ordered alphabetically.

```javascript
signedRequest.post('ServiceB', {
    appId: 'ServiceA',
    signature: 'SignatureHashedWithServiceBSignatureKey'
});
```

For Service B to validate a request coming from service A:

- Signature must have been hashed with service B incomming signature key.
- In order to validate the signature, this one (the signature) should be removed from the query string.
- The content of the request (body and query parameters) should be sorted alphabetically before stringifying it.
- AppId should be the right appId from service A.
- Timestamp should be valid (in a range of time). You can configure it initializin the signature service with the 'validOffset' option:
```javascript
const signature = require('signature-module')({
    validOffset: {
        amount: 20,
        time: 'minutes'
    }
});
```

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