Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - Authorization of Azure Storage service REST API

I've been stuck for the whole day making my first call of Azure Storage REST API. Postman's response showed that it's due to error in Azure authentication, but I have no idea what's the problem.

Here is the browser script to send Azure Storage REST API:

function azureListContainers() {

var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET



x-ms-date:' + strTime + '
x-ms-version:2015-12-11
/myaccount/?comp=list';

var hash = CryptoJS.HmacSHA256(strToSign, key);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKeyLite myaccount:"+hashInBase64;

console.log(strToSign);
console.log(auth);
console.log(strTime);

$.ajax({
    type: "GET",
    beforeSend: function (request)
    {
        request.setRequestHeader("Authorization", auth);
        request.setRequestHeader("x-ms-date", strTime);
        request.setRequestHeader("x-ms-version", "2015-12-11");
    },
    url: "https://myaccount.blob.core.windows.net/?comp=list",
    processData: false,
    success: function(msg) {
        console.log(msg);
    }
});
}

Chrome Developer Tool just returned No 'Access-Control-Allow-Origin' header without further reason, so I copied the contents of var auth and var strTime, created the same request using Postman tool:

[Command]
GET https://myaccount.blob.core.windows.net/?comp=list


[Headers]
Authorization:SharedKeyLite myaccount:Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11


[Response Body]
<?xml version="1.0" encoding="utf-8"?>
<Error>
    <Code>AuthenticationFailed</Code>
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:9be3d595-0001-0012-4929-f2fde2000000
Time:2016-08-09T10:31:52.6542965Z</Message>
    <AuthenticationErrorDetail>The MAC signature found in the HTTP request 'Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=' is not the same as any computed signature. Server used following string to sign: 'GET



x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
/myaccount/?comp=list'.</AuthenticationErrorDetail>
</Error>

After diff the two strings, I believe var strToSign in my script is the same as the string Azure used to sign. But still there was an authentication error. Please help indicate what's the problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Chrome Developer Tool just returned No 'Access-Control-Allow-Origin' header

As you are using javascript to send http requests against to Azure Storage Server directly from client. It's a common CORS issue.

When you occur this issue, you can enable the CORS for your storage services. For simple, you can leverage Microsoft Azure Storage Explorer to configure your storage.

enter image description here

AuthenticationFailed

I did two modifications based on your code snippet, in my test project to make it work.

  1. var hash = CryptoJS.HmacSHA256(strToSign, key); The second parameter, should be a base64 decode from the account key, refer to the Azure Storage SDK for node.js
  2. In my test, I had to use SharedKey scheme and authenticate with SharedKey token to make your scenario to work.

Here is my test code snippet, FYI.,

var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET











x-ms-date:' + strTime + '
x-ms-version:2015-12-11
/<accountname>/
comp:list';
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKey <accountname>:"+hashInBase64; 

Additionally, for security reason, please implement the Azure Storage Services in backend web server. As using pure javascript on client, will expose your account name and account key to public, which will raise the risk to your data and info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...