2015-02-02 00:05:09 +01:00
|
|
|
--
|
|
|
|
-- init.lua
|
|
|
|
--
|
|
|
|
-- This is the initialization file of SSOwat. It is called once at the Nginx
|
|
|
|
-- server's start.
|
|
|
|
-- Consequently, all the variables declared (along with libraries and
|
|
|
|
-- translations) in this file will be *persistent* from one HTTP request to
|
|
|
|
-- another.
|
|
|
|
--
|
|
|
|
|
2013-10-20 16:38:49 +02:00
|
|
|
-- Remove prepending '@' & trailing 'init.lua'
|
|
|
|
script_path = string.sub(debug.getinfo(1).source, 2, -9)
|
|
|
|
|
|
|
|
-- Include local libs in package.path
|
|
|
|
package.path = package.path .. ";"..script_path.."?.lua"
|
|
|
|
|
2013-10-15 10:11:39 +02:00
|
|
|
-- Load libraries
|
2015-05-16 09:42:26 +02:00
|
|
|
local json = require "json"
|
|
|
|
local lualdap = require "lualdap"
|
|
|
|
local math = require "math"
|
|
|
|
local lfs = require "lfs"
|
|
|
|
local socket = require "socket"
|
|
|
|
local config = require "config"
|
2018-06-07 22:33:12 +02:00
|
|
|
lustache = require "lustache"
|
2013-10-15 10:11:39 +02:00
|
|
|
|
2015-02-02 00:05:09 +01:00
|
|
|
-- Persistent shared table
|
2013-10-20 22:07:26 +02:00
|
|
|
flashs = {}
|
2014-05-12 14:32:56 +02:00
|
|
|
i18n = {}
|
|
|
|
|
2017-05-15 03:29:34 +02:00
|
|
|
-- convert a string to a hex
|
|
|
|
function tohex(str)
|
|
|
|
return (str:gsub('.', function (c)
|
|
|
|
return string.format('%02X', string.byte(c))
|
|
|
|
end))
|
|
|
|
end
|
|
|
|
|
2015-04-30 15:08:08 +02:00
|
|
|
-- Efficient function to get a random string
|
2016-04-30 12:40:59 +02:00
|
|
|
function random_string()
|
2017-05-15 03:29:34 +02:00
|
|
|
local random_bytes = io.open("/dev/urandom"):read(64);
|
|
|
|
return tohex(random_bytes);
|
2015-04-30 15:08:08 +02:00
|
|
|
end
|
|
|
|
|
2015-02-02 00:05:09 +01:00
|
|
|
-- Load translations in the "i18n" above table
|
2014-05-12 14:32:56 +02:00
|
|
|
local locale_dir = script_path.."portal/locales/"
|
|
|
|
for file in lfs.dir(locale_dir) do
|
|
|
|
if string.sub(file, -4) == "json" then
|
|
|
|
local lang = string.sub(file, 1, 2)
|
|
|
|
local locale_file = io.open(locale_dir..file, "r")
|
|
|
|
i18n[lang] = json.decode(locale_file:read("*all"))
|
|
|
|
end
|
|
|
|
end
|
2013-10-16 11:27:18 +02:00
|
|
|
|
|
|
|
-- Path of the configuration
|
2013-10-30 17:29:19 +01:00
|
|
|
conf_path = "/etc/ssowat/conf.json"
|
2013-10-20 17:24:44 +02:00
|
|
|
|
2015-02-02 00:05:09 +01:00
|
|
|
-- You should see that in your Nginx error logs by default
|
2013-10-20 17:24:44 +02:00
|
|
|
ngx.log(ngx.INFO, "SSOwat ready")
|