Library used to sign http request as defined in https://tools.ietf.org/html/draft-cavage-http-signatures-12
npm install @knotcity/http-request-signer
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 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
}
``