aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/requests.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r--server/helpers/requests.ts78
1 files changed, 40 insertions, 38 deletions
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 {
9} from '../initializers' 9} from '../initializers'
10import { PodInstance } from '../models' 10import { PodInstance } from '../models'
11import { PodSignature } from '../../shared' 11import { PodSignature } from '../../shared'
12import { sign } from './peertube-crypto' 12import { signObject } from './peertube-crypto'
13
14function doRequest (requestOptions: request.CoreOptions & request.UriOptions) {
15 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
16 request(requestOptions, (err, response, body) => err ? rej(err) : res({ response, body }))
17 })
18}
13 19
14type MakeRetryRequestParams = { 20type MakeRetryRequestParams = {
15 url: string, 21 url: string,
@@ -31,61 +37,57 @@ function makeRetryRequest (params: MakeRetryRequestParams) {
31} 37}
32 38
33type MakeSecureRequestParams = { 39type MakeSecureRequestParams = {
34 method: 'GET' | 'POST'
35 toPod: PodInstance 40 toPod: PodInstance
36 path: string 41 path: string
37 data?: Object 42 data?: Object
38} 43}
39function makeSecureRequest (params: MakeSecureRequestParams) { 44function makeSecureRequest (params: MakeSecureRequestParams) {
40 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { 45 const requestParams: {
41 const requestParams: { 46 method: 'POST',
42 url: string, 47 uri: string,
43 json: { 48 json: {
44 signature: PodSignature, 49 signature: PodSignature,
45 data: any 50 data: any
46 }
47 } = {
48 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
49 json: {
50 signature: null,
51 data: null
52 }
53 } 51 }
54 52 } = {
55 if (params.method !== 'POST') { 53 method: 'POST',
56 return rej(new Error('Cannot make a secure request with a non POST method.')) 54 uri: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
55 json: {
56 signature: null,
57 data: null
57 } 58 }
59 }
58 60
59 const host = CONFIG.WEBSERVER.HOST 61 const host = CONFIG.WEBSERVER.HOST
60 62
61 let dataToSign 63 let dataToSign
62 if (params.data) { 64 if (params.data) {
63 dataToSign = params.data 65 dataToSign = params.data
64 } else { 66 } else {
65 // We do not have data to sign so we just take our host 67 // We do not have data to sign so we just take our host
66 // It is not ideal but the connection should be in HTTPS 68 // It is not ideal but the connection should be in HTTPS
67 dataToSign = host 69 dataToSign = host
68 } 70 }
69 71
70 sign(dataToSign).then(signature => { 72 sign(dataToSign).then(signature => {
71 requestParams.json.signature = { 73 requestParams.json.signature = {
72 host, // Which host we pretend to be 74 host, // Which host we pretend to be
73 signature 75 signature
74 } 76 }
75 77
76 // If there are data information 78 // If there are data information
77 if (params.data) { 79 if (params.data) {
78 requestParams.json.data = params.data 80 requestParams.json.data = params.data
79 } 81 }
80 82
81 request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body })) 83 return doRequest(requestParams)
82 })
83 }) 84 })
84} 85}
85 86
86// --------------------------------------------------------------------------- 87// ---------------------------------------------------------------------------
87 88
88export { 89export {
90 doRequest,
89 makeRetryRequest, 91 makeRetryRequest,
90 makeSecureRequest 92 makeSecureRequest
91} 93}