From ea191e08068a660d7e6c00bcb84075098fd8c5cc Mon Sep 17 00:00:00 2001 From: william Murphy Date: Mon, 9 Sep 2019 17:43:06 -0500 Subject: [PATCH] switch to node native crytpo keypair gen for easier setup on windows, activate https --- index.js | 6 ++++++ package.json | 2 +- routes/admin.js | 21 ++++++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index e4b9140..ce8b361 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const routes = require('./routes'), bodyParser = require('body-parser'), cors = require('cors'), http = require('http'), + https = require('https'), basicAuth = require('express-basic-auth'); let sslOptions; @@ -73,3 +74,8 @@ app.use('/api/inbox', cors(), routes.inbox); http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); +if (sslOptions) { + https.createServer(sslOptions, app).listen(app.get('port-https'), function () { + console.log('Express server listening on port ' + app.get('port-https')); + }); +} \ No newline at end of file diff --git a/package.json b/package.json index 89b33d2..49afcd5 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "cors": "^2.8.4", "express": "^4.16.3", "express-basic-auth": "^1.1.5", - "generate-rsa-keypair": "^0.1.2", "request": "^2.87.0" }, "engines": { @@ -17,6 +16,7 @@ }, "devDependencies": {}, "scripts": { + "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", diff --git a/routes/admin.js b/routes/admin.js index 0398389..bb8f38d 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -1,8 +1,7 @@ 'use strict'; const express = require('express'), router = express.Router(), - crypto = require('crypto'), - generateRSAKeypair = require('generate-rsa-keypair'); + crypto = require('crypto'); function createActor(name, domain, pubkey) { return { @@ -48,12 +47,24 @@ router.post('/create', function (req, res) { let db = req.app.get('db'); let domain = req.app.get('domain'); // create keypair - var pair = generateRSAKeypair(); - let actorRecord = createActor(account, domain, pair.public); + var pair = crypto.generateKeyPairSync('rsa', { + modulusLength: 4096, + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem', + cipher: 'aes-256-cbc', + passphrase: 'top secret' + } + }); + let actorRecord = createActor(account, domain, pair.publicKey); let webfingerRecord = createWebfinger(account, domain); const apikey = crypto.randomBytes(16).toString('hex'); try { - db.prepare('insert or replace into accounts(name, actor, apikey, pubkey, privkey, webfinger) values(?, ?, ?, ?, ?, ?)').run(`${account}@${domain}`, JSON.stringify(actorRecord), apikey, pair.public, pair.private, JSON.stringify(webfingerRecord)); + db.prepare('insert or replace into accounts(name, actor, apikey, pubkey, privkey, webfinger) values(?, ?, ?, ?, ?, ?)').run(`${account}@${domain}`, JSON.stringify(actorRecord), apikey, pair.publicKey, pair.privateKey, JSON.stringify(webfingerRecord)); res.status(200).json({msg: 'ok', apikey}); } catch(e) {