diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-19 21:34:51 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-19 21:34:51 +0200 |
commit | e861452fb26553177ad4e32bfb18b4fd8a5b1816 (patch) | |
tree | 7c0cfd464709243a452b431665f5107a973df682 /server/helpers/peertube-crypto.js | |
parent | 5c39adb7313e0696aabb4b71196ab7b0b378c359 (diff) | |
download | PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.gz PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.zst PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.zip |
Server: put config in constants
Diffstat (limited to 'server/helpers/peertube-crypto.js')
-rw-r--r-- | server/helpers/peertube-crypto.js | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 46dff8d03..ef130ea5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js | |||
@@ -1,15 +1,13 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const config = require('config') | ||
4 | const crypto = require('crypto') | 3 | const crypto = require('crypto') |
5 | const fs = require('fs') | 4 | const fs = require('fs') |
6 | const openssl = require('openssl-wrapper') | 5 | const openssl = require('openssl-wrapper') |
7 | const path = require('path') | ||
8 | const ursa = require('ursa') | 6 | const ursa = require('ursa') |
9 | 7 | ||
8 | const constants = require('../initializers/constants') | ||
10 | const logger = require('./logger') | 9 | const logger = require('./logger') |
11 | 10 | ||
12 | const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) | ||
13 | const algorithm = 'aes-256-ctr' | 11 | const algorithm = 'aes-256-ctr' |
14 | 12 | ||
15 | const peertubeCrypto = { | 13 | const peertubeCrypto = { |
@@ -17,7 +15,6 @@ const peertubeCrypto = { | |||
17 | createCertsIfNotExist: createCertsIfNotExist, | 15 | createCertsIfNotExist: createCertsIfNotExist, |
18 | decrypt: decrypt, | 16 | decrypt: decrypt, |
19 | encrypt: encrypt, | 17 | encrypt: encrypt, |
20 | getCertDir: getCertDir, | ||
21 | sign: sign | 18 | sign: sign |
22 | } | 19 | } |
23 | 20 | ||
@@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) { | |||
40 | } | 37 | } |
41 | 38 | ||
42 | function decrypt (key, data, callback) { | 39 | function decrypt (key, data, callback) { |
43 | fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { | 40 | fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { |
44 | if (err) return callback(err) | 41 | if (err) return callback(err) |
45 | 42 | ||
46 | const myPrivateKey = ursa.createPrivateKey(file) | 43 | const myPrivateKey = ursa.createPrivateKey(file) |
@@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) { | |||
67 | }) | 64 | }) |
68 | } | 65 | } |
69 | 66 | ||
70 | function getCertDir () { | ||
71 | return certDir | ||
72 | } | ||
73 | |||
74 | function sign (data) { | 67 | function sign (data) { |
75 | const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) | 68 | const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) |
76 | const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') | 69 | const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') |
77 | 70 | ||
78 | return signature | 71 | return signature |
@@ -85,7 +78,7 @@ module.exports = peertubeCrypto | |||
85 | // --------------------------------------------------------------------------- | 78 | // --------------------------------------------------------------------------- |
86 | 79 | ||
87 | function certsExist (callback) { | 80 | function certsExist (callback) { |
88 | fs.exists(certDir + 'peertube.key.pem', function (exists) { | 81 | fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { |
89 | return callback(exists) | 82 | return callback(exists) |
90 | }) | 83 | }) |
91 | } | 84 | } |
@@ -99,15 +92,25 @@ function createCerts (callback) { | |||
99 | } | 92 | } |
100 | 93 | ||
101 | logger.info('Generating a RSA key...') | 94 | logger.info('Generating a RSA key...') |
102 | openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { | 95 | |
96 | let options = { | ||
97 | 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', | ||
98 | '2048': false | ||
99 | } | ||
100 | openssl.exec('genrsa', options, function (err) { | ||
103 | if (err) { | 101 | if (err) { |
104 | logger.error('Cannot create private key on this pod.') | 102 | logger.error('Cannot create private key on this pod.') |
105 | return callback(err) | 103 | return callback(err) |
106 | } | 104 | } |
107 | logger.info('RSA key generated.') | 105 | logger.info('RSA key generated.') |
108 | 106 | ||
107 | options = { | ||
108 | 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', | ||
109 | 'pubout': true, | ||
110 | 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' | ||
111 | } | ||
109 | logger.info('Manage public key...') | 112 | logger.info('Manage public key...') |
110 | openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { | 113 | openssl.exec('rsa', options, function (err) { |
111 | if (err) { | 114 | if (err) { |
112 | logger.error('Cannot create public key on this pod.') | 115 | logger.error('Cannot create public key on this pod.') |
113 | return callback(err) | 116 | return callback(err) |