Mercurial > prosody-modules
changeset 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 (2016-04-15) |
parents | f24b02e0d706 |
children | 394a62163a91 |
files | mod_auth_http_async/mod_auth_http_async.lua |
diffstat | 1 files changed, 24 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/mod_auth_http_async/mod_auth_http_async.lua Sun Apr 03 17:09:03 2016 +0200 +++ b/mod_auth_http_async/mod_auth_http_async.lua Fri Apr 15 13:45:01 2016 +0000 @@ -9,7 +9,7 @@ local new_sasl = require "util.sasl".new; local base64 = require "util.encodings".base64.encode; -local waiter =require "util.async".waiter; +local have_async, async = pcall(require, "util.async"); local http = require "net.http"; local log = module._log; @@ -19,8 +19,7 @@ if api_base == "" then error("http_auth_url required") end local function async_http_request(url, ex) - local wait, done = waiter(); - + local wait, done = async.waiter(); local content, code, request, response; local function cb(content_, code_, request_, response_) content, code, request, response = content_, code_, request_, response_; @@ -35,18 +34,31 @@ function provider.test_password(username, password) log("debug", "test password for user %s at host %s", username, host); - - - local _, code = async_http_request(api_base:gsub("$user", username), { + local url = api_base:gsub("$user", username); + local ex = { headers = { Authorization = "Basic "..base64(username..":"..password); }; - }); - - if code >= 200 and code <= 299 then - return true; + } + if (have_async) then + local _, code = async_http_request(url, ex); + if code >= 200 and code <= 299 then + module:log("debug", "HTTP auth provider confirmed valid password"); + return true; + else + module:log("debug", "HTTP auth provider returned status code %d", code); + end else - module:log("debug", "HTTP auth provider returned status code %d", code); - return nil, "Auth failed. Invalid username or password."; + local ok, err = http.request(url, ex, function(body, code) + if code >= 200 and code <= 299 then + module:log("debug", "HTTP auth provider confirmed valid password"); + else + module:log("debug", "HTTP auth provider returned status code %d", code); + end + end); + if ok then + return true; + end end + return nil, "Auth failed. Invalid username or password."; end function provider.set_password(username, password) @@ -74,4 +86,3 @@ end module:provides("auth", provider); -