From a43419f9ff6d9cdc1de0b553a763eeca81dd38a3 Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Sat, 21 Sep 2019 16:53:55 -0500 Subject: [PATCH] setup db utils in store directory, move getOrCreateActor and break out getActor for actions which require and existing actor (e.g. signature validation) --- db/actor.js | 5 ---- index.js | 2 +- routes/inbox.js | 5 ++-- routes/user.js | 9 ++++---- routes/webfinger.js | 11 +++++---- store/actor.js | 34 +++++++++++++++++++++++++++ {db => store}/index.js | 0 {db => store}/setup.js | 0 utils/index.js | 52 ++++-------------------------------------- 9 files changed, 53 insertions(+), 65 deletions(-) delete mode 100644 db/actor.js create mode 100644 store/actor.js rename {db => store}/index.js (100%) rename {db => store}/setup.js (100%) diff --git a/db/actor.js b/db/actor.js deleted file mode 100644 index 6f0c13d..0000000 --- a/db/actor.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict' - -module.exports = { - -} diff --git a/index.js b/index.js index 4b6b2a2..3044863 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ const app = express(); // Connection URL const url = 'mongodb://localhost:27017'; -const dbSetup = require('./db/setup'); +const dbSetup = require('./store/setup'); // Database Name const dbName = 'test'; diff --git a/routes/inbox.js b/routes/inbox.js index 5f0f2b4..a695c68 100644 --- a/routes/inbox.js +++ b/routes/inbox.js @@ -3,6 +3,7 @@ const router = express.Router() const utils = require('../utils') const pub = require('../pub') const net = require('../net') +const store = require('../store') const request = require('request-promise-native') const httpSignature = require('http-signature') const {ObjectId} = require('mongodb') @@ -20,7 +21,7 @@ router.post('/', net.validators.activity, function (req, res) { sigHead = httpSignature.parse(req, {clockSkew: 3000}) req.url = tempUrl // TODO - real key lookup with remote fetch - utils.getOrCreateActor(sigHead.keyId.replace(/.*\//, ''), db) + store.actor.getActor(sigHead.keyId.replace(/.*\//, ''), db) .then(user => { const valid = httpSignature.verifySignature(sigHead, user.publicKey.publicKeyPem) console.log('key validation', valid) @@ -31,7 +32,7 @@ router.post('/', net.validators.activity, function (req, res) { req.body._meta._target = req.body.object.id // send acceptance reply Promise.all([ - utils.getOrCreateActor(req.user, db, true), + store.actor.getOrCreateActor(req.user, db, true), request({ url: pub.utils.actorFromActivity(req.body), headers: {Accept: 'application/activity+json'}, diff --git a/routes/user.js b/routes/user.js index 24fec37..72cf8fe 100644 --- a/routes/user.js +++ b/routes/user.js @@ -1,8 +1,9 @@ -'use strict'; -const express = require('express'), - router = express.Router(); +'use strict' +const express = require('express') +const router = express.Router() const utils = require('../utils') const pub = require('../pub') +const store = require('../store') router.get('/:name', async function (req, res) { let name = req.params.name; @@ -11,7 +12,7 @@ router.get('/:name', async function (req, res) { } else { let db = req.app.get('db') - const user = await utils.getOrCreateActor(name, db) + const user = await store.actor.getOrCreateActor(name, db) if (user) { return res.json(pub.utils.toJSONLD(user)) } diff --git a/routes/webfinger.js b/routes/webfinger.js index 977cd57..c792719 100644 --- a/routes/webfinger.js +++ b/routes/webfinger.js @@ -1,7 +1,8 @@ -'use strict'; -const express = require('express'), - router = express.Router(); -const utils = require('../utils') +'use strict' +const express = require('express') +const router = express.Router() + +const store = require('../store') const acctReg = /acct:[@~]?([^@]+)@?(.*)/ router.get('/', function (req, res) { let resource = req.query.resource; @@ -13,7 +14,7 @@ router.get('/', function (req, res) { return res.status(400).send('Requested user is not from this domain') } let db = req.app.get('db'); - utils.getOrCreateActor(acct[1], db) + store.actor.getOrCreateActor(acct[1], db) .then(result => { if (!result) { return res.status(404).send(`${acct[1]}@${acct[2]} not found`) diff --git a/store/actor.js b/store/actor.js new file mode 100644 index 0000000..4ed787c --- /dev/null +++ b/store/actor.js @@ -0,0 +1,34 @@ +'use strict' +const pub = require('../pub') +module.exports = { + getActor, + getOrCreateActor, +} + +const actorProj = {_id: 0, _meta: 0} +const metaActorProj = {_id: 0} + +function getActor (preferredUsername, db, includeMeta) { + return db.collection('objects') + .find({id: pub.utils.usernameToIRI(preferredUsername)}) + .limit(1) + // strict comparison as we don't want to return private keys on accident + .project(includeMeta === true ? metaActorProj : actorProj) + .next() +} + +async function getOrCreateActor(preferredUsername, db, includeMeta) { + let user = await getActor(preferredUsername, db, includeMeta) + if (user) { + return user + } + // auto create groups whenever an unknown actor is referenced + user = await pub.actor.createLocalActor(preferredUsername, 'Group') + await db.collection('objects').insertOne(user) + // only executed on success + delete user._id + if (includeMeta !== true) { + delete user._meta + } + return user +} \ No newline at end of file diff --git a/db/index.js b/store/index.js similarity index 100% rename from db/index.js rename to store/index.js diff --git a/db/setup.js b/store/setup.js similarity index 100% rename from db/setup.js rename to store/setup.js diff --git a/utils/index.js b/utils/index.js index d32dabc..3847b29 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,49 +1,5 @@ -const crypto = require('crypto') -const {promisify} = require('util') -const utils = require('../utils') -const config = require('../config.json') -const db = require('../db') -const pub = require('../pub') - -module.exports.consts = require('./consts') - -function isObject(value) { - return value && typeof value === 'object' && value.constructor === Object +'use strict' +// misc utilities +module.exports = { + consts: require('./consts') } -// outtermost closure starts the recursion counter -// const level = 0; -function traverseObject(obj, f) { - const traverse = o => { - // const level = level + 1 - // if (level > 5) return o - traverseObject(o, f) - } - if (!isObject(obj)) return obj; - Object.keys(obj).forEach(traverse) - return f(obj); -} - -const actorProj = {_id: 0, _meta: 0} -const metaActorProj = {_id: 0} -async function getOrCreateActor(preferredUsername, db, includeMeta) { - const id = pub.utils.usernameToIRI(preferredUsername) - let user = await db.collection('objects') - .find({id: id}) - .limit(1) - // strict comparison as we don't want to return private keys on accident - .project(includeMeta === true ? metaActorProj : actorProj) - .next() - if (user) { - return user - } - // auto create groups whenever an unknown actor is referenced - user = await pub.actor.createLocalActor(preferredUsername, 'Group') - await db.collection('objects').insertOne(user) - // only executed on success - delete user._id - if (includeMeta !== true) { - delete user._meta - } - return user -} -module.exports.getOrCreateActor = getOrCreateActor \ No newline at end of file