This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

curl to LUA
#9
(03.01.2022, 06:27)admin Wrote: It does not work because the standard HTTP library does not support digest authentication scheme. You need to use code from this example instead: https://forum.logicmachine.net/showthrea...64#pid9364

If i test with script below i get the same error...

Code:
local skthttp = require('socket.http') local skturl = require('socket.url') local ltn12 = require('ltn12') local md5sum = require('encdec').md5 local hash = function(...)   return md5sum(table.concat({...}, ':')) end local parse_header = function(header)   local result = {}   for key, value in (header .. ','):gmatch('(%w+)=(.-),') do     if value:sub(1, 1) == '"' then -- strip quotes       result[ key:lower() ] = value:sub(2, -2)     else       result[ key:lower() ] = value     end   end   return result end local make_digest_header = function(headers)   local digest = {}   for _, header in ipairs(headers) do     if not header.unquote then       header[ 2 ] = '"' .. header[ 2 ] .. '"'     end     digest[ #digest + 1 ] = header[ 1 ] .. '=' .. header[ 2 ]   end   return 'Digest ' .. table.concat(digest, ', ') end local _request = function(req)   if not req.url then     return nil, 'missing url'   end   local url = skturl.parse(req.url)   local user, password = url.user, url.password   local sink = req.sink   if not user or not password then     return nil, 'missing credentials in url'   end   url.user, url.password, url.authority, url.userinfo = nil, nil, nil, nil   req.url = skturl.build(url)   local source   if req.source then     local chunks = {}     local capture = function(chunk)       if chunk then         chunks[ #chunks + 1 ] = chunk       end       return chunk     end     local chunk_id = 0     source = function()       chunk_id = chunk_id + 1       return chunks[ chunk_id ]     end     req.source = ltn12.source.chain(req.source, capture)   end   req.sink = nil   local body, code, hdrs = skthttp.request(req)   if code == 401 and hdrs['www-authenticate'] then     local ht = parse_header(hdrs['www-authenticate'])     if not ht.realm or not ht.nonce then       return nil, 'missing realm/nonce from response'     end     local qop = ht.qop     if qop and qop ~= 'auth' then       return nil, 'unsupported qop ' .. tostring(qop)     end     if ht.algorithm and ht.algorithm:lower() ~= 'md5' then       return nil, 'unsupported algo ' .. tostring(ht.algorithm)     end     local nc = '00000001'     local cnonce = string.format('%08x', os.time())     local uri = skturl.build({ path = url.path, query = url.query })     local method = req.method or 'GET'     local response = hash(       hash(user, ht.realm, password),       ht.nonce,       nc,       cnonce,       'auth',       hash(method, uri)     )     req.headers = req.headers or {}     local auth = {       { 'username', user },       { 'realm', ht.realm },       { 'nonce', ht.nonce },       { 'uri', uri },       { 'cnonce', cnonce },       { 'nc', nc, unquote = true },       { 'qop', 'auth' },       { 'algorithm', 'MD5' },       { 'response', response },     }     if ht.opaque then       table.insert(auth, { 'opaque', ht.opaque })     end     req.headers.authorization = make_digest_header(auth)     if not req.headers.cookie and hdrs['set-cookie'] then       -- not really correct but enough for httpbin       local cookie = (hdrs['set-cookie'] .. ';'):match('(.-=.-)[;,]')       if cookie then         req.headers.cookie = '$Version: 0; ' .. cookie .. ';'       end     end     if req.source then       req.source = source     end     req.sink = sink     body, code, hdrs = skthttp.request(req)   end   return body, code, hdrs end local request = function(url)   local t = type(url)   if t == 'table' then     return _request(table.clone(url))   elseif t == 'string' then     local req = {}     local _, code, headers = _request({ url = url, sink = ltn12.sink.table(req) })     return table.concat(req), code, headers   end end url = 'http://admin:XXXX@192.168.0.1/ISAPI/VideoIntercom/callStatus?format=json' image, err, hdrs = request(url) require('json') data = json.pdecode(data) if type(data) == 'table' then   status = data.CallStatus.status   grp.checkwrite('32/5/1', data.CallStatus.status) else   alert('request failed doorbell') end
Reply


Messages In This Thread
curl to LUA - by KoBra - 29.12.2021, 10:41
RE: curl to LUA - by admin - 29.12.2021, 11:05
RE: curl to LUA - by KoBra - 29.12.2021, 15:18
RE: curl to LUA - by admin - 29.12.2021, 16:04
RE: curl to LUA - by KoBra - 01.01.2022, 20:30
RE: curl to LUA - by admin - 02.01.2022, 13:01
RE: curl to LUA - by KoBra - 02.01.2022, 14:05
RE: curl to LUA - by admin - 03.01.2022, 06:27
RE: curl to LUA - by KoBra - 03.01.2022, 14:26
RE: curl to LUA - by admin - 03.01.2022, 14:52
RE: curl to LUA - by KoBra - 03.01.2022, 20:43
RE: curl to LUA - by Krstfr2k - 03.03.2022, 07:08
RE: curl to LUA - by admin - 03.03.2022, 08:33
RE: curl to LUA - by Krstfr2k - 03.03.2022, 08:41
RE: curl to LUA - by admin - 03.03.2022, 09:04
RE: curl to LUA - by Krstfr2k - 04.03.2022, 12:26
RE: curl to LUA - by cekca - 27.01.2023, 13:45
RE: curl to LUA - by admin - 27.01.2023, 13:56

Forum Jump: