Files
nft-proxy/nginx-proxy/lua/auth/auth_config.lua
T
2026-02-08 20:53:19 +04:00

97 lines
3.0 KiB
Lua

local _M = {}
local cjson = require "cjson"
-- Configuration loading function
function _M.load_config(premature)
if premature then
return
end
-- Use provided config file path or fallback
local config_path = os.getenv("AUTH_CONFIG_FILE") or "/app/auth_config.json"
-- Get Go Auth Service URL from environment variable
local base_auth_url = os.getenv("GO_AUTH_SERVICE_URL") or "http://auth-service:8081"
-- Remove trailing slash to fix proxy_pass behavior
_M.go_auth_service_url = base_auth_url:gsub("/$", "")
ngx.log(ngx.NOTICE, "auth_config: Using auth service URL: ", _M.go_auth_service_url)
-- Read and parse JSON config file
local config_data = _M.read_json_config(config_path)
if config_data then
-- JWT rate limiting configuration
_M.requests_per_token = config_data.requests_per_token or 100
-- JWT token expiry configuration
_M.token_expiry_minutes = config_data.token_expiry_minutes or 10
-- Log the initialized values
ngx.log(ngx.NOTICE, "auth_config: Loaded from ", config_path)
ngx.log(ngx.NOTICE, "auth_config: requests_per_token=", _M.requests_per_token)
ngx.log(ngx.NOTICE, "auth_config: token_expiry_minutes=", _M.token_expiry_minutes)
else
-- Fallback to default values if JSON config fails
_M.requests_per_token = 100
_M.token_expiry_minutes = 10
ngx.log(ngx.WARN, "auth_config: Using default values")
end
end
-- Initialize configuration using timer
function _M.init()
-- Set default values immediately to prevent race conditions
local default_url = os.getenv("GO_AUTH_SERVICE_URL") or "http://auth-service:8081"
_M.go_auth_service_url = default_url:gsub("/$", "")
_M.requests_per_token = 100
_M.token_expiry_minutes = 10
-- Schedule config loading using timer
local ok, err = ngx.timer.at(0, _M.load_config)
if not ok then
ngx.log(ngx.ERR, "auth_config: Failed to create timer: ", err)
end
end
-- Read and parse JSON configuration file
function _M.read_json_config(file_path)
local file = io.open(file_path, "r")
if not file then
ngx.log(ngx.ERR, "auth_config: Could not open config file: ", file_path)
return nil
end
local content = file:read("*all")
file:close()
if not content or content == "" then
ngx.log(ngx.ERR, "auth_config: Config file is empty: ", file_path)
return nil
end
local ok, config_data = pcall(cjson.decode, content)
if not ok then
ngx.log(ngx.ERR, "auth_config: Failed to parse JSON config: ", config_data)
return nil
end
return config_data
end
-- Getter functions for clean access
function _M.get_requests_per_token()
return _M.requests_per_token
end
function _M.get_token_expiry_minutes()
return _M.token_expiry_minutes
end
function _M.get_go_auth_service_url()
return _M.go_auth_service_url
end
return _M