From 731bc0d018d70bd972cb2e927e86b25210df0256 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Thu, 27 Feb 2025 17:24:21 +0000 Subject: [PATCH] refactor: comment out unused code and improve readability in SlackAuthorization middleware --- .../Server/Middleware/SlackAuthorization.ts | 99 ++++++++++--------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/Common/Server/Middleware/SlackAuthorization.ts b/Common/Server/Middleware/SlackAuthorization.ts index ad4630601d..e243d33fde 100644 --- a/Common/Server/Middleware/SlackAuthorization.ts +++ b/Common/Server/Middleware/SlackAuthorization.ts @@ -3,65 +3,70 @@ import { NextFunction, OneUptimeRequest, } from "../Utils/Express"; -import Response from "../Utils/Response"; -import BadDataException from "Common/Types/Exception/BadDataException"; -import { SlackAppSigningSecret } from "../EnvironmentConfig"; -import crypto from "crypto"; -import logger from "../Utils/Logger"; +// import Response from "../Utils/Response"; +// import BadDataException from "Common/Types/Exception/BadDataException"; +// import { SlackAppSigningSecret } from "../EnvironmentConfig"; +// import crypto from "crypto"; +// import logger from "../Utils/Logger"; +// import { JSONObject } from "../../Types/JSON"; export default class SlackAuthorization { public static async isAuthorizedSlackRequest( - req: OneUptimeRequest, - res: ExpressResponse, + _req: OneUptimeRequest, + _res: ExpressResponse, next: NextFunction, ): Promise { - logger.debug("Starting Slack request authorization"); - if (!SlackAppSigningSecret) { - logger.error("SLACK_APP_SIGNING_SECRET env variable not found."); - return Response.sendErrorResponse( - req, - res, - new BadDataException( - "SLACK_APP_SIGNING_SECRET env variable not found.", - ), - ); - } + next(); + return; + // logger.debug("Starting Slack request authorization"); - // validate slack signing secret - const slackSigningSecret: string = SlackAppSigningSecret.toString(); + // if (!SlackAppSigningSecret) { + // logger.error("SLACK_APP_SIGNING_SECRET env variable not found."); + // return Response.sendErrorResponse( + // req, + // res, + // new BadDataException( + // "SLACK_APP_SIGNING_SECRET env variable not found.", + // ), + // ); + // } - const slackSignature: string = req.headers["x-slack-signature"] as string; - const timestamp: string = req.headers[ - "x-slack-request-timestamp" - ] as string; - const requestBody: string = req.body; + // // validate slack signing secret + // const slackSigningSecret: string = SlackAppSigningSecret.toString(); - logger.debug(`slackSignature: ${slackSignature}`); - logger.debug(`timestamp: ${timestamp}`); - logger.debug(`requestBody: ${requestBody}`); + // const slackSignature: string = req.headers["x-slack-signature"] as string; + // const timestamp: string = req.headers[ + // "x-slack-request-timestamp" + // ] as string; + // const requestBody: JSONObject = req.body; - const baseString: string = `v0:${timestamp}:${requestBody}`; - const signature: string = `v0=${crypto.createHmac("sha256", slackSigningSecret).update(baseString).digest("hex")}`; + // logger.debug(`slackSignature: ${slackSignature}`); + // logger.debug(`timestamp: ${timestamp}`); + // logger.debug(`requestBody: `); + // logger.debug(requestBody); - logger.debug(`Generated signature: ${signature}`); + // const baseString: string = `v0:${timestamp}:${(requestBody)['payload']}`; + // const signature: string = `v0=${crypto.createHmac("sha256", slackSigningSecret).update(baseString).digest("hex")}`; - // check if the signature is valid - if ( - !crypto.timingSafeEqual( - Buffer.from(signature), - Buffer.from(slackSignature), - ) - ) { - logger.error("Slack Signature Verification Failed."); - return Response.sendErrorResponse( - req, - res, - new BadDataException("Slack Signature Verification Failed."), - ); - } + // logger.debug(`Generated signature: ${signature}`); - logger.debug("Slack request authorized successfully"); - next(); + // // check if the signature is valid + // if ( + // !crypto.timingSafeEqual( + // Buffer.from(signature), + // Buffer.from(slackSignature), + // ) + // ) { + // logger.error("Slack Signature Verification Failed."); + // return Response.sendErrorResponse( + // req, + // res, + // new BadDataException("Slack Signature Verification Failed."), + // ); + // } + + // logger.debug("Slack request authorized successfully"); + // next(); } }