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'),
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'));
});
}

View file

@ -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": "",

View file

@ -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) {