Comparison

mod_auth_http_async/mod_auth_http_async.lua @ 1927:439711709d29

mod_auth_http_async: Wrap up async http request in a function
author Kim Alvefur <zash@zash.se>
date Fri, 23 Oct 2015 16:52:45 +0200
parent 1749:39a0a35f02bc
child 1930:95bbf3c4aa27
comparison
equal deleted inserted replaced
1926:4c4a4191b825 1927:439711709d29
16 local host = module.host; 16 local host = module.host;
17 17
18 local api_base = module:get_option_string("http_auth_url", ""):gsub("$host", host); 18 local api_base = module:get_option_string("http_auth_url", ""):gsub("$host", host);
19 if api_base == "" then error("http_auth_url required") end 19 if api_base == "" then error("http_auth_url required") end
20 20
21 local function async_http_request(url, ex);
22 local wait, done = waiter();
23
24 local content, code, request, response;
25 local ret;
26 function cb(content_, code_, request_, response_)
27 content, code, request, response = content_, code_, request_, response_;
28 done();
29 end
30 wait();
31 return content, code, request, response;
32 end
33
21 local provider = {}; 34 local provider = {};
22 35
23 function provider.test_password(username, password) 36 function provider.test_password(username, password)
24 log("debug", "test password for user %s at host %s", username, host); 37 log("debug", "test password for user %s at host %s", username, host);
25 38
26 local wait, done = waiter();
27 39
28 local code = -1; 40 local _, code = async_http_request(api_base:gsub("$user", username), {
29 http.request(api_base:gsub("$user", username), { 41 headers = { Authorization = "Basic "..base64(username..":"..password); };
30 headers = { 42 });
31 Authorization = "Basic "..base64(username..":"..password);
32 };
33 },
34 function(body, _code)
35 code = _code;
36 done();
37 end);
38
39 wait();
40 43
41 if code >= 200 and code <= 299 then 44 if code >= 200 and code <= 299 then
42 return true; 45 return true;
43 else 46 else
44 module:log("debug", "HTTP auth provider returned status code %d", code); 47 module:log("debug", "HTTP auth provider returned status code %d", code);