mod_unified_push: Remove dependency on trunk util.jwt (0.12 compat)
authorMatthew Wild <mwild1@gmail.com>
Tue, 10 Jan 2023 16:34:21 +0000
changeset 5143 449e4ca4de32
parent 5142 4511e90d1d08
child 5144 ce11c217704a
mod_unified_push: Remove dependency on trunk util.jwt (0.12 compat) This should allow the module to work on 0.12, while preserving expiry checking (which was not built in to 0.12's util.jwt).
mod_unified_push/README.md
mod_unified_push/mod_unified_push.lua
--- a/mod_unified_push/README.md	Tue Jan 10 16:07:00 2023 +0000
+++ b/mod_unified_push/README.md	Tue Jan 10 16:34:21 2023 +0000
@@ -56,4 +56,5 @@
 
 ## Compatibility
 
-Requires Prosody trunk (not compatible with 0.12).
+| trunk | Works |
+| 0.12  | Should work |
--- a/mod_unified_push/mod_unified_push.lua	Tue Jan 10 16:07:00 2023 +0000
+++ b/mod_unified_push/mod_unified_push.lua	Tue Jan 10 16:34:21 2023 +0000
@@ -4,7 +4,7 @@
 local base64 = require "util.encodings".base64;
 local datetime = require "util.datetime";
 local id = require "util.id";
-local jwt_sign, jwt_verify = require "util.jwt".init("HS256", unified_push_secret);
+local jwt = require "util.jwt";
 local st = require "util.stanza";
 local urlencode = require "util.http".urlencode;
 
@@ -23,6 +23,23 @@
 	return s;
 end
 
+-- COMPAT w/0.12
+local function jwt_sign(data)
+	return jwt.sign(data, unified_push_secret);
+end
+
+-- COMPAT w/0.12: add expiry check
+local function jwt_verify(token)
+	local ok, result = jwt.verify(token, unified_push_secret);
+	if not ok then
+		return ok, result;
+	end
+	if result.exp and result.exp < os.time() then
+		return nil, "token-expired";
+	end
+	return ok, result;
+end
+
 -- Handle incoming registration from XMPP client
 function handle_register(event)
 	local origin, stanza = event.origin, event.stanza;