annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2884
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
1 -- HTTP Is User Valid
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
2 -- By Nicolas Cedilnik <nicoco@nicoco.fr>
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
3
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
4 local jid_prep = require "util.jid".prep;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
5 local jid_split = require "util.jid".split;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
6 local test_password = require "core.usermanager".test_password;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
7 local b64_decode = require "util.encodings".base64.decode;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
8 local saslprep = require "util.encodings".stringprep.saslprep;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
9 local realm = module:get_host() .. "/" .. module:get_name();
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
10 module:depends"http";
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
11
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
12 local function authenticate (event, path)
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
13 local request = event.request;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
14 local response = event.response;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
15 local headers = request.headers;
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
16 if not headers.authorization then
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
17 return 400
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
18 end
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
19 local from_jid, password = b64_decode(headers.authorization:match"[^ ]*$"):match"([^:]*):(.*)";
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
20 from_jid = jid_prep(from_jid);
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
21 password = saslprep(password);
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
22 if from_jid and password then
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
23 local user, host = jid_split(from_jid);
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
24 local ok, err = test_password(user, host, password);
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
25 if ok and user and host then
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
26 return 200
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
27 elseif err then
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
28 return 401
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
29 end
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
30 end
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
31 end
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
32
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
33 module:provides("http", {
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
34 route = {
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
35 GET = authenticate
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
36 };
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
37 });