diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-09 17:51:58 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | e4f97babf701481b55cc10fb3448feab5f97c867 (patch) | |
tree | af37402a594dc5ff09f71ecb0687e8cfe4cdb471 /server/helpers/requests.ts | |
parent | 343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff) | |
download | PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip |
Begin activitypub
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r-- | server/helpers/requests.ts | 78 |
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' |
10 | import { PodInstance } from '../models' | 10 | import { PodInstance } from '../models' |
11 | import { PodSignature } from '../../shared' | 11 | import { PodSignature } from '../../shared' |
12 | import { sign } from './peertube-crypto' | 12 | import { signObject } from './peertube-crypto' |
13 | |||
14 | function 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 | ||
14 | type MakeRetryRequestParams = { | 20 | type MakeRetryRequestParams = { |
15 | url: string, | 21 | url: string, |
@@ -31,61 +37,57 @@ function makeRetryRequest (params: MakeRetryRequestParams) { | |||
31 | } | 37 | } |
32 | 38 | ||
33 | type MakeSecureRequestParams = { | 39 | type 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 | } |
39 | function makeSecureRequest (params: MakeSecureRequestParams) { | 44 | function 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 | ||
88 | export { | 89 | export { |
90 | doRequest, | ||
89 | makeRetryRequest, | 91 | makeRetryRequest, |
90 | makeSecureRequest | 92 | makeSecureRequest |
91 | } | 93 | } |