switch to node native crytpo keypair gen for easier setup on windows, activate https

This commit is contained in:
william Murphy 2019-09-09 17:43:06 -05:00
parent ed7ade3fa6
commit ea191e0806
3 changed files with 23 additions and 6 deletions

View file

@ -9,6 +9,7 @@ const routes = require('./routes'),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
cors = require('cors'), cors = require('cors'),
http = require('http'), http = require('http'),
https = require('https'),
basicAuth = require('express-basic-auth'); basicAuth = require('express-basic-auth');
let sslOptions; let sslOptions;
@ -73,3 +74,8 @@ app.use('/api/inbox', cors(), routes.inbox);
http.createServer(app).listen(app.get('port'), function(){ http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port')); 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'));
});
}

View file

@ -9,7 +9,6 @@
"cors": "^2.8.4", "cors": "^2.8.4",
"express": "^4.16.3", "express": "^4.16.3",
"express-basic-auth": "^1.1.5", "express-basic-auth": "^1.1.5",
"generate-rsa-keypair": "^0.1.2",
"request": "^2.87.0" "request": "^2.87.0"
}, },
"engines": { "engines": {
@ -17,6 +16,7 @@
}, },
"devDependencies": {}, "devDependencies": {},
"scripts": { "scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "", "author": "",

View file

@ -1,8 +1,7 @@
'use strict'; 'use strict';
const express = require('express'), const express = require('express'),
router = express.Router(), router = express.Router(),
crypto = require('crypto'), crypto = require('crypto');
generateRSAKeypair = require('generate-rsa-keypair');
function createActor(name, domain, pubkey) { function createActor(name, domain, pubkey) {
return { return {
@ -48,12 +47,24 @@ router.post('/create', function (req, res) {
let db = req.app.get('db'); let db = req.app.get('db');
let domain = req.app.get('domain'); let domain = req.app.get('domain');
// create keypair // create keypair
var pair = generateRSAKeypair(); var pair = crypto.generateKeyPairSync('rsa', {
let actorRecord = createActor(account, domain, pair.public); 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); let webfingerRecord = createWebfinger(account, domain);
const apikey = crypto.randomBytes(16).toString('hex'); const apikey = crypto.randomBytes(16).toString('hex');
try { 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}); res.status(200).json({msg: 'ok', apikey});
} }
catch(e) { catch(e) {