Skip to content

Commit

Permalink
refactor: comment out unused code and improve readability in SlackAut…
Browse files Browse the repository at this point in the history
…horization middleware
  • Loading branch information
simlarsen committed Feb 27, 2025
1 parent 4b3ce59 commit 731bc0d
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions Common/Server/Middleware/SlackAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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();
}
}

0 comments on commit 731bc0d

Please sign in to comment.