annotate mod_http_auth_check/mod_http_auth_check.lua @ 5193:2bb29ece216b

mod_http_oauth2: Implement stateless dynamic client registration Replaces previous explicit registration that required either the additional module mod_adhoc_oauth2_client or manually editing the database. That method was enough to have something to test with, but would not probably not scale easily. Dynamic client registration allows creating clients on the fly, which may be even easier in theory. In order to not allow basically unauthenticated writes to the database, we implement a stateless model here. per_host_key := HMAC(config -> oauth2_registration_key, hostname) client_id := JWT { client metadata } signed with per_host_key client_secret := HMAC(per_host_key, client_id) This should ensure everything we need to know is part of the client_id, allowing redirects etc to be validated, and the client_secret can be validated with only the client_id and the per_host_key. A nonce injected into the client_id JWT should ensure nobody can submit the same client metadata and retrieve the same client_secret
author Kim Alvefur <zash@zash.se>
date Fri, 03 Mar 2023 21:14:19 +0100
parents 5ca6d53d3186
children
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 });