2019-09-22 05:05:30 +00:00
|
|
|
'use strict'
|
|
|
|
const httpSignature = require('http-signature')
|
|
|
|
const pub = require('../pub')
|
|
|
|
// http communication middleware
|
|
|
|
module.exports = {
|
2019-09-24 03:18:35 +00:00
|
|
|
auth,
|
2019-09-22 05:20:37 +00:00
|
|
|
verifySignature
|
2019-09-22 05:05:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 03:18:35 +00:00
|
|
|
function auth (req, res, next) {
|
|
|
|
// no client-to-server support at this time
|
|
|
|
if (req.app.get('env') !== 'development') {
|
|
|
|
return res.status(405).send()
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
2019-09-22 05:05:30 +00:00
|
|
|
async function verifySignature (req, res, next) {
|
2019-09-26 02:45:52 +00:00
|
|
|
try {
|
|
|
|
if (!req.get('authorization') && !req.get('signature')) {
|
|
|
|
// support for apps not using signature extension to ActivityPub
|
|
|
|
const actor = await pub.object.resolveObject(pub.utils.actorFromActivity(req.body))
|
|
|
|
if (actor.publicKey && req.app.get('env') !== 'development') {
|
2019-09-28 15:23:24 +00:00
|
|
|
console.log('Missing http signature')
|
2019-09-26 02:45:52 +00:00
|
|
|
return res.status(400).send('Missing http signature')
|
|
|
|
}
|
|
|
|
return next()
|
2019-09-24 03:18:35 +00:00
|
|
|
}
|
2019-09-26 02:45:52 +00:00
|
|
|
const sigHead = httpSignature.parse(req)
|
|
|
|
const signer = await pub.object.resolveObject(sigHead.keyId, req.app.get('db'))
|
|
|
|
const valid = httpSignature.verifySignature(sigHead, signer.publicKey.publicKeyPem)
|
|
|
|
if (!valid) {
|
2019-09-28 15:23:24 +00:00
|
|
|
console.log('signature validation failure', sigHead.keyId)
|
2019-09-26 02:45:52 +00:00
|
|
|
return res.status(400).send('Invalid http signature')
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
} catch (err) {
|
2019-12-25 22:22:31 +00:00
|
|
|
if (req.body.type === 'Delete' && err.message.startsWith('410')) {
|
2019-12-25 21:52:15 +00:00
|
|
|
// user delete message that can't be verified because we don't have the user cached
|
2019-12-25 22:22:31 +00:00
|
|
|
console.log('Unverifiable delete')
|
|
|
|
return res.status(200).send()
|
2019-12-25 21:52:15 +00:00
|
|
|
}
|
|
|
|
console.log('error during signature verification', err.message, req.body)
|
2019-09-26 02:45:52 +00:00
|
|
|
return res.status(500).send()
|
2019-09-22 05:20:37 +00:00
|
|
|
}
|
2019-09-22 05:05:30 +00:00
|
|
|
}
|