Comparison

mod_auth_http_async/mod_auth_http_async.lua @ 2750:1d139e33c502

mod_auth_http_async: Updated sync_http_auth function to accept username and password and send those as a basic authentication header
author Matt Loupe <mloupe2@gmail.com>
date Wed, 23 Aug 2017 14:31:55 -0500
parent 2630:96eb1c4f9ff7
child 2811:39156d6f7268
comparison
equal deleted inserted replaced
2749:9756211fcbe3 2750:1d139e33c502
26 if rawget(_G, "base_parsed") == nil then 26 if rawget(_G, "base_parsed") == nil then
27 rawset(_G, "base_parsed", false) 27 rawset(_G, "base_parsed", false)
28 end 28 end
29 29
30 local function async_http_auth(url, username, password) 30 local function async_http_auth(url, username, password)
31 module:log("debug", "async_http_auth()");
31 local http = require "net.http"; 32 local http = require "net.http";
32 local wait, done = async.waiter(); 33 local wait, done = async.waiter();
33 local content, code, request, response; 34 local content, code, request, response;
34 local ex = { 35 local ex = {
35 headers = { Authorization = "Basic "..base64(username..":"..password); }; 36 headers = { Authorization = "Basic "..base64(username..":"..password); };
47 module:log("debug", "HTTP auth provider returned status code %d", code); 48 module:log("debug", "HTTP auth provider returned status code %d", code);
48 end 49 end
49 return nil, "Auth failed. Invalid username or password."; 50 return nil, "Auth failed. Invalid username or password.";
50 end 51 end
51 52
52 local function sync_http_auth(url) 53 local function sync_http_auth(url,username, password)
54 module:log("debug", "sync_http_auth()");
53 local http = require "socket.http"; 55 local http = require "socket.http";
54 local https = require "ssl.https"; 56 local https = require "ssl.https";
55 local request; 57 local request;
56 if string.sub(url, 1, string.len('https')) == 'https' then 58 if string.sub(url, 1, string.len('https')) == 'https' then
57 request = https.request; 59 request = https.request;
58 else 60 else
59 request = http.request; 61 request = http.request;
60 end 62 end
61 local _, code, headers, status = request{ 63 local _, code, headers, status = request{
62 url = url, 64 url = url,
63 headers = { ACCEPT = "application/json, text/plain, */*"; } 65 headers = { Authorization = "Basic "..base64(username..":"..password); }
64 }; 66 };
65 if type(code) == "number" and code >= 200 and code <= 299 then 67 if type(code) == "number" and code >= 200 and code <= 299 then
66 module:log("debug", "HTTP auth provider confirmed valid password"); 68 module:log("debug", "HTTP auth provider confirmed valid password");
67 return true; 69 return true;
68 else 70 else
75 local url = api_base:gsub("$user", username):gsub("$password", password); 77 local url = api_base:gsub("$user", username):gsub("$password", password);
76 log("debug", "Testing password for user %s at host %s with URL %s", username, host, url); 78 log("debug", "Testing password for user %s at host %s with URL %s", username, host, url);
77 if (have_async) then 79 if (have_async) then
78 return async_http_auth(url, username, password); 80 return async_http_auth(url, username, password);
79 else 81 else
80 return sync_http_auth(url); 82 return sync_http_auth(url, username, password);
81 end 83 end
82 end 84 end
83 85
84 function provider.users() 86 function provider.users()
85 return function() 87 return function()