104 |
104 |
105 local backend = module:get_option_string("unified_push_backend", backends.paseto and "paseto" or "storage"); |
105 local backend = module:get_option_string("unified_push_backend", backends.paseto and "paseto" or "storage"); |
106 |
106 |
107 local function register_route(params) |
107 local function register_route(params) |
108 local expiry = os.time() + push_registration_ttl; |
108 local expiry = os.time() + push_registration_ttl; |
109 local token = backends[backend].sign({ |
109 local token, err = backends[backend].sign({ |
110 instance = params.instance; |
110 instance = params.instance; |
111 application = params.application; |
111 application = params.application; |
112 sub = params.jid; |
112 sub = params.jid; |
113 exp = expiry; |
113 exp = expiry; |
114 }); |
114 }); |
|
115 if not token then return nil, err; end |
115 return { |
116 return { |
116 url = module:http_url("push").."/"..urlencode(token); |
117 url = module:http_url("push").."/"..urlencode(token); |
117 expiry = expiry; |
118 expiry = expiry; |
118 }; |
119 }; |
119 end |
120 end |
120 |
121 |
121 -- Handle incoming registration from XMPP client |
122 -- Handle incoming registration from XMPP client |
122 function handle_register(event) |
123 function handle_register(event) |
|
124 module:log("debug", "Push registration request received"); |
123 local origin, stanza = event.origin, event.stanza; |
125 local origin, stanza = event.origin, event.stanza; |
124 if not is_jid_permitted(stanza.attr.from) then |
126 if not is_jid_permitted(stanza.attr.from) then |
125 return st.error_reply(stanza, "auth", "forbidden"); |
127 module:log("debug", "Sender <%s> not permitted to register on this UnifiedPush service", stanza.attr.from); |
|
128 return origin.send(st.error_reply(stanza, "auth", "forbidden")); |
126 end |
129 end |
127 local instance, instance_err = check_sha256(stanza.tags[1].attr.instance); |
130 local instance, instance_err = check_sha256(stanza.tags[1].attr.instance); |
128 if not instance then |
131 if not instance then |
129 return st.error_reply(stanza, "modify", "bad-request", "instance: "..instance_err); |
132 return origin.send(st.error_reply(stanza, "modify", "bad-request", "instance: "..instance_err)); |
130 end |
133 end |
131 local application, application_err = check_sha256(stanza.tags[1].attr.application); |
134 local application, application_err = check_sha256(stanza.tags[1].attr.application); |
132 if not application then |
135 if not application then |
133 return st.error_reply(stanza, "modify", "bad-request", "application: "..application_err); |
136 return origin.send(st.error_reply(stanza, "modify", "bad-request", "application: "..application_err)); |
134 end |
137 end |
135 local route = register_route({ |
138 |
|
139 local route, register_err = register_route({ |
136 instance = instance; |
140 instance = instance; |
137 application = application; |
141 application = application; |
138 jid = stanza.attr.from; |
142 jid = stanza.attr.from; |
139 }); |
143 }); |
140 |
144 |
141 if not route then |
145 if not route then |
142 return st.error_reply(stanza, "wait", "internal-server-error"); |
146 module:log("warn", "Failed to create registration using %s backend: %s", backend, register_err); |
|
147 return origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
143 end |
148 end |
144 |
149 |
145 module:log("debug", "New push registration successful"); |
150 module:log("debug", "New push registration successful"); |
146 return origin.send(st.reply(stanza):tag("registered", { |
151 return origin.send(st.reply(stanza):tag("registered", { |
147 expiration = datetime.datetime(route.expiry); |
152 expiration = datetime.datetime(route.expiry); |