Mercurial > prosody-modules
comparison mod_auth_http_async/mod_auth_http_async.lua @ 2159:5e8dec076afc
mod_auth_http_async: Fall back to non-async calling of http_auth_url
if util.async is not available
author | JC Brand <jcbrand@minddistrict.com> |
---|---|
date | Fri, 15 Apr 2016 13:45:01 +0000 |
parents | 54f9e8663139 |
children | b2a198665946 |
comparison
equal
deleted
inserted
replaced
2155:f24b02e0d706 | 2159:5e8dec076afc |
---|---|
7 -- COPYING file in the source package for more information. | 7 -- COPYING file in the source package for more information. |
8 -- | 8 -- |
9 | 9 |
10 local new_sasl = require "util.sasl".new; | 10 local new_sasl = require "util.sasl".new; |
11 local base64 = require "util.encodings".base64.encode; | 11 local base64 = require "util.encodings".base64.encode; |
12 local waiter =require "util.async".waiter; | 12 local have_async, async = pcall(require, "util.async"); |
13 local http = require "net.http"; | 13 local http = require "net.http"; |
14 | 14 |
15 local log = module._log; | 15 local log = module._log; |
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) | 21 local function async_http_request(url, ex) |
22 local wait, done = waiter(); | 22 local wait, done = async.waiter(); |
23 | |
24 local content, code, request, response; | 23 local content, code, request, response; |
25 local function cb(content_, code_, request_, response_) | 24 local function cb(content_, code_, request_, response_) |
26 content, code, request, response = content_, code_, request_, response_; | 25 content, code, request, response = content_, code_, request_, response_; |
27 done(); | 26 done(); |
28 end | 27 end |
33 | 32 |
34 local provider = {}; | 33 local provider = {}; |
35 | 34 |
36 function provider.test_password(username, password) | 35 function provider.test_password(username, password) |
37 log("debug", "test password for user %s at host %s", username, host); | 36 log("debug", "test password for user %s at host %s", username, host); |
38 | 37 local url = api_base:gsub("$user", username); |
39 | 38 local ex = { |
40 local _, code = async_http_request(api_base:gsub("$user", username), { | |
41 headers = { Authorization = "Basic "..base64(username..":"..password); }; | 39 headers = { Authorization = "Basic "..base64(username..":"..password); }; |
42 }); | 40 } |
43 | 41 if (have_async) then |
44 if code >= 200 and code <= 299 then | 42 local _, code = async_http_request(url, ex); |
45 return true; | 43 if code >= 200 and code <= 299 then |
44 module:log("debug", "HTTP auth provider confirmed valid password"); | |
45 return true; | |
46 else | |
47 module:log("debug", "HTTP auth provider returned status code %d", code); | |
48 end | |
46 else | 49 else |
47 module:log("debug", "HTTP auth provider returned status code %d", code); | 50 local ok, err = http.request(url, ex, function(body, code) |
48 return nil, "Auth failed. Invalid username or password."; | 51 if code >= 200 and code <= 299 then |
52 module:log("debug", "HTTP auth provider confirmed valid password"); | |
53 else | |
54 module:log("debug", "HTTP auth provider returned status code %d", code); | |
55 end | |
56 end); | |
57 if ok then | |
58 return true; | |
59 end | |
49 end | 60 end |
61 return nil, "Auth failed. Invalid username or password."; | |
50 end | 62 end |
51 | 63 |
52 function provider.set_password(username, password) | 64 function provider.set_password(username, password) |
53 return nil, "Changing passwords not supported"; | 65 return nil, "Changing passwords not supported"; |
54 end | 66 end |
72 end | 84 end |
73 }); | 85 }); |
74 end | 86 end |
75 | 87 |
76 module:provides("auth", provider); | 88 module:provides("auth", provider); |
77 |