From 709756b8e183f67ef9bf8f7bc149af4736260350 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 7 Jul 2017 16:57:28 +0200 Subject: Async signature and various fixes --- server/helpers/peertube-crypto.ts | 13 +++++-------- server/helpers/requests.ts | 36 +++++++++++++++++------------------- 2 files changed, 22 insertions(+), 27 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts index 8e8001cd6..0c73e8539 100644 --- a/server/helpers/peertube-crypto.ts +++ b/server/helpers/peertube-crypto.ts @@ -1,5 +1,5 @@ import * as crypto from 'crypto' -import * as fs from 'fs' +import * as Promise from 'bluebird' import { join } from 'path' import { @@ -52,18 +52,15 @@ function sign (data: string|Object) { dataString = JSON.stringify(data) } catch (err) { logger.error('Cannot sign data.', { error: err }) - return '' + return Promise.resolve('') } } sign.update(dataString, 'utf8') - // TODO: make async - const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) - const myKey = fs.readFileSync(certPath) - const signature = sign.sign(myKey.toString(), SIGNATURE_ENCODING) - - return signature + return getMyPrivateCert().then(myKey => { + return sign.sign(myKey, SIGNATURE_ENCODING) + }) } function comparePassword (plainPassword: string, hashPassword: string) { diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index b31074373..183f6df0d 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -33,7 +33,6 @@ type MakeSecureRequestParams = { method: 'GET'|'POST' toPod: PodInstance path: string - sign: boolean data?: Object } function makeSecureRequest (params: MakeSecureRequestParams) { @@ -47,31 +46,30 @@ function makeSecureRequest (params: MakeSecureRequestParams) { return rej(new Error('Cannot make a secure request with a non POST method.')) } - // Add signature if it is specified in the params - if (params.sign === true) { - const host = CONFIG.WEBSERVER.HOST + const host = CONFIG.WEBSERVER.HOST - let dataToSign - if (params.data) { - dataToSign = params.data - } else { - // We do not have data to sign so we just take our host - // It is not ideal but the connection should be in HTTPS - dataToSign = host - } + let dataToSign + if (params.data) { + dataToSign = params.data + } else { + // We do not have data to sign so we just take our host + // It is not ideal but the connection should be in HTTPS + dataToSign = host + } + sign(dataToSign).then(signature => { requestParams.json['signature'] = { host, // Which host we pretend to be - signature: sign(dataToSign) + signature } - } - // If there are data informations - if (params.data) { - requestParams.json['data'] = params.data - } + // If there are data informations + if (params.data) { + requestParams.json['data'] = params.data + } - request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body })) + request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body })) + }) }) } -- cgit v1.2.3