Reputation: 713
Base on this Microsoft documentation: https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token
the code for nodejs is:
function createSharedAccessToken(uri, saName, saKey) {
if (!uri || !saName || !saKey) {
throw "Missing required parameter";
}
var encoded = encodeURIComponent(uri);
var now = new Date();
var week = 60*60*24*7;
var ttl = Math.round(now.getTime() / 1000) + week;
var signature = encoded + '\n' + ttl;
var signatureUTF8 = utf8.encode(signature);
var hash = crypto.createHmac('sha256', saKey).update(signatureUTF8).digest('base64');
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName;
}
And I was trying it on Postman, in case anyone need help with it. This is the code that works for me under the Pre-request Script using Postman
var namespace = pm.collectionVariables.get("serviceNamespace");
var uri = "https://" + namespace + ".servicebus.windows.net/adfTest/messages";
var saName = pm.collectionVariables.get("SharedAccessKeyName");
var saKey = pm.collectionVariables.get("SharedAccessKeySend");
var sasToken = createSharedAccessToken(uri, saName, saKey);
console.log(sasToken);
pm.collectionVariables.set("SasToken", sasToken);
function createSharedAccessToken(uri, saName, saKey) {
if (!uri || !saName || !saKey) {
throw "Missing required parameter";
}
var encoded = encodeURIComponent(uri).toLowerCase();
var now = new Date();
var week = 60*60*24*7;
var ttl = Math.round(now.getTime() / 1000) + week;
var signature = encoded + '\n' + ttl;
var hash = CryptoJS.HmacSHA256(signature, saKey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
encodeURIComponent(hashInBase64) + '&se=' + ttl + '&skn=' + saName;
}
Upvotes: 3
Views: 5450
Reputation: 713
Get the values from Azure portal - Service bus serviceNamespace, SharedAccessKey, SharedAccessKeyName
Add the code under Pre-request Script
var namespace = pm.collectionVariables.get("serviceNamespace");
var uri = "https://" + namespace + ".servicebus.windows.net/adfTest/messages";
var saName = pm.collectionVariables.get("SharedAccessKeyName");
var saKey = pm.collectionVariables.get("SharedAccessKeySend");
var sasToken = createSharedAccessToken(uri, saName, saKey);
console.log(sasToken);
pm.collectionVariables.set("SasToken", sasToken);
function createSharedAccessToken(uri, saName, saKey) {
if (!uri || !saName || !saKey) {
throw "Missing required parameter";
}
var encoded = encodeURIComponent(uri).toLowerCase();
var now = new Date();
var week = 60*60*24*7;
var ttl = Math.round(now.getTime() / 1000) + week;
var signature = encoded + '\n' + ttl;
var hash = CryptoJS.HmacSHA256(signature, saKey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
encodeURIComponent(hashInBase64) + '&se=' + ttl + '&skn=' + saName;
}
Note: the adfTest
part is the name of the topic, it should be replace by your topic name
var uri = "https://" + namespace + ".servicebus.windows.net/adfTest/messages";
Select API key from the dropdown menu.
Key = Authorization
Value = {{SasToken}} <-- this is the variable name
Add to = Header
These are the requirements for the post
POST https://<yournamespace>.servicebus.windows.net/<yourentity>/messages
Content-Type: application/json
Authorization: SharedAccessSignature sr=https%3A%2F%2F<yournamespace>.servicebus.windows.net%2F<yourentity>&sig=<yoursignature from code above>&se=1438205742&skn=KeyName
ContentType: application/atom+xml;type=entry;charset=utf-8
The URL should look like: https://.servicebus.windows.net//messages
under Authorization inherit auth from parent:
this is gonna add Authorization header with the SAS token generated by the pre-request Script under the collection
This could be any json data
Upvotes: 7