2023-05-17 13:13:30 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const { generateKeyPairSync } = require('crypto');
|
|
|
|
|
|
|
|
|
|
const winston = require('winston');
|
2023-05-29 17:42:44 -04:00
|
|
|
const request = require('request-promise-native');
|
2023-05-17 13:13:30 -04:00
|
|
|
|
2023-05-29 17:42:44 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const helpers = require('./helpers');
|
2023-05-17 13:13:30 -04:00
|
|
|
|
|
|
|
|
const ActivityPub = module.exports;
|
|
|
|
|
|
2023-05-29 17:42:44 -04:00
|
|
|
ActivityPub.getActor = async (id) => {
|
|
|
|
|
const { hostname, actorUri: uri } = await helpers.query(id);
|
|
|
|
|
if (!uri) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const actor = await request({
|
|
|
|
|
uri,
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
|
|
|
|
|
},
|
|
|
|
|
json: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
actor.hostname = hostname;
|
|
|
|
|
|
|
|
|
|
return actor;
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-17 13:13:30 -04:00
|
|
|
ActivityPub.getPublicKey = async (uid) => {
|
|
|
|
|
let publicKey;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
({ publicKey } = await db.getObject(`uid:${uid}:keys`));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
({ publicKey } = await generateKeys(uid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return publicKey;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function generateKeys(uid) {
|
2023-05-23 16:13:16 -04:00
|
|
|
winston.verbose(`[activitypub] Generating RSA key-pair for uid ${uid}`);
|
2023-05-17 13:13:30 -04:00
|
|
|
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 };
|
|
|
|
|
}
|