locked
Send message to queue gives the "40103: Invalid authorization token signature error" RRS feed

  • Question

  • I am new to the azure

    I am trying to send a message to the service bus queue uisng javascript.But i am getting the below error

    (40103: Invalid authorization token signature)

        <script type="text/javascript">
            var m_ServiceNamespace = "MyQueue-ns";
           
            var m_SasKey = "tDkpA8LQ5OPoh/1fcWcBaVQZ6ZSByVxyasPsr+J4D4k="; //paste here key1     
            var m_SasKeyName = "RootManageSharedAccessKey";
            var environment = "servicebus.Windows.net";
            $(document).ready(function () {
                $("#btnSend").click(function () {
                    alert("button");
                    var txtMsg = "link"
                    var msg = { "message": txtMsg, "id": 1234 };
                    var queue = "myqueue";
                    SB.sendMessage(queue, JSON.stringify(msg),
                      "application/json", function (messagingResult) {
                          var res = messagingResult;
                      });
                });
            });
            var getToken = function (entityPath) {

                var uri = "http://" + m_ServiceNamespace +
                ".servicebus.windows.net/" + entityPath;

                var endocedResourceUri = encodeURIComponent(uri);

                var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
                var t1 = new Date();
                var expireInSeconds = +(31 * 24 * 3600) + 3600 +
               (((t1.getTime() - t0.getTime()) / 1000) | 0);

                var plainSignature = escape(endocedResourceUri +"\n" + expireInSeconds)

                var hash = CryptoJS.HmacSHA256(plainSignature, m_SasKey);
                var base64HashValue = CryptoJS.enc.Base64.stringify(hash);

                var token = "SharedAccessSignature sr=" + endocedResourceUri + "&sig=" +
                encodeURIComponent(base64HashValue) + "&se=" + expireInSeconds + "&skn=" +
                m_SasKeyName;

                return token;
            }
            var utf8Encode = function (s) {
                for (var c, i = -1, l = (s = s.split("")).length,
                    o = String.fromCharCode; ++i < l;
                    s = (c = s.charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) +
                    o(0x80 | (c & 0x3f)) : s
                );
                return s.join("");
            }
            var SB = {
               sendMessage: function (entityPath, body, contentType, callback) {
                   var securityToken = getToken(entityPath);
                   console.log(securityToken);
                    var entityUri = "https://" + m_ServiceNamespace + "." +
                    environment + "/" + entityPath;
                    var sendUri = entityUri + "/messages";
                    var xmlHttpRequest = new XMLHttpRequest();

                    xmlHttpRequest.open("POST", sendUri, true);
                    xmlHttpRequest.setRequestHeader('Content-Type', contentType);
                    xmlHttpRequest.setRequestHeader("Authorization", securityToken);
                    xmlHttpRequest.onreadystatechange = function () {

                        if (this.readyState == 4) {

                            var messagingResult;

                            if (this.status == 201) {
                                messagingResult = new MessagingResult("Success",
                                this.status, null, this.response);
                            }
                            else {
                                //messagingResult = new MessagingResult("Failure",
                                //this.status, null, this.response);
                                console.log(this.status +"Message" +this.response);
                            }

                            if (callback != null)
                                callback(messagingResult);
                        }
                    };

                    xmlHttpRequest.send(body);
                }
                     }
        </script>


    • Edited by sk2014 Wednesday, May 25, 2016 10:33 AM
    Tuesday, May 24, 2016 1:28 PM

Answers

  • solved  the error.Problem is with default value of uri is taking as upper case letters.so i changed to lower case.

    Issue is resolved.

    This is my updated code

    var getToken = function (queueName) {
        var uri = "http://" + serviceNamespace + environment + "/" + queueName;
        var endocedResourceUri = encodeURIComponent(uri.toLowerCase());
        var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
        var t1 = new Date();
        var expireInSeconds = +(31 * 24 * 3600) + 3600 +
       (((t1.getTime() - t0.getTime()) / 1000) | 0);
        var plainSignature = endocedResourceUri.toLowerCase() + "\n" + expireInSeconds;
        var hash = CryptoJS.HmacSHA256(plainSignature, sasKey);
        var base64HashValue = CryptoJS.enc.Base64.stringify(hash);
        var str = encodeURIComponent(base64HashValue);
        str = str.replace("%3D", "%3d");
        var token = "SharedAccessSignature sr=" + endocedResourceUri.toLowerCase() + "&sig=" +
        str + "&se=" + expireInSeconds + "&skn=" +
        sasKeyName;

        return token;
    }

    Hope it helps for others, who are looking for javascript solution.


    • Marked as answer by sk2014 Wednesday, May 25, 2016 10:37 AM
    • Edited by sk2014 Wednesday, May 25, 2016 10:37 AM
    Wednesday, May 25, 2016 10:37 AM

All replies

  • I suspect an issue with the token generator but  I am not able to identify that. Can you replace your token generator with the SDK provided one below?

    https://github.com/ddobric/ServiceBusJavaScriptSdk/blob/master/ServiceBusJS/Scripts/servicebusjssdk-1.2.js

    Tuesday, May 24, 2016 9:53 PM
  • solved  the error.Problem is with default value of uri is taking as upper case letters.so i changed to lower case.

    Issue is resolved.

    This is my updated code

    var getToken = function (queueName) {
        var uri = "http://" + serviceNamespace + environment + "/" + queueName;
        var endocedResourceUri = encodeURIComponent(uri.toLowerCase());
        var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
        var t1 = new Date();
        var expireInSeconds = +(31 * 24 * 3600) + 3600 +
       (((t1.getTime() - t0.getTime()) / 1000) | 0);
        var plainSignature = endocedResourceUri.toLowerCase() + "\n" + expireInSeconds;
        var hash = CryptoJS.HmacSHA256(plainSignature, sasKey);
        var base64HashValue = CryptoJS.enc.Base64.stringify(hash);
        var str = encodeURIComponent(base64HashValue);
        str = str.replace("%3D", "%3d");
        var token = "SharedAccessSignature sr=" + endocedResourceUri.toLowerCase() + "&sig=" +
        str + "&se=" + expireInSeconds + "&skn=" +
        sasKeyName;

        return token;
    }

    Hope it helps for others, who are looking for javascript solution.


    • Marked as answer by sk2014 Wednesday, May 25, 2016 10:37 AM
    • Edited by sk2014 Wednesday, May 25, 2016 10:37 AM
    Wednesday, May 25, 2016 10:37 AM