aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/peertube-crypto.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-17 21:42:47 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-17 21:42:47 +0100
commit15103f11ec54989a0dee7fc33daa458ecb116fe4 (patch)
treeb25f8ee33f32ccd61df46afc27a2a4628f9f0be5 /server/helpers/peertube-crypto.js
parent1e4b0080ff1b4e802f71ec1f4cbf8cbcc70cdcbd (diff)
downloadPeerTube-15103f11ec54989a0dee7fc33daa458ecb116fe4.tar.gz
PeerTube-15103f11ec54989a0dee7fc33daa458ecb116fe4.tar.zst
PeerTube-15103f11ec54989a0dee7fc33daa458ecb116fe4.zip
Server: paths refractoring
Diffstat (limited to 'server/helpers/peertube-crypto.js')
-rw-r--r--server/helpers/peertube-crypto.js38
1 files changed, 28 insertions, 10 deletions
diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js
index 0f1e02ad6..ef6808d5c 100644
--- a/server/helpers/peertube-crypto.js
+++ b/server/helpers/peertube-crypto.js
@@ -4,6 +4,7 @@ const crypto = require('crypto')
4const bcrypt = require('bcrypt') 4const bcrypt = require('bcrypt')
5const fs = require('fs') 5const fs = require('fs')
6const openssl = require('openssl-wrapper') 6const openssl = require('openssl-wrapper')
7const pathUtils = require('path')
7 8
8const constants = require('../initializers/constants') 9const constants = require('../initializers/constants')
9const logger = require('./logger') 10const logger = require('./logger')
@@ -13,6 +14,8 @@ const peertubeCrypto = {
13 comparePassword, 14 comparePassword,
14 createCertsIfNotExist, 15 createCertsIfNotExist,
15 cryptPassword, 16 cryptPassword,
17 getMyPrivateCert,
18 getMyPublicCert,
16 sign 19 sign
17} 20}
18 21
@@ -55,7 +58,8 @@ function sign (data) {
55 sign.update(dataString, 'utf8') 58 sign.update(dataString, 'utf8')
56 59
57 // TODO: make async 60 // TODO: make async
58 const myKey = fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem') 61 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
62 const myKey = fs.readFileSync(certPath)
59 const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING) 63 const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING)
60 64
61 return signature 65 return signature
@@ -91,6 +95,16 @@ function cryptPassword (password, callback) {
91 }) 95 })
92} 96}
93 97
98function getMyPrivateCert (callback) {
99 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
100 fs.readFile(certPath, 'utf8', callback)
101}
102
103function getMyPublicCert (callback) {
104 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PUBLIC_CERT_NAME)
105 fs.readFile(certPath, 'utf8', callback)
106}
107
94// --------------------------------------------------------------------------- 108// ---------------------------------------------------------------------------
95 109
96module.exports = peertubeCrypto 110module.exports = peertubeCrypto
@@ -98,7 +112,8 @@ module.exports = peertubeCrypto
98// --------------------------------------------------------------------------- 112// ---------------------------------------------------------------------------
99 113
100function certsExist (callback) { 114function certsExist (callback) {
101 fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { 115 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
116 fs.exists(certPath, function (exists) {
102 return callback(exists) 117 return callback(exists)
103 }) 118 })
104} 119}
@@ -113,24 +128,27 @@ function createCerts (callback) {
113 128
114 logger.info('Generating a RSA key...') 129 logger.info('Generating a RSA key...')
115 130
116 let options = { 131 const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
117 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', 132 const genRsaOptions = {
133 'out': privateCertPath,
118 '2048': false 134 '2048': false
119 } 135 }
120 openssl.exec('genrsa', options, function (err) { 136 openssl.exec('genrsa', genRsaOptions, function (err) {
121 if (err) { 137 if (err) {
122 logger.error('Cannot create private key on this pod.') 138 logger.error('Cannot create private key on this pod.')
123 return callback(err) 139 return callback(err)
124 } 140 }
141
125 logger.info('RSA key generated.') 142 logger.info('RSA key generated.')
143 logger.info('Managing public key...')
126 144
127 options = { 145 const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
128 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', 146 const rsaOptions = {
147 'in': privateCertPath,
129 'pubout': true, 148 'pubout': true,
130 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' 149 'out': publicCertPath
131 } 150 }
132 logger.info('Manage public key...') 151 openssl.exec('rsa', rsaOptions, function (err) {
133 openssl.exec('rsa', options, function (err) {
134 if (err) { 152 if (err) {
135 logger.error('Cannot create public key on this pod.') 153 logger.error('Cannot create public key on this pod.')
136 return callback(err) 154 return callback(err)