1
0
forked from Tank/braga
braga/framework/auth.lua
2026-03-08 15:29:51 +04:00

117 lines
2.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local _M = {}
local resp = require('framework.resp')
local shell = require "resty.shell"
require("resty.session").init({
remember = true,
audience = "proxy",
secret = "8UQW1fsdag",
storage = "cookie",
})
local login
local password
local admin_login = 'nz8080'
local admin_password = 'WarWind.Ru'
local error = {code = 400, error = 'Неизвестная ошибка авторизации'}
local errors = {
NO_HEADER = {code = 400, error = 'Отсутствует заголовок для авторизации'},
NO_BASIC = {code = 412, error = 'Неподдерживаемый тип авторизации'},
NO_BASE = {code = 412, error = 'Неподдерживаемый тип строки авторизации'},
NO_LOGIN = {code = 412, error = 'Не указан логин'},
NO_PASSWORD = {code = 412, error = 'Не указан пароль'},
BAD_LOGIN = {code = 403, error = 'Логин не верен'},
BAD_PASSWORD = {code = 403, error = 'Пароль не верен'},
BAD_USER = {code = 403, error = 'Пользователь не найден'},
BAD_SALT = {code = 444, error = 'Неверный размер номера устройства'},
BAD_SEED = {code = 444, error = 'Контрольная сумма не совпадает'},
}
local function seed(salt)
local ok, stdout, stderr, reason, status =
shell.run('/home/zoviet/lock/decode/decode '..salt, '', 3000, 128)
if not ok then
return false
end
return tonumber(stdout)
end
function get_auth()
local auth = ngx.req.get_headers()['Authorization']
if auth==nil then
error = errors.NO_HEADER
return false
end
auth = string.match(auth,'Basic (.+)')
if auth==nil then
error = errors.NO_BASIC
return false
end
auth = ngx.decode_base64(auth)
if auth==nil then
error = errors.NO_BASE
return false
end
login,password = string.match(auth,'(.+):(.+)')
if login == nil then
error = errors.NO_LOGIN
return false
end
if password == nil then
error = errors.NO_PASSWORD
return false
end
return true
end
function _M.resp()
return resp(nil,error)
end
function _M.error()
return error.error
end
-- Авторизация админа
function _M.admin()
if not get_auth() then return false end
if admin_login ~= login then
error = errors.BAD_LOGIN
return false
end
if admin_password ~= password then
error = errors.BAD_PASSWORD
return false
end
return true
end
-- Авторизация админ-панели
function _M.front(login,password)
if admin_login ~= login then
error = errors.BAD_LOGIN
return false
end
if admin_password ~= password then
error = errors.BAD_PASSWORD
return false
end
return true
end
-- Авторизация через сессии
function _M.session()
local ses = require "resty.session".start()
print(ses:get("login"))
if _M.front(ses:get("login"),ses:get("password")) then return true end
require "resty.session".destroy()
ngx.redirect('/login')
end
return _M