0
votes

I am writing some code in Lua to create an item in Azure Table Storage.

I have tried Shared Key signatures:

local account = 'XXX'
local key = 'YYY'
local table = 'test'
local date = os.date('!%a, %d %b %Y %H:%M:%S GMT', os.time)

local sts = "POST\n" ..
            "\n" ..  --Content-MD5
            "application/json\n" ..  --Content-Type
            args.date .. "\n" ..  --Date
            string.format("/%s/%s", account, table)

and Shared Key Lite:

local sts = string.format("%s\n/%s/%s", date, account, table)

I have also tried escaping or not escaping the string:

local sts = ngx.escape_uri(sts)

I then sign it:

local signature = ngx.encode_base64(crypto.hmac.digest("sha256", sts, ngx.decode_base64(args.key), false))

then send the request:

local url = string.format("https://%s.table.core.windows.net/%s", account, table)
local auth = string.format('SharedKey %s:%s', account, signature)
-- or local auth = string.format('SharedKeyLite %s:%s', account, signature)
local item = cjson.encode(item)

local httpc = http.new()
local res, err = httpc:request_uri(url, {
            method = "POST",
            data = item,
            headers = {
                ["Authorization"] = auth,
                ["x-ms-date"] = date,
                ["Accept"] = "application/json;odata=nometadata",
                ["x-ms-version"] = "2015-12-11",
                ["Content-Type"] = "application/json",
                ["Content-Length"] = #item,
                ["DataServiceVersion"] = "3.0;NetFx"
            }
})

I get this error:

{"odata.error":{"code":"AuthenticationFailed","message":{"lang":"en-US","value":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:AAA\nTime:2017-02-16T17:51:19.6107765Z"}}}

Why? Thank you.

1

1 Answers

0
votes

To fix this, you can try to set optional raw flag to true to let crypto.hmac.digest() function's output be a direct binary.

local signature = ngx.encode_base64(crypto.hmac.digest("sha256", sts, ngx.decode_base64(args.key), true))