feat: http signatures support, .sign() and .verify() AP helper methods

This commit is contained in:
Julian Lam
2023-06-19 17:29:22 -04:00
parent 4f5f025d57
commit e7184eb8cc
6 changed files with 174 additions and 30 deletions

View File

@@ -1,7 +1,10 @@
'use strict';
const request = require('request-promise-native');
const { generateKeyPairSync, sign } = require('crypto');
const winston = require('winston');
const db = require('../database');
const ttl = require('../cache/ttl');
const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours
@@ -36,6 +39,29 @@ Helpers.query = async (id) => {
({ href: actorUri } = actorUri);
}
webfingerCache.set(id, { username, hostname, actorUri });
return { username, hostname, actorUri };
const { publicKey } = response.body;
webfingerCache.set(id, { username, hostname, actorUri, publicKey });
return { username, hostname, actorUri, publicKey };
};
Helpers.generateKeys = async (uid) => {
winston.verbose(`[activitypub] Generating RSA key-pair for uid ${uid}`);
const {
publicKey,
privateKey,
} = generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
});
await db.setObject(`uid:${uid}:keys`, { publicKey, privateKey });
return { publicKey, privateKey };
};