2017-02-16 17 views
0

Я пишу код в Lua для создания элемента в хранилище таблиц Azure.Подпись для хранения таблиц Azure REST API

я попытался подписи Shared Key:

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) 

и Shared Key Lite:

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

Я также попытался бежать или не бежать строку:

local sts = ngx.escape_uri(sts) 

Я тогда Подпишите это:

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

затем отправить запрос:

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" 
      } 
}) 

я получаю эту ошибку:

{"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"}}} 

Почему? Спасибо.

ответ

0

Чтобы исправить это, вы можете попробовать установить дополнительный сырой флаг true, чтобы выход crypto.hmac.digest() функционального направления является прямым двоичным.

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