rubberguppe/pub/federation.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict'
const crypto = require('crypto')
const request = require('request-promise-native')
const pubUtils = require('./utils')
// federation communication utilities
module.exports = {
requestObject,
deliver
}
function requestObject (id) {
2019-09-22 05:20:37 +00:00
return request({
url: id,
headers: { Accept: 'application/activity+json' },
2020-10-25 01:33:45 +00:00
httpSignature: {
key: global.guppeSystemUser._meta.privateKey,
keyId: global.guppeSystemUser.id,
headers: ['(request-target)', 'host', 'date'],
authorizationHeaderName: 'Signature'
}
2019-09-22 05:20:37 +00:00
})
}
function deliver (actor, activity, addresses) {
if (activity.bto) {
delete activity.bto
}
if (activity.bcc) {
delete activity.bcc
}
const requests = addresses.map(addr => {
const body = pubUtils.toJSONLD(activity)
const digest = crypto.createHash('sha256')
.update(JSON.stringify(body))
.digest('base64')
return request({
method: 'POST',
url: addr,
headers: {
'Content-Type': 'application/activity+json',
Digest: `SHA-256=${digest}`
},
httpSignature: {
key: actor._meta.privateKey,
keyId: actor.id,
headers: ['(request-target)', 'host', 'date', 'digest'],
authorizationHeaderName: 'Signature'
},
json: true,
resolveWithFullResponse: true,
simple: false,
body
})
2019-09-28 15:23:24 +00:00
.then(result => console.log('delivery:', addr, result.statusCode))
2019-10-01 01:55:20 +00:00
.catch(err => console.log(err.message))
})
return Promise.all(requests)
}