Annotate

mod_http_auth_check/mod_http_auth_check.lua @ 4941:e7b9bc629ecc

mod_rest: Add special handling to catch MAM results from remote hosts Makes MAM queries to remote hosts works. As the comment says, MAM results from users' local archives or local MUCs are returned via origin.send() which is provided in the event and thus already worked. Results from remote hosts go via normal stanza routing and events, which need this extra handling to catch. This pattern of iq-set, message+, iq-result is generally limited to MAM. Closest similar thing might be MUC join, but to really handle that you would need the webhook callback mechanism.
author Kim Alvefur <zash@zash.se>
date Mon, 16 May 2022 19:47:09 +0200
parent 2886: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
2886
5ca6d53d3186 Return 401 with correct realm when no user/pass is provided
Nicolas Cedilnik <nicoco@nicoco.fr>
parents: 2884
diff changeset
17 response.headers.www_authenticate = ("Basic realm=%q"):format(realm);
5ca6d53d3186 Return 401 with correct realm when no user/pass is provided
Nicolas Cedilnik <nicoco@nicoco.fr>
parents: 2884
diff changeset
18 return 401
2884
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
19 end
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
20 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
21 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
22 password = saslprep(password);
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
23 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
24 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
25 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
26 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
27 return 200
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
28 elseif err then
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
29 return 401
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 end
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
33
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
34 module:provides("http", {
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
35 route = {
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
36 GET = authenticate
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
37 };
16e9f37b3f82 mod_http_auth_check: New HTTP module to test user credentials
Nicolas Cedilnik <nicoco@nicoco.fr>
parents:
diff changeset
38 });