Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuglajbclajep committed Apr 17, 2021
1 parent daea71f commit a924bc9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ addEventListener("fetch", (event) => {
});

async function handleRequest(request: Request) {
if (!(await verify(request))) return new Response(null, { status: 403 });
// caution: request body can be got only once
const body = await request.text();
if (!verify(request.headers, body)) return new Response("", { status: 403 });

const params: Record<string, string> = [
...new URLSearchParams(await request.text()).entries(),
...new URLSearchParams(body).entries(),
].reduce((ps, [k, v]) => ({ ...ps, [k]: v }), {});
const twitter = new Twitter({
consumer_key: TWITTER_CONSUMER_KEY,
Expand Down
16 changes: 5 additions & 11 deletions src/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ import Hex from "crypto-js/enc-hex";
import hmacSHA256 from "crypto-js/hmac-sha256";

// see https://api.slack.com/authentication/verifying-requests-from-slack
async function verify(request: Request): Promise<boolean> {
const timestamp = request.headers.get("X-Slack-Request-Timestamp");
const baseString = `v0:${timestamp}:${await request.text()}`;
function verify(headers: Headers, body: string): boolean {
const timestamp = headers.get("X-Slack-Request-Timestamp");
const baseString = `v0:${timestamp}:${body}`;
const signature = Hex.stringify(hmacSHA256(baseString, SLACK_SIGNING_SECRET));
return `v0=${signature}` === request.headers.get("X-Slack-Signature");
}

// TODO: when verify() works, delete it
async function deprecatedVerify(request: Request): Promise<boolean> {
const token = new URLSearchParams(await request.text()).get("token");
return token === SLACK_VERIFICATION_TOKEN;
return `v0=${signature}` === headers.get("X-Slack-Signature");
}

function errorMessage(text: string): object {
Expand All @@ -23,4 +17,4 @@ function errorMessage(text: string): object {
};
}

export { deprecatedVerify as verify, errorMessage };
export { verify, errorMessage };

0 comments on commit a924bc9

Please sign in to comment.