2019-08-02 17:29:55 +02:00
|
|
|
const fetch = require('node-fetch')
|
2019-08-10 15:00:08 +02:00
|
|
|
// const request = require('request')
|
2019-07-31 02:01:21 +02:00
|
|
|
const crypto = require('crypto')
|
2019-07-30 18:57:45 +02:00
|
|
|
const config = require('config')
|
2019-08-02 17:29:55 +02:00
|
|
|
const httpSignature = require('http-signature')
|
2019-08-08 16:52:13 +02:00
|
|
|
const debug = require('debug')('fediverse:helpers')
|
2019-09-11 12:00:13 +02:00
|
|
|
const { user: User } = require('../api/models')
|
|
|
|
const url = require('url')
|
2019-08-02 17:29:55 +02:00
|
|
|
|
|
|
|
const actorCache = []
|
2019-07-30 18:32:26 +02:00
|
|
|
|
|
|
|
const Helpers = {
|
2019-09-11 19:12:24 +02:00
|
|
|
async signAndSend (message, user, to) {
|
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-09-11 12:00:13 +02:00
|
|
|
const toOrigin = url.parse(to)
|
|
|
|
const toPath = toOrigin.path + '/inbox'
|
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
|
|
|
|
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}"`
|
2019-09-11 12:00:13 +02:00
|
|
|
return await fetch(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(),
|
2019-08-01 15:18:45 +02:00
|
|
|
'Signature': header,
|
2019-08-10 15:00:08 +02:00
|
|
|
'Content-Type': 'application/activity+json; charset=utf-8',
|
|
|
|
'Accept': 'application/activity+json, application/json; chartset=utf-8'
|
2019-07-30 18:32:26 +02:00
|
|
|
},
|
|
|
|
method: 'POST',
|
2019-08-10 15:00:08 +02:00
|
|
|
body: JSON.stringify(message) })
|
2019-07-31 01:43:08 +02:00
|
|
|
},
|
2019-09-11 12:00:13 +02:00
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async sendEvent (event, user) {
|
2019-09-11 12:00:13 +02:00
|
|
|
// TODO: has to use sharedInbox!
|
|
|
|
// event is sent by user that published it and by the admin instance
|
2019-09-11 19:12:24 +02:00
|
|
|
const instanceAdmin = await User.findOne({ where: { email: config.admin } })
|
|
|
|
if (!instanceAdmin || !instanceAdmin.username) {
|
2019-09-11 13:12:05 +02:00
|
|
|
debug('Instance admin not found (there is no user with email => %s)', config.admin)
|
|
|
|
return
|
|
|
|
}
|
2019-09-11 12:00:13 +02:00
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
for (const follower of instanceAdmin.followers) {
|
2019-09-11 12:00:13 +02:00
|
|
|
debug('Notify %s with event %s', follower, event.title)
|
2019-09-11 13:12:05 +02:00
|
|
|
const body = {
|
|
|
|
id: `${config.baseurl}/federation/m/c_${event.id}`,
|
|
|
|
type: 'Create',
|
|
|
|
actor: `${config.baseurl}/federation/u/${instanceAdmin.username}`,
|
|
|
|
url: `${config.baseurl}/federation/m/${event.id}`,
|
|
|
|
object: event.toAP(instanceAdmin.username, follower)
|
|
|
|
}
|
2019-09-11 12:00:13 +02:00
|
|
|
body['@context'] = 'https://www.w3.org/ns/activitystreams'
|
|
|
|
Helpers.signAndSend(body, user, follower)
|
|
|
|
}
|
2019-09-11 19:12:24 +02:00
|
|
|
|
2019-09-11 12:00:13 +02:00
|
|
|
// in case the event is published by the Admin itself do not republish
|
2019-09-11 13:12:05 +02:00
|
|
|
if (instanceAdmin.id === user.id) {
|
|
|
|
debug('')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user.settings.enable_federation || !user.username) { return }
|
|
|
|
for (const follower of user.followers) {
|
2019-08-10 15:00:08 +02:00
|
|
|
debug('Notify %s with event %s', follower, event.title)
|
2019-09-11 13:12:05 +02:00
|
|
|
const body = {
|
2019-09-11 19:12:24 +02:00
|
|
|
id: `${config.baseurl}/federation/m/${event.id}#create`,
|
2019-09-11 13:12:05 +02:00
|
|
|
type: 'Create',
|
2019-09-11 19:12:24 +02:00
|
|
|
to: ['https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
cc: [`${config.baseurl}/federation/u/${user.username}/followers`],
|
|
|
|
published: event.createdAt,
|
2019-09-11 13:12:05 +02:00
|
|
|
actor: `${config.baseurl}/federation/u/${user.username}`,
|
|
|
|
object: event.toAP(user.username, follower)
|
|
|
|
}
|
2019-07-31 02:08:46 +02:00
|
|
|
body['@context'] = 'https://www.w3.org/ns/activitystreams'
|
2019-07-31 01:43:08 +02:00
|
|
|
Helpers.signAndSend(body, user, follower)
|
2019-09-11 12:00:13 +02:00
|
|
|
}
|
2019-08-02 13:43:28 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async getFederatedUser (address) {
|
2019-08-02 13:43:28 +02:00
|
|
|
address = address.trim()
|
2019-08-02 17:29:55 +02:00
|
|
|
const [ username, host ] = address.split('@')
|
|
|
|
const url = `https://${host}/.well-known/webfinger?resource=acct:${username}@${host}`
|
|
|
|
return Helpers.getActor(url)
|
|
|
|
},
|
2019-09-11 19:12:24 +02:00
|
|
|
|
2019-08-02 17:29:55 +02:00
|
|
|
// TODO: cache
|
2019-09-11 19:12:24 +02:00
|
|
|
async getActor (url, force = false) {
|
2019-08-09 01:58:11 +02:00
|
|
|
// try with cache first
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!force && actorCache[url]) { return actorCache[url] }
|
|
|
|
const user = await fetch(url, { headers: { 'Accept': 'application/jrd+json, application/json' } })
|
2019-08-09 00:19:57 +02:00
|
|
|
.then(res => {
|
|
|
|
if (!res.ok) {
|
|
|
|
debug('[ERR] Actor %s => %s', url, res.statusText)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return res.json()
|
2019-08-08 17:48:12 +02:00
|
|
|
})
|
2019-08-02 17:29:55 +02:00
|
|
|
actorCache[url] = user
|
2019-08-02 13:43:28 +02:00
|
|
|
return user
|
|
|
|
},
|
|
|
|
|
2019-08-02 17:29:55 +02:00
|
|
|
// ref: https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
|
2019-09-11 19:12:24 +02:00
|
|
|
async verifySignature (req, res, next) {
|
2019-08-03 00:48:48 +02:00
|
|
|
let user = await Helpers.getActor(req.body.actor)
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.status(401).send('Actor not found') }
|
2019-08-02 17:29:55 +02:00
|
|
|
|
|
|
|
// little hack -> https://github.com/joyent/node-http-signature/pull/83
|
|
|
|
req.headers.authorization = 'Signature ' + req.headers.signature
|
2019-08-08 17:48:12 +02:00
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
// another little hack :/
|
2019-08-08 17:48:12 +02:00
|
|
|
// https://github.com/joyent/node-http-signature/issues/87
|
|
|
|
req.url = '/federation' + req.url
|
2019-08-02 17:29:55 +02:00
|
|
|
const parsed = httpSignature.parseRequest(req)
|
2019-09-11 19:12:24 +02:00
|
|
|
if (httpSignature.verifySignature(parsed, user.publicKey.publicKeyPem)) { return next() }
|
|
|
|
|
2019-08-02 17:29:55 +02:00
|
|
|
// signature not valid, try without cache
|
2019-08-03 00:48:48 +02:00
|
|
|
user = await Helpers.getActor(req.body.actor, true)
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.status(401).send('Actor not found') }
|
|
|
|
if (httpSignature.verifySignature(parsed, user.publicKey.publicKeyPem)) { return next() }
|
2019-08-02 17:29:55 +02:00
|
|
|
|
|
|
|
// still not valid
|
2019-09-11 12:00:13 +02:00
|
|
|
debug('Invalid signature from user %s', req.body.actor)
|
2019-08-08 17:48:12 +02:00
|
|
|
res.send('Request signature could not be verified', 401)
|
2019-07-30 18:32:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:57:45 +02:00
|
|
|
module.exports = Helpers
|