if not res then
ngx.log(ngx.ERR, "请求状态端点失败 : ", err)
return ngx.exit(500)
end
-- 检查状态码是否为 200
if res.status ~= 200 then
ngx.exit(403)
end
-- 解析JSON
local data, err = cjson.decode(res.body)
if not data then
ngx.log(ngx.ERR, "JSON解析失败 : ", err)
return ngx.exit(500)
end
-- 检查需要的字段
if data.Status ~= "display-2fa-qrcode" or not data.QRCode or not data.QRCode.secret
then
ngx.exit(403)
end
-- 如果所有条件满足,允许继续处理请求到 abc.cgi
-- 这里不需要额外动作,因为 access阶段通过后会继续执行 proxy_pass等
}
proxy_pass https://example.com/abc.cgi;
}
-- 替換成實際的 2FA 檢查 URL
local res, err = httpc:request_uri("https://your-auth-server/2fa-check", {
method = "GET",
ssl_verify = false, -- 根據需要啟用 SSL 驗證
headers = {
["Content-Type"] = "application/json",
-- 添加需要的 headers (如 Authorization)
}
})
if not res then
ngx.log(ngx.ERR, "2FA 驗證請求失敗 : ", err)
return false
end
-- 檢查 HTTP 狀態碼
if res.status ~= 200 then
ngx.log(ngx.WARN, "非 200 狀態碼: ", res.status)
return false
end
-- 解析 JSON 響應
local data = cjson.decode(res.body)
if not data then
ngx.log(ngx.ERR, "JSON 解析失敗 ")
return false
end
-- 驗證響應內容
return data.Status == "display-2fa-qrcode"
and data.QRCode
and data.QRCode.secret ~= nil
end
-- 主邏輯
if verify_2fa_condition() then
-- 條件滿足時繼續存取目標 URL
ngx.exec("/proxy-abc-cgi") -- 需要在 nginx 配置對應的 location