# @knotcity/http-request-signer

> Library used to sign http request as defined in https://tools.ietf.org/html/draft-cavage-http-signatures-12

Latest version **0.5.1** (published 2024-08-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install @knotcity/http-request-signer
pnpm add @knotcity/http-request-signer
yarn add @knotcity/http-request-signer
bun add @knotcity/http-request-signer
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2024-08-26 |
| First published | 2021-02-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 35.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Knot |
| Maintainers | knotcity |

## Links

- npm: https://www.npmjs.com/package/@knotcity/http-request-signer
- Repository: https://github.com/knotcity/http-requests-signer
- Homepage: https://github.com/knotcity/http-requests-signer#readme
- Issues: https://github.com/knotcity/http-requests-signer/issues
- npm.io page: https://npm.io/package/@knotcity/http-request-signer

## Recent versions

- 0.5.1 (latest) — 2024-08-26
- 0.4.0 — 2022-10-19
- 0.3.3 — 2022-01-21
- 0.2.0 — 2021-02-15

## README

# Knot HTTP Requests Signer

Library used to sign the HTTP request as defined in https://tools.ietf.org/html/draft-cavage-http-signatures-12.
More info on why we use it can be found [here](https://doc.knotcity.io/services/http-signature/).

# Installation

This project is written in TypeScript using Node.js 12 and targeting es2017.
It MAY work for older versions of Node.js.

Install using npm:
```
npm install @knotcity/http-request-signer
```

# Usage

You have a client and server example in the `example/` folder.
Import the module in the same way as you do normally.

```
// JavaScript
const hrs = require('@knotcity/http-request-signer');
// TypeScript
import hrs = require('@knotcity/http-request-signer');
```

## Generating a key

You can use the [crypto module](https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback) from Node.js to generate a key pair or the utility function in the package to generate an ec `secp521r1` curve.

```
const keyPair = hrs.generateECKeyPair();
// Public key
keyPair.publicKey;
// Private key
keyPair.privateKey;
```

## Signing a request

To sign a request you need to add the `Authorization` header with the value given by the `generateAuthorization` function.

```
import http = require('http');

// Make a request using node's HTTP module, but you can also use other modules like axios
const req = http.request(...);

// Fetch the private key from somewhere safe
const privKey = "...";

// Generate the signature
const auth = hrs.generateAuthorization({
    headers: req.getHeaders(),
    method: req.method,
    path: req.path
}, {
    headers: ['date', '(request-target)', 'content-length'], // List of header to use in the signature
    privateKey: privKey, // Private key
    hash: 'sha256', // Hash to use
    algorithm: 'ecdsa', // Algorithm used for the key
    keyId: 'keyn1' // The key identifier (this is defined by you, to allow to find the public key matching the private key)
});

// Add signature to request
req.setHeader('authorization', auth);
```

## Verifying a signature

The verification process is similar to the signature process as we collect the same info, but using the public key instead of the private key (as the server does not know the private key).

```
try
{
    // Extract all components of the Authorization header
    const components = hrs.parseAuthorizationHeader(req.headers.authorization);
    
    // Here you would get the keyId from the components and fetch the matching public key
    const pubKey = "...";

    const valid = verifyAuthorization(components, {
            headers: req.headers,
            method: req.method || '',
            path: req.url || ''
        }, pubKey);
}
catch(err)
{
    // Throws if the header cannot be parsed
}
```

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