Skip to content

Commit

Permalink
chore : auth added for webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyadip007 committed Feb 2, 2024
1 parent 4a297f0 commit e94e08f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
19 changes: 13 additions & 6 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const crypto = require('crypto');
const { config } = require('../config');
const { validateToken } = require('../services/common');

const github = function (req, res, next) {
if (!verifyGithubSignature(req)) {
Expand All @@ -9,21 +10,27 @@ const github = function (req, res, next) {
next();
};

const gitlab = function (req, res, next) {
if (!verifyGitlabSignature(req)) {
const gitlab = async function (req, res, next) {
if (!(await verifyGitlabSignature(req))) {
res.status(401).send('Unauthorized');
return;
}
}8
next();
};

const verifyGithubSignature = (req) => {
const verifyGithubSignature = async (req) => {
const githubSignature = crypto.createHmac('sha256', config.githubWebhookSecret).update(JSON.stringify(req.body)).digest('hex');
return `sha256=${githubSignature}` === req.headers['x-hub-signature-256'];
};

const verifyGitlabSignature = (req) => {
return config.gitlabWebhookSecret === req.headers['x-gitlab-token'];
const verifyGitlabSignature =async (req) => {
try{
await validateToken(req.headers['x-gitlab-token']);
}
catch(error){
return false;
}
return true;
};

module.exports = { github, gitlab };
18 changes: 17 additions & 1 deletion services/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ const orchestratorDeploymentRequest = async (data) => {
}
};

const validateToken = async (token) => {
log.info(token);
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
};
try {
const response = await axios.get(`${config.orchestratorBaseUrl}/apikey/validate`, { headers });
log.info(response?.data);
} catch (error) {
log.error(error);
throw new Error(error?.response?.data.message);
}
};

const orchestratorEnvListRequest = async (repoUrl, contextDir) => {
const headers = {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -51,5 +66,6 @@ const createOrchestratorPayload = (payload, contextDir, envs, ref, ephemeral) =>
module.exports = {
orchestratorDeploymentRequest,
createOrchestratorPayload,
orchestratorEnvListRequest
orchestratorEnvListRequest,
validateToken
};

0 comments on commit e94e08f

Please sign in to comment.