Skip to content

Instantly share code, notes, and snippets.

@bdeanindy
Last active February 4, 2021 17:13
Show Gist options
  • Save bdeanindy/ed711a1e573eacbcb9c231242f0d53ad to your computer and use it in GitHub Desktop.
Save bdeanindy/ed711a1e573eacbcb9c231242f0d53ad to your computer and use it in GitHub Desktop.
Programming in Node.js and using Zoom Web SDK? Here is an example utility module to generate the signature as required, and documented here (in PHP): https://zoom.github.io/zoom-sdk-web/global.html#generate_signature
'use strict';
// Determine if crypto support is available
let crypto;
try {
crypto = require('crypto');
} catch (err) {
console.error('crypto support is unavailable');
throw err;
}
// NOTE: Best practice is to load the Zoom API Keys from environment files, not hardcoded
const APP_KEY = process.env.ZOOM_APP_KEY;
const APP_SECRET = process.env.ZOOM_APP_SECRET;
// Define this module
let ZoomUtil = {};
ZoomUtil.rtrim = (targetString, characterList) {
// NOTE: This method was borrowed from here (I have not tested it)...
// discuss at: http://locutus.io/php/rtrim/
// original by: Kevin van Zonneveld (http://kvz.io)
// input by: Erkekjetter
// input by: rem
// improved by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// example 1: rtrim(' Kevin van Zonneveld ')
// returns 1: ' Kevin van Zonneveld'
charlist = !charlist ? ' \\s\u00A0' : (charlist + '')
.replace(/([[\]().?/*{}+$^:])/g, '\\$1')
var re = new RegExp('[' + charlist + ']+$', 'g')
return (str + '').replace(re, '')
}
};
ZoomUtil.generateHash = function (string) {
// USING HMAC
let hmac = crypto.createHmac('sha256', new Buffer(APP_SECRET, 'utf8'));
hmac.update(string);
let signature = hmac.digest('hex');
// Base64 encoded the hash
let tmpSig = new Buffer(signature).toString('base64');
// NOTE: Untested code below, just following what the PHP example code (may not be necessary)
// replacing all instances of `+/` within tmpSig to `-_` and then trimming all whitespace and
// hyphens from the end of the string
let regex = /\+\//;
return this.rtrim(tmpSig.replace(regex, `-_`), '='); // According to the PHP example, this should now be a URL safe base64 encoded signature.
};
ZoomUtil.generateSignatureString = function (meetingNumber = 0, role = 1) {
/**
NOTE: there are constants which are expected to be loaded from an environment file (a best practice for any Zoom API Keys)
// Uncomment to debug if needed
console.log('Inside buildRequestHash()');
console.log('meetingNumber: ', meetingNumber);
console.log('role: ', role);
**/
let errorMsgs = [];
// Ensure data is provided as needed by Zoom Web SDK and related Zoom API.
// NOTE: This logic check could be greatly improved, but it should work for demonstration purposes
if(0 === meetingNumber) {
errorMsgs.push('Invalid argument, `meetingNumber` is required. More info: https://zoom.github.io/zoom-sdk-web/global.html#generate_signature');
}
let time = +new Date(); // Unix timestamp (milliseconds since epoch
// NOTE: Possible values for `role`: 0 is for participant, 1 is for host
// Construct the content string required by generateHash()
let content = new Buffer(`${appKey}${meetingNumber}${time}${role}`).toString('base64');
if(0 !== errorMsgs.length) {
console.error(errorMsgs);
}
//console.log('RAW CONTENT: ', content);
return this.generateHash(content);
};
module.exports = ZoomUtil;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment