npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

activitypub-http-signatures

v2.5.0

Published

A library for creating, parsing, and verifying HTTP signature headers, as per the [Signing HTTP Messages draft 80](https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-08) specification.

Downloads

152

Readme

ActivityPub HTTP Signatures

A library for creating, parsing, and verifying HTTP signature headers, as per the Signing HTTP Messages draft 08 specification.

Basic use

Signing

Signed Fetch

The simplest way to make a signed request is using SignedFetch. In this method you create a fetch-like function that automatically signs requests before sending them.

Note that this function only supports a subset of fetch's options - for more advanced usage or for use with other HTTP libraries, see the next section.

import { Sha256Signer } from 'activitypub-http-signatures';
import fetch from 'node-fetch';

// You should make the public key available at this URL
const publicKeyId = 'http://my-site.example/@paul#main-key';
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
${process.env.PRIVATE_KEY}
-----END RSA PRIVATE KEY-----`;

export async function postResource(url, body) {
	// You need to bring your own fetch function and public/private key pair;
	// see the next section for more info
	const signedFetch = SignedFetch.sha256(fetch, { publicKeyId, privateKey });
	const res = await signedFetch(url, {
		// This is a subset of `fetch`'s options - it only supports method and body,
		// and the object-form of headers (i.e. not the Headers class instance).
		method: 'POST',
		headers: {
			'content-type': 'application/ld+json'
		},
		body
	});

	// The response object is whatever your base fetch function returns
	return res.ok;
}

Generating the signed headers

The following example illustrates using the lower-level library functions to sign requests:

import { Sha256Signer } from 'activitypub-http-signatures';
import fetch from 'node-fetch';

// You should make the public key available at this URL
const publicKeyId = 'http://my-site.example/@paul#main-key';
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
${process.env.PRIVATE_KEY}
-----END RSA PRIVATE KEY-----`;

export function getResource(url) {
	// Create the instance of the signer class
	// You can pass an optional `headers` array to specify which headers should be
	// used in the signature.
	// The default is ['(request-target)', 'host', 'date']
	const signer = new Sha256Signer({ publicKeyId, privateKey });

	// A dict of the headers used in your request.
	// The host and date headers will be added automatically
	// if they're missing.
	const headers = {
		accept: 'application/ld+json'
	}
	const method = 'GET';

	// Generate the date, host and signature headers, and add to
	// the accept header dict.
	const signedHeaders = signer.generateHeaders({
		url,
		method,
		headers
	});

	// Create the HTTP request with the generated header
	// You could use fetch or any other HTTP library
	return fetch(
		url,
		{
			headers: signedHeaders,
			// (you can add any other options your fetch implementation supports)
		}
	)
}

Verifying

import parser from 'activitypub-http-signatures';
import fetch from 'node-fetch';

// Assuming we're dealing with a nodejs IncommingMessage
export async function verifyIncomingRequestSignature(req){
	// Parse the fields from the signature header
	// NB: If you're using express, you need to use originalUrl instead of url
	const { url, method, headers } = req;
	const signature = parser.parse({ url, method, headers });

	// If there's no signature header the parse function will return null
	if(signature === null) {
		throw new Error('The headers object has no `signature` key');
	}

	// Get the public key object using the provided key ID
	const keyRes = await fetch(
		signature.keyId,
		{
			headers: {
				accept: 'application/ld+json, application/json'
			}
		}
	);

	const { publicKey } = await keyRes.json();

	// Verify the signature
	const success = signature.verify(
		publicKey.publicKeyPem,	// The PEM string from the public key object
	);

	if(!success){
		throw new Error('http signature validation failed')
	} else {
		return publicKey.owner
	}
}

Changes in V2

Version 2 of this package has a completely new and hopefully simpler API. All previous exports have been removed, you now need to use Sha256Signer to sign outgoing requests and Parser to parse and verify incoming requests. See the examples above for details.

Typescript definitions are included since V2.1.0