generate keys for local users

This commit is contained in:
Will Murphy 2019-09-14 21:49:33 -05:00
parent d7c733ae95
commit 0a3c758ad7
6 changed files with 66 additions and 26 deletions

View file

@ -4,5 +4,6 @@
"DOMAIN": "", "DOMAIN": "",
"PORT": "3000", "PORT": "3000",
"PRIVKEY_PATH": "", "PRIVKEY_PATH": "",
"CERT_PATH": "" "CERT_PATH": "",
"KEYPASS": ""
} }

View file

@ -1,3 +1,6 @@
const utils = require('../utils')
const crypto = require('crypto')
module.exports = async function dbSetup (db, domain) { module.exports = async function dbSetup (db, domain) {
await db.collection('streams').createIndex({ await db.collection('streams').createIndex({
_target: 1, _target: 1,
@ -7,24 +10,10 @@ module.exports = async function dbSetup (db, domain) {
actor: 1, actor: 1,
_id: -1, _id: -1,
}) })
const dummyUser = await utils.createLocalActor('dummy', 'Person')
await db.collection('objects').findOneAndReplace( await db.collection('objects').findOneAndReplace(
{preferredUsername: 'dummy'}, {preferredUsername: 'dummy'},
{ dummyUser,
id: `https://${domain}/u/dummy`,
"type": "Person",
"following": `https://${domain}/u/dummy/following`,
"followers": `https://${domain}/u/dummy/followers`,
"liked": `https://${domain}/u/dummy/liked`,
"inbox": `https://${domain}/u/dummy/inbox`,
"outbox": `https://${domain}/u/dummy/outbox`,
"preferredUsername": "dummy",
"name": "Dummy Person",
"summary": "Gotta have someone in the db",
"icon": `http://${domain}/f/dummy.png`,
attachment: [
`http://${domain}/f/dummy.glb`
]
},
{ {
upsert: true, upsert: true,
returnOriginal: false, returnOriginal: false,

View file

@ -20,7 +20,7 @@ router.get('/', function (req, res) {
db.collection('streams') db.collection('streams')
.find({_target: req.user}) .find({_target: req.user})
.sort({_id: -1}) .sort({_id: -1})
.project({_id: 0, _target: 0, '@context': 0, 'object._id': 0, 'object.@context': 0}) .project({_id: 0, _target: 0, _meta: 0, '@context': 0, 'object._id': 0, 'object.@context': 0, 'objecct._meta': 0})
.toArray() .toArray()
.then(stream => res.json(utils.arrayToCollection(stream, true))) .then(stream => res.json(utils.arrayToCollection(stream, true)))
.catch(err => { .catch(err => {

View file

@ -19,7 +19,7 @@ router.get('/', function (req, res) {
db.collection('streams') db.collection('streams')
.find({actor: utils.userNameToIRI(req.user)}) .find({actor: utils.userNameToIRI(req.user)})
.sort({_id: -1}) .sort({_id: -1})
.project({_id: 0, _target: 0, 'object._id': 0, 'object.@context': 0}) .project({_id: 0, _target: 0, _meta: 0, 'object._id': 0, 'object.@context': 0, 'object._meta': 0})
.toArray() .toArray()
.then(stream => res.json(utils.arrayToCollection(stream, true))) .then(stream => res.json(utils.arrayToCollection(stream, true)))
.catch(err => { .catch(err => {

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const express = require('express'), const express = require('express'),
router = express.Router(); router = express.Router();
// const inbox = require('./inbox'); const utils = require('../utils')
const {toJSONLD} = require('../utils/index.js'); const {toJSONLD} = require('../utils/index.js');
router.get('/:name', async function (req, res) { router.get('/:name', async function (req, res) {
@ -11,10 +11,14 @@ router.get('/:name', async function (req, res) {
} }
else { else {
let objs = req.app.get('objs'); let objs = req.app.get('objs');
const id = `https://${req.app.get('domain')}/u/${name}` let db = req.app.get('db')
const id = utils.userNameToIRI(name)
console.log(`looking up '${id}'`) console.log(`looking up '${id}'`)
const user = await objs.findOne({type: 'Person', id: id}, {fields: {_id: 0}}) const user = await db.collection('objects')
// .project({_id: 0}) .find({type: 'Person', id: id})
.limit(1)
.project({_id: 0, _meta: 0})
.next()
if (user) { if (user) {
return res.json(toJSONLD(user)) return res.json(toJSONLD(user))
} }

View file

@ -1,3 +1,5 @@
const crypto = require('crypto')
const {promisify} = require('util')
const {ASContext} = require('./consts') const {ASContext} = require('./consts')
module.exports.validators = require('./validators'); module.exports.validators = require('./validators');
const config = require('../config.json') const config = require('../config.json')
@ -32,6 +34,50 @@ module.exports.arrayToCollection = function (arr, ordered) {
} }
} }
module.exports.userNameToIRI = function (user) { function userNameToIRI (user) {
return `https://${config.DOMAIN}/u/${user}` return `https://${config.DOMAIN}/u/${user}`
} }
module.exports.userNameToIRI = userNameToIRI
const generateKeyPairPromise = promisify(crypto.generateKeyPair)
module.exports.createLocalActor = function (name, type) {
return generateKeyPairPromise('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: config.KEYPASS
}
}).then(pair => {
const actorBase = userNameToIRI(name);
return {
_meta: {
privateKey: pair.privateKey,
},
id: `${actorBase}`,
"type": type,
"following": `${actorBase}/following`,
"followers": `${actorBase}/followers`,
"liked": `${actorBase}/liked`,
"inbox": `${actorBase}/inbox`,
"outbox": `${actorBase}/outbox`,
"preferredUsername": name,
"name": "Dummy Person",
"summary": "Gotta have someone in the db",
"icon": `http://${config.DOMAIN}/f/${name}.png`,
attachment: [
`http://${config.DOMAIN}/f/${name}.glb`
],
publicKey: {
'id': `${actorBase}#main-key`,
'owner': `${actorBase}`,
'publicKeyPem': pair.publicKey
},
}
})
}