gancio-upstream/server/federation/helpers.js

103 lines
3.7 KiB
JavaScript
Raw Normal View History

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-08-02 17:29:55 +02:00
const actorCache = []
2019-07-30 18:32:26 +02:00
const Helpers = {
2019-08-07 01:26:14 +02:00
async signAndSend(message, user, to) {
2019-07-31 01:43:08 +02:00
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-08-01 15:18:45 +02:00
console.error('requestTo ', toInbox)
console.error('host ', toOrigin.hostname)
2019-08-10 15:00:08 +02:00
const response = 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) })
console.log('Response:', response.body, response.statusCode, response.status, response.statusMessage)
2019-07-31 01:43:08 +02:00
},
async sendEvent(event, user) {
const followers = user.followers
for(let follower of followers) {
2019-08-10 15:00:08 +02:00
debug('Notify %s with event %s', follower, event.title)
2019-07-31 01:43:08 +02:00
const body = 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-08-02 13:43:28 +02:00
},
async getFederatedUser(address) {
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)
},
// TODO: cache
async getActor(url, force=false) {
2019-08-09 01:58:11 +02:00
// try with cache first
2019-08-02 17:29:55 +02:00
if (!force && actorCache[url]) return actorCache[url]
2019-08-02 13:43:28 +02:00
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-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/
async verifySignature(req, res, next) {
2019-08-03 00:48:48 +02:00
let user = await Helpers.getActor(req.body.actor)
2019-08-09 01:58:11 +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
// another little hack :/
// 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)
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-08-09 01:58:11 +02:00
if (!user) return res.status(401).send('Actor not found')
2019-08-02 17:29:55 +02:00
if (httpSignature.verifySignature(parsed, user.publicKey.publicKeyPem)) return next()
// still not valid
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