From 4122acf8c8e80d7654e9ba7e1297a38e834f49de Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 24 Jun 2022 17:03:28 +0100 Subject: util.paseto: Implementation of PASETO v4.public tokens PASETO provides an alternative to JWT with the promise of fewer implementation pitfalls. The v4.public algorithm allows asymmetric cryptographically-verified token issuance and validation. In summary, such tokens can be issued by one party and securely verified by any other party independently using the public key of the issuer. This has a number of potential applications in a decentralized network and ecosystem such as XMPP. For example, such tokens could be combined with XEP-0317 to allow hats to be verified even in the context of a third-party MUC service. --- util/paseto.lua | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 util/paseto.lua (limited to 'util') diff --git a/util/paseto.lua b/util/paseto.lua new file mode 100644 index 00000000..5f162ad0 --- /dev/null +++ b/util/paseto.lua @@ -0,0 +1,123 @@ +local crypto = require "util.crypto"; +local json = require "util.json"; +local base64_encode = require "util.encodings".base64.encode; +local base64_decode = require "util.encodings".base64.decode; +local secure_equals = require "util.hashes".equals; +local bit = require "util.bitcompat"; +local s_pack = require "util.struct".pack; + +local s_gsub = string.gsub; + +local pubkey_methods = {}; +local privkey_methods = {}; + +local v4_public_pubkey_mt = { __index = pubkey_methods }; +local v4_public_privkey_mt = { __index = privkey_methods }; +local v4_public = {}; + +local b64url_rep = { ["+"] = "-", ["/"] = "_", ["="] = "", ["-"] = "+", ["_"] = "/" }; +local function b64url(data) + return (s_gsub(base64_encode(data), "[+/=]", b64url_rep)); +end +local function unb64url(data) + return base64_decode(s_gsub(data, "[-_]", b64url_rep).."=="); +end + +local function le64(n) + return s_pack("