From e4f97babf701481b55cc10fb3448feab5f97c867 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Nov 2017 17:51:58 +0100 Subject: Begin activitypub --- server/helpers/requests.ts | 78 ++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 38 deletions(-) (limited to 'server/helpers/requests.ts') diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index af1f401de..8c4c983f7 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -9,7 +9,13 @@ import { } from '../initializers' import { PodInstance } from '../models' import { PodSignature } from '../../shared' -import { sign } from './peertube-crypto' +import { signObject } from './peertube-crypto' + +function doRequest (requestOptions: request.CoreOptions & request.UriOptions) { + return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { + request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body })) + }) +} type MakeRetryRequestParams = { url: string, @@ -31,61 +37,57 @@ function makeRetryRequest (params: MakeRetryRequestParams) { } type MakeSecureRequestParams = { - method: 'GET' | 'POST' toPod: PodInstance path: string data?: Object } function makeSecureRequest (params: MakeSecureRequestParams) { - return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { - const requestParams: { - url: string, - json: { - signature: PodSignature, - data: any - } - } = { - url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, - json: { - signature: null, - data: null - } + const requestParams: { + method: 'POST', + uri: string, + json: { + signature: PodSignature, + data: any } - - if (params.method !== 'POST') { - return rej(new Error('Cannot make a secure request with a non POST method.')) + } = { + method: 'POST', + uri: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, + json: { + signature: null, + data: null } + } - 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).then(signature => { + requestParams.json.signature = { + host, // Which host we pretend to be + signature + } - // If there are data information - if (params.data) { - requestParams.json.data = params.data - } + // If there are data information + if (params.data) { + requestParams.json.data = params.data + } - request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body })) - }) + return doRequest(requestParams) }) } // --------------------------------------------------------------------------- export { + doRequest, makeRetryRequest, makeSecureRequest } -- cgit v1.2.3