Skip to content

Commit

Permalink
Fix signature for no body.
Browse files Browse the repository at this point in the history
  • Loading branch information
navied committed Oct 28, 2020
1 parent a2fd1bf commit d229718
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
16 changes: 7 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2946,7 +2946,6 @@ module.exports.wrap = wrap;
const axios = __webpack_require__(545);
const crypto = __webpack_require__(417);
const core = __webpack_require__(186);

const hmacSecret = core.getInput('hmacSecret');

if (!hmacSecret || hmacSecret === "" || hmacSecret.trim() === "") {
Expand All @@ -2960,9 +2959,11 @@ if (hmacSecret.length < 32) {

const createHmacSignature = body => {
const hmac = crypto.createHmac("sha256", hmacSecret);
const bodySignature = hmac.update(JSON.stringify(body)).digest("hex");

return `${bodySignature}`;
if (body === "") {
return hmac.digest("hex");
} else {
return hmac.update(JSON.stringify(body)).digest("hex");
}
};

function isJsonString(str) {
Expand All @@ -2976,21 +2977,18 @@ function isJsonString(str) {

const url = core.getInput('url');
const dataInput = core.getInput('data');

const data = isJsonString(dataInput) ? JSON.parse(dataInput) : dataInput;


const signature = createHmacSignature(data);

axios.post(url, data, {
headers: {
"X-Hub-Signature": signature,
"X-Hub-SHA": process.env.GITHUB_SHA
}
}).then(function (res) {
}).then(function () {
core.info(`Webhook sent sucessfully`)
}).catch(function (error) {
core.setFailed(`Request failed with status code ${error.response.status}!`);
core.setFailed(`Request failed with status code ${error.response.status}`);
});

/***/ }),
Expand Down
16 changes: 7 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const axios = require("axios");
const crypto = require("crypto");
const core = require('@actions/core');

const hmacSecret = core.getInput('hmacSecret');

if (!hmacSecret || hmacSecret === "" || hmacSecret.trim() === "") {
Expand All @@ -15,9 +14,11 @@ if (hmacSecret.length < 32) {

const createHmacSignature = body => {
const hmac = crypto.createHmac("sha256", hmacSecret);
const bodySignature = hmac.update(JSON.stringify(body)).digest("hex");

return `${bodySignature}`;
if (body === "") {
return hmac.digest("hex");
} else {
return hmac.update(JSON.stringify(body)).digest("hex");
}
};

function isJsonString(str) {
Expand All @@ -31,19 +32,16 @@ function isJsonString(str) {

const url = core.getInput('url');
const dataInput = core.getInput('data');

const data = isJsonString(dataInput) ? JSON.parse(dataInput) : dataInput;


const signature = createHmacSignature(data);

axios.post(url, data, {
headers: {
"X-Hub-Signature": signature,
"X-Hub-SHA": process.env.GITHUB_SHA
}
}).then(function (res) {
}).then(function () {
core.info(`Webhook sent sucessfully`)
}).catch(function (error) {
core.setFailed(`Request failed with status code ${error.response.status}!`);
core.setFailed(`Request failed with status code ${error.response.status}`);
});

0 comments on commit d229718

Please sign in to comment.