summaryrefslogtreecommitdiffstats
path: root/server/resty/session/encoders/base64.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server/resty/session/encoders/base64.lua')
-rw-r--r--server/resty/session/encoders/base64.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/server/resty/session/encoders/base64.lua b/server/resty/session/encoders/base64.lua
new file mode 100644
index 0000000..ddaf4e8
--- /dev/null
+++ b/server/resty/session/encoders/base64.lua
@@ -0,0 +1,39 @@
+local encode_base64 = ngx.encode_base64
+local decode_base64 = ngx.decode_base64
+
+local gsub = string.gsub
+
+local ENCODE_CHARS = {
+ ["+"] = "-",
+ ["/"] = "_",
+}
+
+local DECODE_CHARS = {
+ ["-"] = "+",
+ ["_"] = "/",
+}
+
+local encoder = {}
+
+function encoder.encode(value)
+ if not value then
+ return nil, "unable to base64 encode value"
+ end
+
+ local encoded = encode_base64(value, true)
+ if not encoded then
+ return nil, "unable to base64 encode value"
+ end
+
+ return gsub(encoded, "[+/]", ENCODE_CHARS)
+end
+
+function encoder.decode(value)
+ if not value then
+ return nil, "unable to base64 decode value"
+ end
+
+ return decode_base64((gsub(value, "[-_]", DECODE_CHARS)))
+end
+
+return encoder