comparison mod_unified_push/mod_unified_push.lua @ 5128:7cc0f68b8715

mod_unified_push: Experimenal Unified Push provider
author Matthew Wild <mwild1@gmail.com>
date Thu, 05 Jan 2023 17:28:06 +0000
parents
children 67b2c982bea2
comparison
equal deleted inserted replaced
5127:be859bfdd44e 5128:7cc0f68b8715
1 local unified_push_secret = assert(module:get_option_string("unified_push_secret"), "required option: unified_push_secret");
2 local push_registration_ttl = module:get_option_number("unified_push_registration_ttl", 86400);
3
4 local base64 = require "util.encodings".base64;
5 local datetime = require "util.datetime";
6 local jwt_sign, jwt_verify = require "util.jwt".init("HS256", unified_push_secret);
7 local st = require "util.stanza";
8 local urlencode = require "util.http".urlencode;
9
10 local xmlns_up = "http://gultsch.de/xmpp/drafts/unified-push";
11
12 module:depends("http");
13
14 local function check_sha256(s)
15 if not s then return nil, "no value provided"; end
16 local d = base64.decode(s);
17 if not d then return nil, "invalid base64"; end
18 if #d ~= 32 then return nil, "incorrect decoded length, expected 32"; end
19 return s;
20 end
21
22 -- Handle incoming registration from XMPP client
23 function handle_register(event)
24 local origin, stanza = event.origin, event.stanza;
25 local instance, instance_err = check_sha256(stanza.tags[1].attr.instance);
26 if not instance then
27 return st.error_reply(stanza, "modify", "bad-request", "instance: "..instance_err);
28 end
29 local application, application_err = check_sha256(stanza.tags[1].attr.application);
30 if not application then
31 return st.error_reply(stanza, "modify", "bad-request", "application: "..application_err);
32 end
33 local expiry = os.time() + push_registration_ttl;
34 local url = module:http_url().."/"..urlencode(jwt_sign({
35 instance = instance;
36 application = application;
37 sub = stanza.attr.from;
38 exp = expiry;
39 }));
40 module:log("debug", "New push registration successful");
41 return origin.send(st.reply(stanza):tag("registered", {
42 expiration = datetime.datetime(expiry);
43 endpoint = url;
44 xmlns = xmlns_up;
45 }));
46 end
47
48 module:hook("iq-set/host/"..xmlns_up..":register", handle_register);
49
50 -- Handle incoming POST
51 function handle_push(event, subpath)
52 local data, err = jwt_verify(subpath);
53 if not data then
54 module:log("debug", "Received push to unacceptable token (%s)", err);
55 return 404;
56 end
57 local payload = event.request.body;
58 if not payload or payload == "" then
59 return 400;
60 elseif #payload > 4096 then
61 return 413;
62 end
63 local push_iq = st.iq({ type = "set", to = data.sub, id = event.request.id })
64 :text_tag("push", base64.encode(payload), { instance = data.instance, application = data.application, xmlns = xmlns_up });
65 return module:send_iq(push_iq):next(function ()
66 return 201;
67 end, function (error_event)
68 local e_type, e_cond, e_text = error_event.stanza:get_error();
69 if e_cond == "item-not-found" or e_cond == "feature-not-implemented" then
70 module:log("debug", "Push rejected");
71 return 404;
72 elseif e_cond == "service-unavailable" or e_cond == "recipient-unavailable" then
73 return 503;
74 end
75 module:log("warn", "Unexpected push error response: %s/%s/%s", e_type, e_cond, e_text);
76 return 500;
77 end);
78 end
79
80 module:provides("http", {
81 name = "push";
82 route = {
83 ["GET /*"] = function (event)
84 event.response.headers.content_type = "application/json";
85 return [[{"unifiedpush":{"version":1}}]];
86 end;
87 ["POST /*"] = handle_push;
88 };
89 });