Azure
Generate a SAS token
Generate a secure token for Azure Event Hubs REST APIs
After you’ve created a Shared Access Policy (SAP) you need to generate the token to use.
We need to generate these ourselves, but luckily, Azure provides some code for us. Below is a small node script using this code to make life easy.
index.js
const crypto = require("crypto")
// In seconds - week
const EXPIRES_IN = 60*60*24*7;
// In seconds - 100 years
// const EXPIRES_IN = 31536000 * 100;
function createSharedAccessToken(uri, saName, saKey) {
if (!uri || !saName || !saKey) {
throw "Missing required parameter";
}
var encoded = encodeURIComponent(uri);
var now = new Date();
var ttl = Math.round(now.getTime() / 1000) + EXPIRES_IN;
var signature = encoded + '\n' + ttl;
var hash = crypto.createHmac('sha256', saKey).update(signature, 'utf8').digest('base64');
return 'SharedAccessSignature sr=' + encoded + '&sig=' + encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName;
}
const uri = "" // Endpoint
const saName = "" // SAP Name
const saKey = "" // Primary or Secondary Key
console.log(
createSharedAccessToken(uri, saName, saKey)
);
You’ll need to fill in those 3 variables based on your project and then run. node index.js
That will spit out a value that you set the Authorization
header to whenever you send a event to your Queue/Topic/etc.