-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
72 lines (50 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const crypto = require('crypto')
const app = express()
const port = process.env.PORT || 4000
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.status(200)
res.send(`Zoom Webhook sample successfully running. Set this URL with the /webhook path as your apps Event notification endpoint URL. https://github.com/zoom/webhook-sample`)
})
app.post('/webhook', (req, res) => {
var response
console.log(req.headers)
console.log(req.body)
// construct the message string
const message = `v0:${req.headers['x-zm-request-timestamp']}:${JSON.stringify(req.body)}`
const hashForVerify = crypto.createHmac('sha256', process.env.ZOOM_WEBHOOK_SECRET_TOKEN).update(message).digest('hex')
// hash the message string with your Webhook Secret Token and prepend the version semantic
const signature = `v0=${hashForVerify}`
// you validating the request came from Zoom https://marketplace.zoom.us/docs/api-reference/webhook-reference#notification-structure
if (req.headers['x-zm-signature'] === signature) {
// Zoom validating you control the webhook endpoint https://marketplace.zoom.us/docs/api-reference/webhook-reference#validate-webhook-endpoint
if(req.body.event === 'endpoint.url_validation') {
const hashForValidate = crypto.createHmac('sha256', process.env.ZOOM_WEBHOOK_SECRET_TOKEN).update(req.body.payload.plainToken).digest('hex')
response = {
message: {
plainToken: req.body.payload.plainToken,
encryptedToken: hashForValidate
},
status: 200
}
console.log(response.message)
res.status(response.status)
res.json(response.message)
} else {
response = { message: 'Authorized request to Zoom Webhook sample.', status: 200 }
console.log(response.message)
res.status(response.status)
res.json(response)
// business logic here, example make API request to Zoom or 3rd party
}
} else {
response = { message: 'Unauthorized request to Zoom Webhook sample.', status: 401 }
console.log(response.message)
res.status(response.status)
res.json(response)
}
})
app.listen(port, () => console.log(`Zoom Webhook sample listening on port ${port}!`))