comparison mod_auth_http_async/mod_auth_http_async.lua @ 6318:07808fcda0e4

mod_auth_http_async: Remove non-async code (really should not be needed anymore)
author Kim Alvefur <zash@zash.se>
date Tue, 02 Sep 2025 22:19:51 +0200
parents 39156d6f7268
children
comparison
equal deleted inserted replaced
6317:7b693a5539ad 6318:07808fcda0e4
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 have_async, async = pcall(require, "util.async"); 12 local async = require "util.async";
13 13
14 local log = module._log; 14 local log = module._log;
15 local host = module.host; 15 local host = module.host;
16 16
17 local api_base = module:get_option_string("http_auth_url", ""):gsub("$host", host); 17 local api_base = module:get_option_string("http_auth_url", ""):gsub("$host", host);
18 if api_base == "" then error("http_auth_url required") end 18 if api_base == "" then error("http_auth_url required") end
19 19
20 local provider = {}; 20 local provider = {};
21
22 -- globals required by socket.http
23 if rawget(_G, "PROXY") == nil then
24 rawset(_G, "PROXY", false)
25 end
26 if rawget(_G, "base_parsed") == nil then
27 rawset(_G, "base_parsed", false)
28 end
29 if not have_async then -- FINE! Set your globals then
30 prosody.unlock_globals()
31 require "ltn12"
32 require "socket"
33 require "socket.http"
34 require "ssl.https"
35 prosody.lock_globals()
36 end
37 21
38 local function async_http_auth(url, username, password) 22 local function async_http_auth(url, username, password)
39 module:log("debug", "async_http_auth()"); 23 module:log("debug", "async_http_auth()");
40 local http = require "net.http"; 24 local http = require "net.http";
41 local wait, done = async.waiter(); 25 local wait, done = async.waiter();
56 module:log("debug", "HTTP auth provider returned status code %d", code); 40 module:log("debug", "HTTP auth provider returned status code %d", code);
57 end 41 end
58 return nil, "Auth failed. Invalid username or password."; 42 return nil, "Auth failed. Invalid username or password.";
59 end 43 end
60 44
61 local function sync_http_auth(url,username, password)
62 module:log("debug", "sync_http_auth()");
63 require "ltn12";
64 local http = require "socket.http";
65 local https = require "ssl.https";
66 local request;
67 if string.sub(url, 1, string.len('https')) == 'https' then
68 request = https.request;
69 else
70 request = http.request;
71 end
72 local _, code, headers, status = request{
73 url = url,
74 headers = { Authorization = "Basic "..base64(username..":"..password); }
75 };
76 if type(code) == "number" and code >= 200 and code <= 299 then
77 module:log("debug", "HTTP auth provider confirmed valid password");
78 return true;
79 else
80 module:log("debug", "HTTP auth provider returned status code: "..code);
81 end
82 return nil, "Auth failed. Invalid username or password.";
83 end
84
85 function provider.test_password(username, password) 45 function provider.test_password(username, password)
86 local url = api_base:gsub("$user", username):gsub("$password", password); 46 local url = api_base:gsub("$user", username):gsub("$password", password);
87 log("debug", "Testing password for user %s at host %s with URL %s", username, host, url); 47 log("debug", "Testing password for user %s at host %s with URL %s", username, host, url);
88 if (have_async) then 48 return async_http_auth(url, username, password);
89 return async_http_auth(url, username, password);
90 else
91 return sync_http_auth(url, username, password);
92 end
93 end 49 end
94 50
95 function provider.users() 51 function provider.users()
96 return function() 52 return function()
97 return nil; 53 return nil;