Mercurial > prosody-modules
comparison mod_unified_push/mod_unified_push.lua @ 5148:bf42f1401f1c
mod_unified_push: Refactor in anticipation of other registration backends
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 13 Jan 2023 16:50:43 +0000 |
parents | 658658ea9323 |
children | fa56ed2bacab |
comparison
equal
deleted
inserted
replaced
5147:658658ea9323 | 5148:bf42f1401f1c |
---|---|
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 id = require "util.id"; |
7 local jid = require "util.jid"; | |
7 local jwt = require "util.jwt"; | 8 local jwt = require "util.jwt"; |
8 local st = require "util.stanza"; | 9 local st = require "util.stanza"; |
9 local urlencode = require "util.http".urlencode; | 10 local urlencode = require "util.http".urlencode; |
10 | 11 |
11 local xmlns_up = "http://gultsch.de/xmpp/drafts/unified-push"; | 12 local xmlns_up = "http://gultsch.de/xmpp/drafts/unified-push"; |
52 return nil, "token-expired"; | 53 return nil, "token-expired"; |
53 end | 54 end |
54 return ok, result; | 55 return ok, result; |
55 end | 56 end |
56 | 57 |
58 local function register_route(params) | |
59 local expiry = os.time() + push_registration_ttl; | |
60 return { | |
61 url = module:http_url("push").."/"..urlencode(jwt_sign(unified_push_secret, { | |
62 instance = params.instance; | |
63 application = params.application; | |
64 sub = params.jid; | |
65 exp = expiry; | |
66 })); | |
67 expiry = expiry; | |
68 }; | |
69 end | |
70 | |
57 -- Handle incoming registration from XMPP client | 71 -- Handle incoming registration from XMPP client |
58 function handle_register(event) | 72 function handle_register(event) |
59 local origin, stanza = event.origin, event.stanza; | 73 local origin, stanza = event.origin, event.stanza; |
60 if not is_jid_permitted(stanza.attr.from) then | 74 if not is_jid_permitted(stanza.attr.from) then |
61 return st.error_reply(stanza, "auth", "forbidden"); | 75 return st.error_reply(stanza, "auth", "forbidden"); |
66 end | 80 end |
67 local application, application_err = check_sha256(stanza.tags[1].attr.application); | 81 local application, application_err = check_sha256(stanza.tags[1].attr.application); |
68 if not application then | 82 if not application then |
69 return st.error_reply(stanza, "modify", "bad-request", "application: "..application_err); | 83 return st.error_reply(stanza, "modify", "bad-request", "application: "..application_err); |
70 end | 84 end |
71 local expiry = os.time() + push_registration_ttl; | 85 local route = register_route({ |
72 local url = module:http_url("push").."/"..urlencode(jwt_sign({ | |
73 instance = instance; | 86 instance = instance; |
74 application = application; | 87 application = application; |
75 sub = stanza.attr.from; | 88 jid = stanza.attr.from; |
76 exp = expiry; | 89 }); |
77 })); | 90 |
91 if not route then | |
92 return st.error_reply(stanza, "wait", "internal-server-error"); | |
93 end | |
94 | |
78 module:log("debug", "New push registration successful"); | 95 module:log("debug", "New push registration successful"); |
79 return origin.send(st.reply(stanza):tag("registered", { | 96 return origin.send(st.reply(stanza):tag("registered", { |
80 expiration = datetime.datetime(expiry); | 97 expiration = datetime.datetime(route.expiry); |
81 endpoint = url; | 98 endpoint = route.url; |
82 xmlns = xmlns_up; | 99 xmlns = xmlns_up; |
83 })); | 100 })); |
84 end | 101 end |
85 | 102 |
86 module:hook("iq-set/host/"..xmlns_up..":register", handle_register); | 103 module:hook("iq-set/host/"..xmlns_up..":register", handle_register); |