comparison mod_http_auth_check/mod_http_auth_check.lua @ 2884:16e9f37b3f82

mod_http_auth_check: New HTTP module to test user credentials
author Nicolas Cedilnik <nicoco@nicoco.fr>
date Tue, 20 Feb 2018 12:57:44 +0000
parents
children 5ca6d53d3186
comparison
equal deleted inserted replaced
2883:7c16afc70d11 2884:16e9f37b3f82
1 -- HTTP Is User Valid
2 -- By Nicolas Cedilnik <nicoco@nicoco.fr>
3
4 local jid_prep = require "util.jid".prep;
5 local jid_split = require "util.jid".split;
6 local test_password = require "core.usermanager".test_password;
7 local b64_decode = require "util.encodings".base64.decode;
8 local saslprep = require "util.encodings".stringprep.saslprep;
9 local realm = module:get_host() .. "/" .. module:get_name();
10 module:depends"http";
11
12 local function authenticate (event, path)
13 local request = event.request;
14 local response = event.response;
15 local headers = request.headers;
16 if not headers.authorization then
17 return 400
18 end
19 local from_jid, password = b64_decode(headers.authorization:match"[^ ]*$"):match"([^:]*):(.*)";
20 from_jid = jid_prep(from_jid);
21 password = saslprep(password);
22 if from_jid and password then
23 local user, host = jid_split(from_jid);
24 local ok, err = test_password(user, host, password);
25 if ok and user and host then
26 return 200
27 elseif err then
28 return 401
29 end
30 end
31 end
32
33 module:provides("http", {
34 route = {
35 GET = authenticate
36 };
37 });