2019-07-30 18:32:26 +02:00
|
|
|
const fetch = require('fetch')
|
|
|
|
const request = require('request')
|
|
|
|
const crypto = require('crypto')
|
2019-07-30 18:57:45 +02:00
|
|
|
const config = require('config')
|
2019-07-30 18:32:26 +02:00
|
|
|
|
|
|
|
const Helpers = {
|
2019-07-31 01:43:08 +02:00
|
|
|
async signAndSend(message, user, to) {//, domain, req, res, targetOrigin) {
|
|
|
|
|
2019-07-30 18:32:26 +02:00
|
|
|
// get the URI of the actor object and append 'inbox' to it
|
2019-07-31 01:43:08 +02:00
|
|
|
const toInbox = to + '/inbox'
|
2019-07-31 01:55:52 +02:00
|
|
|
const toOrigin = new URL(to)
|
|
|
|
const toPath = toInbox.replace(toOrigin.origin, '')
|
2019-07-30 18:32:26 +02:00
|
|
|
// get the private key
|
|
|
|
const privkey = user.rsa.privateKey
|
|
|
|
const signer = crypto.createSign('sha256')
|
2019-07-31 01:43:08 +02:00
|
|
|
const d = new Date()
|
2019-07-31 01:55:52 +02:00
|
|
|
const stringToSign = `(request-target): post ${toPath}\nhost: ${toOrigin.hostname}\ndate: ${d.toUTCString()}`
|
2019-07-31 01:43:08 +02:00
|
|
|
console.error('stringToSign ', stringToSign)
|
|
|
|
|
2019-07-30 18:32:26 +02:00
|
|
|
signer.update(stringToSign)
|
|
|
|
signer.end()
|
|
|
|
const signature = signer.sign(privkey)
|
|
|
|
const signature_b64 = signature.toString('base64')
|
2019-07-31 01:43:08 +02:00
|
|
|
const header = `keyId="${config.baseurl}/federation/u/${user.username}",headers="(request-target) host date",signature="${signature_b64}"`
|
|
|
|
console.error('header ', header)
|
2019-07-30 18:32:26 +02:00
|
|
|
request({
|
2019-07-31 01:55:52 +02:00
|
|
|
url: toInbox,
|
2019-07-30 18:32:26 +02:00
|
|
|
headers: {
|
2019-07-31 01:55:52 +02:00
|
|
|
'Host': toOrigin.hostname,
|
2019-07-30 18:32:26 +02:00
|
|
|
'Date': d.toUTCString(),
|
|
|
|
'Signature': header
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
json: true,
|
|
|
|
body: message
|
|
|
|
}, function (error, response){
|
|
|
|
if (error) {
|
|
|
|
console.log('Error:', error, response.body)
|
|
|
|
}
|
|
|
|
else {
|
2019-07-31 01:03:21 +02:00
|
|
|
console.log('Response:', response.body, response.statusCode, response.status, response.statusMessage)
|
2019-07-30 18:32:26 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return res.status(200)
|
|
|
|
},
|
|
|
|
async sendAcceptMessage (body, user, domain, req, res, targetOrigin) {
|
|
|
|
const guid = crypto.randomBytes(16).toString('hex')
|
|
|
|
let message = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': `${config.baseurl}/federation/${guid}`,
|
|
|
|
'type': 'Accept',
|
|
|
|
'actor': `${config.baseurl}/federation/u/${user.username}`,
|
|
|
|
'object': body,
|
|
|
|
}
|
2019-07-31 01:43:08 +02:00
|
|
|
// Helpers.signAndSend(message, user, domain, req, res, targetOrigin)
|
|
|
|
},
|
|
|
|
async sendEvent(event, user) {
|
|
|
|
console.error('devo inviare un evento ai followers')
|
|
|
|
const followers = user.followers
|
|
|
|
console.error('send to ', followers)
|
|
|
|
for(let follower of followers) {
|
|
|
|
console.error('send message to ', follower)
|
|
|
|
const body = event.toAP(user.username, follower)
|
|
|
|
Helpers.signAndSend(body, user, follower)
|
|
|
|
}
|
2019-07-30 18:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:57:45 +02:00
|
|
|
module.exports = Helpers
|