unpkg/modules/secretKey.js

32 lines
819 B
JavaScript
Raw Normal View History

2018-12-17 17:38:05 +00:00
const fs = require('fs');
const path = require('path');
const forge = require('node-forge');
const invariant = require('invariant');
2018-06-09 06:01:13 +00:00
let secretKey;
2018-12-17 17:38:05 +00:00
if (process.env.NODE_ENV === 'production') {
2018-06-09 06:01:13 +00:00
invariant(
process.env.PRIVATE_KEY,
2018-12-17 17:38:05 +00:00
'Missing $PRIVATE_KEY environment variable'
2018-06-09 06:01:13 +00:00
);
secretKey = {
public: fs.readFileSync(
2018-12-17 17:38:05 +00:00
path.resolve(__dirname, '../secret_key.pub'),
'utf8'
2018-06-09 06:01:13 +00:00
),
private: process.env.PRIVATE_KEY
};
} else {
// Generate a random keypair for dev/testing.
// See https://gist.github.com/sebadoom/2b70969e70db5da9a203bebd9cff099f
const keypair = forge.rsa.generateKeyPair({ bits: 2048 });
secretKey = {
public: forge.pki.publicKeyToPem(keypair.publicKey, 72),
private: forge.pki.privateKeyToPem(keypair.privateKey, 72)
};
}
module.exports = secretKey;