Mercurial > prosody-modules
comparison mod_unified_push/mod_unified_push.lua @ 5136:67b2c982bea2
mod_unified_push: Various fixes, now working with Conversations
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 10 Jan 2023 15:45:03 +0000 |
parents | 7cc0f68b8715 |
children | 449e4ca4de32 |
comparison
equal
deleted
inserted
replaced
5135:35085e0d52ad | 5136:67b2c982bea2 |
---|---|
1 local unified_push_secret = assert(module:get_option_string("unified_push_secret"), "required option: unified_push_secret"); | 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); | 2 local push_registration_ttl = module:get_option_number("unified_push_registration_ttl", 86400); |
3 | 3 |
4 local base64 = require "util.encodings".base64; | 4 local base64 = require "util.encodings".base64; |
5 local datetime = require "util.datetime"; | 5 local datetime = require "util.datetime"; |
6 local id = require "util.id"; | |
6 local jwt_sign, jwt_verify = require "util.jwt".init("HS256", unified_push_secret); | 7 local jwt_sign, jwt_verify = require "util.jwt".init("HS256", unified_push_secret); |
7 local st = require "util.stanza"; | 8 local st = require "util.stanza"; |
8 local urlencode = require "util.http".urlencode; | 9 local urlencode = require "util.http".urlencode; |
9 | 10 |
10 local xmlns_up = "http://gultsch.de/xmpp/drafts/unified-push"; | 11 local xmlns_up = "http://gultsch.de/xmpp/drafts/unified-push"; |
11 | 12 |
12 module:depends("http"); | 13 module:depends("http"); |
14 module:depends("disco"); | |
15 | |
16 module:add_feature(xmlns_up); | |
13 | 17 |
14 local function check_sha256(s) | 18 local function check_sha256(s) |
15 if not s then return nil, "no value provided"; end | 19 if not s then return nil, "no value provided"; end |
16 local d = base64.decode(s); | 20 local d = base64.decode(s); |
17 if not d then return nil, "invalid base64"; end | 21 if not d then return nil, "invalid base64"; end |
29 local application, application_err = check_sha256(stanza.tags[1].attr.application); | 33 local application, application_err = check_sha256(stanza.tags[1].attr.application); |
30 if not application then | 34 if not application then |
31 return st.error_reply(stanza, "modify", "bad-request", "application: "..application_err); | 35 return st.error_reply(stanza, "modify", "bad-request", "application: "..application_err); |
32 end | 36 end |
33 local expiry = os.time() + push_registration_ttl; | 37 local expiry = os.time() + push_registration_ttl; |
34 local url = module:http_url().."/"..urlencode(jwt_sign({ | 38 local url = module:http_url("push").."/"..urlencode(jwt_sign({ |
35 instance = instance; | 39 instance = instance; |
36 application = application; | 40 application = application; |
37 sub = stanza.attr.from; | 41 sub = stanza.attr.from; |
38 exp = expiry; | 42 exp = expiry; |
39 })); | 43 })); |
47 | 51 |
48 module:hook("iq-set/host/"..xmlns_up..":register", handle_register); | 52 module:hook("iq-set/host/"..xmlns_up..":register", handle_register); |
49 | 53 |
50 -- Handle incoming POST | 54 -- Handle incoming POST |
51 function handle_push(event, subpath) | 55 function handle_push(event, subpath) |
52 local data, err = jwt_verify(subpath); | 56 module:log("debug", "Incoming push received!"); |
53 if not data then | 57 local ok, data = jwt_verify(subpath); |
54 module:log("debug", "Received push to unacceptable token (%s)", err); | 58 if not ok then |
59 module:log("debug", "Received push to unacceptable token (%s)", data); | |
55 return 404; | 60 return 404; |
56 end | 61 end |
57 local payload = event.request.body; | 62 local payload = event.request.body; |
58 if not payload or payload == "" then | 63 if not payload or payload == "" then |
64 module:log("warn", "Missing or empty push payload"); | |
59 return 400; | 65 return 400; |
60 elseif #payload > 4096 then | 66 elseif #payload > 4096 then |
67 module:log("warn", "Push payload too large"); | |
61 return 413; | 68 return 413; |
62 end | 69 end |
63 local push_iq = st.iq({ type = "set", to = data.sub, id = event.request.id }) | 70 local push_id = event.request.id or id.short(); |
71 module:log("debug", "Push notification received [%s], relaying to device...", push_id); | |
72 local push_iq = st.iq({ type = "set", to = data.sub, from = module.host, id = push_id }) | |
64 :text_tag("push", base64.encode(payload), { instance = data.instance, application = data.application, xmlns = xmlns_up }); | 73 :text_tag("push", base64.encode(payload), { instance = data.instance, application = data.application, xmlns = xmlns_up }); |
65 return module:send_iq(push_iq):next(function () | 74 return module:send_iq(push_iq):next(function () |
75 module:log("debug", "Push notification delivered [%s]", push_id); | |
66 return 201; | 76 return 201; |
67 end, function (error_event) | 77 end, function (error_event) |
68 local e_type, e_cond, e_text = error_event.stanza:get_error(); | 78 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 | 79 if e_cond == "item-not-found" or e_cond == "feature-not-implemented" then |
70 module:log("debug", "Push rejected"); | 80 module:log("debug", "Push rejected [%s]", push_id); |
71 return 404; | 81 return 404; |
72 elseif e_cond == "service-unavailable" or e_cond == "recipient-unavailable" then | 82 elseif e_cond == "service-unavailable" or e_cond == "recipient-unavailable" then |
83 module:log("debug", "Recipient temporarily unavailable [%s]", push_id); | |
73 return 503; | 84 return 503; |
74 end | 85 end |
75 module:log("warn", "Unexpected push error response: %s/%s/%s", e_type, e_cond, e_text); | 86 module:log("warn", "Unexpected push error response: %s/%s/%s", e_type, e_cond, e_text); |
76 return 500; | 87 return 500; |
77 end); | 88 end); |