|
1 | 1 | const request = require("request"); |
2 | | -const crypto = require("crypto"); |
| 2 | +const CryptoJS = require("crypto-js"); |
3 | 3 |
|
4 | 4 | const hmacSecret = process.env.HMAC_SECRET; |
5 | 5 | if (!hmacSecret || hmacSecret === "" || hmacSecret.trim() === "") { |
6 | | - console.warn( |
7 | | - "The hmac secret seems empty. This doesn't seem like what you want." |
8 | | - ); |
| 6 | + console.warn("The hmac secret seems empty. This doesn't seem like what you want."); |
9 | 7 | } |
10 | 8 | if (hmacSecret.length < 32) { |
11 | | - console.warn( |
12 | | - "The hmac secret seems week. You should use at least 32 secure random hex chars." |
13 | | - ); |
| 9 | + console.warn("The hmac secret seems week. You should use at least 32 secure random hex chars."); |
14 | 10 | } |
15 | 11 |
|
16 | 12 | const createHmacSignature = body => { |
17 | | - const hmac = crypto.createHmac("sha1", hmacSecret); |
18 | | - const bodySignature = hmac.update(JSON.stringify(body)).digest("hex"); |
19 | | - |
20 | | - return `sha1=${bodySignature}`; |
| 13 | + return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(JSON.stringify(body), hmacSecret)); |
21 | 14 | }; |
22 | 15 |
|
23 | 16 | function isJsonString(str) { |
24 | | - try { |
25 | | - const json = JSON.parse(str); |
26 | | - return typeof json === "object"; |
27 | | - } catch (e) { |
28 | | - return false; |
29 | | - } |
| 17 | + try { |
| 18 | + const json = JSON.parse(str); |
| 19 | + return typeof json === "object"; |
| 20 | + } catch (e) { |
| 21 | + return false; |
| 22 | + } |
30 | 23 | } |
31 | 24 |
|
32 | 25 | const uri = process.env.REQUEST_URI; |
33 | 26 | const data = { |
34 | | - data: isJsonString(process.env.REQUEST_DATA) |
35 | | - ? JSON.parse(process.env.REQUEST_DATA) |
36 | | - : process.env.REQUEST_DATA |
| 27 | + data: isJsonString(process.env.REQUEST_DATA) ? JSON.parse(process.env.REQUEST_DATA) : process.env.REQUEST_DATA |
37 | 28 | }; |
38 | 29 |
|
39 | 30 | const signature = createHmacSignature(data); |
40 | 31 |
|
41 | 32 | request( |
42 | | - { |
43 | | - method: "POST", |
44 | | - uri: uri, |
45 | | - |
46 | | - json: true, |
47 | | - body: data, |
48 | | - headers: { |
49 | | - "X-Hub-Signature": signature |
50 | | - } |
51 | | - }, |
52 | | - (error, response, body) => { |
53 | | - if (error || response.statusCode < 200 || response.statusCode > 299) { |
54 | | - // Something went wrong |
55 | | - console.error(`Request failed with status code ${response.statusCode}!`); |
56 | | - console.error(response.body); |
57 | | - |
58 | | - process.exit(1); |
59 | | - } else { |
60 | | - // Success |
61 | | - process.exit(); |
| 33 | + { |
| 34 | + method: `${process.env.METHOD ? process.env.METHOD : 'POST'}`, |
| 35 | + uri: uri, |
| 36 | + json: true, |
| 37 | + body: data, |
| 38 | + headers: { "X-Request-Signature": signature } |
| 39 | + }, |
| 40 | + (error, response, body) => { |
| 41 | + if (error || response.statusCode < 200 || response.statusCode > 299) { |
| 42 | + // Something went wrong |
| 43 | + console.error(`Request failed with status code ${response.statusCode}!`); |
| 44 | + console.error(response.body); |
| 45 | + |
| 46 | + process.exit(1); |
| 47 | + } else { |
| 48 | + // Success |
| 49 | + process.exit(); |
| 50 | + } |
62 | 51 | } |
63 | | - } |
64 | 52 | ); |
0 commit comments