-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3044 challenge handshake and event verification
- Loading branch information
Showing
5 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Request, Response } from 'express'; | ||
import crypto from 'crypto'; | ||
import slackServices from '../services/slack.services'; | ||
|
||
export default class slackController { | ||
static async handleEvent(req: Request, res: Response) { | ||
console.log('got a slack req'); | ||
if (req.body.type === 'url_verification') { | ||
return res.status(200).send({ challenge: req.body.challenge }); | ||
} | ||
|
||
const slackSignature = req.headers['x-slack-signature'] as string; | ||
const slackTimeStamp = req.headers['X-Slack-Request-Timestamp'] as string; | ||
|
||
if (Math.abs(Date.now() - Number(slackTimeStamp) * 1000) > 60 * 5 * 1000) { | ||
return res.status(400).send('Slack request verification failed due to expired timestamp'); | ||
} | ||
|
||
const reqBody = req.body; | ||
|
||
const signatureBase = 'v0:' + slackTimeStamp + ':' + reqBody; | ||
|
||
const finalSignature = | ||
'v0=' + | ||
crypto | ||
.createHmac('sha256', process.env.SLACK_BOT_TOKEN ? process.env.SLACK_BOT_TOKEN : '') | ||
.update(signatureBase) | ||
.digest('hex'); | ||
|
||
if ( | ||
crypto.timingSafeEqual( | ||
Uint8Array.from(Buffer.from(finalSignature, 'utf8')), | ||
Uint8Array.from(Buffer.from(slackSignature, 'utf8')) | ||
) | ||
) { | ||
slackServices.processEvent(req.body); | ||
return res.status(200).send('Event recieved'); | ||
} | ||
|
||
return res.status(400).send('Slack request verification failed due to incorrect signature'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from 'express'; | ||
import slackController from '../controllers/slack.controllers'; | ||
|
||
const slackRouter = express.Router(); | ||
|
||
slackRouter.post('/', slackController.handleEvent); | ||
|
||
export default slackRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default class slackServices { | ||
static async processEvent(req: any ) { | ||
//TODO: process request | ||
console.log(req); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters