|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 001: Validate webhook message using HMAC |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const validator = require('validator'); |
| 9 | +const { formatString, API_TYPES } = require('../../utils.js'); |
| 10 | +const { getExampleByNumber } = require('../../manifestService'); |
| 11 | +const dsConfig = require('../../../config/index.js').config; |
| 12 | +const { computeHash } = require('../examples/validateWebhookMessage'); |
| 13 | + |
| 14 | +const eg001ValidateWebhookMessage = exports; |
| 15 | +const exampleNumber = 1; |
| 16 | +const eg = `cneg00${exampleNumber}`; // This example reference. |
| 17 | +const api = API_TYPES.CONNECT; |
| 18 | +const mustAuthenticate = '/ds/mustAuthenticateJWT'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Create the envelope |
| 22 | + * @param {object} req Request obj |
| 23 | + * @param {object} res Response obj |
| 24 | + */ |
| 25 | +eg001ValidateWebhookMessage.createController = async (req, res) => { |
| 26 | + // Call the worker method |
| 27 | + const { body } = req; |
| 28 | + const args = { |
| 29 | + secret: validator.escape(body.secret), |
| 30 | + payload: body.payload, |
| 31 | + }; |
| 32 | + let results = null; |
| 33 | + |
| 34 | + try { |
| 35 | + results = computeHash(args); |
| 36 | + } catch (error) { |
| 37 | + const errorCode = error?.code; |
| 38 | + const errorMessage = error?.message; |
| 39 | + // In production, may want to provide customized error messages and |
| 40 | + // remediation advice to the user. |
| 41 | + res.render('pages/error', { err: error, errorCode, errorMessage }); |
| 42 | + } |
| 43 | + if (results) { |
| 44 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 45 | + res.render('pages/example_done', { |
| 46 | + title: example.ExampleName, |
| 47 | + message: formatString(example.ResultsPageText, results), |
| 48 | + }); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Form page for this application |
| 54 | + */ |
| 55 | +eg001ValidateWebhookMessage.getController = (req, res) => { |
| 56 | + const example = getExampleByNumber(res.locals.manifest, exampleNumber, api); |
| 57 | + const sourceFile = |
| 58 | + path.basename(__filename)[5].toLowerCase() + |
| 59 | + path.basename(__filename).substr(6); |
| 60 | + res.render('pages/connect-examples/eg001ValidateWebhookMessage', { |
| 61 | + eg: eg, |
| 62 | + csrfToken: req.csrfToken(), |
| 63 | + example: example, |
| 64 | + sourceFile: sourceFile, |
| 65 | + sourceUrl: dsConfig.githubExampleUrl + 'connect/examples/' + sourceFile, |
| 66 | + documentation: dsConfig.documentation + eg, |
| 67 | + showDoc: dsConfig.documentation, |
| 68 | + }); |
| 69 | +}; |
0 commit comments