Extract HTTP Signature verify middleware

This commit is contained in:
Armin 2019-02-28 03:44:57 +01:00
parent bb04ef0664
commit ea2f6b7811
2 changed files with 17 additions and 4 deletions

View File

@ -1,8 +1,8 @@
import { sendCollection } from '../utils/collection' import { sendCollection } from '../utils/collection'
import express from 'express' import express from 'express'
import { serveUser } from './serveUser' import { serveUser } from './serveUser'
import { verifySignature } from '../security'
import { activityPub } from '../ActivityPub' import { activityPub } from '../ActivityPub'
import verify from './verify'
const router = express.Router() const router = express.Router()
const debug = require('debug')('ea:user') const debug = require('debug')('ea:user')
@ -45,11 +45,9 @@ router.get('/:name/outbox', (req, res) => {
} }
}) })
router.post('/:name/inbox', async function (req, res, next) { router.post('/:name/inbox', verify, async function (req, res, next) {
debug(`body = ${JSON.stringify(req.body, null, 2)}`) debug(`body = ${JSON.stringify(req.body, null, 2)}`)
debug(`actorId = ${req.body.actor}`) debug(`actorId = ${req.body.actor}`)
// TODO stop if signature validation fails
debug(`verify = ${await verifySignature(`${req.protocol}://${req.hostname}:${req.port}${req.originalUrl}`, req.headers)}`)
// const result = await saveActorId(req.body.actor) // const result = await saveActorId(req.body.actor)
switch (req.body.type) { switch (req.body.type) {
case 'Create': case 'Create':

View File

@ -0,0 +1,15 @@
import { verifySignature } from '../security'
const debug = require('debug')('ea:verify')
export default async (req, res, next) => {
debug(`actorId = ${req.body.actor}`)
// TODO stop if signature validation fails
if (await verifySignature(`${req.protocol}://${req.hostname}:${req.port}${req.originalUrl}`, req.headers)) {
debug('verify = true')
next()
} else {
// throw Error('Signature validation failed!')
debug('verify = false')
next()
}
}