Cristina Carrasco
Cristina Carrasco

Reputation: 713

How to create SAS token in Postman to use with Service bus?

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

Answers (1)

Cristina Carrasco
Cristina Carrasco

Reputation: 713

  1. Under you collection create the variables: enter image description here

Get the values from Azure portal - Service bus serviceNamespace, SharedAccessKey, SharedAccessKeyName

  1. Add the code under Pre-request Script enter image description here

    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";
  • Under Authorization use the sasToken variable: enter image description here

  • Select API key from the dropdown menu.

  • Key = Authorization

  • Value = {{SasToken}} <-- this is the variable name

  • Add to = Header

  1. Create a post: enter image description here

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: enter image description here

this is gonna add Authorization header with the SAS token generated by the pre-request Script under the collection

Add the other headers: enter image description here

  1. Put the message in the body: enter image description here

This could be any json data

Upvotes: 7

Related Questions