AuthPlatform/src/service/auth/auth.lua

86 lines
2.4 KiB
Lua
Raw Normal View History

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/28 11:09
---
local jsonschema = require("jsonschema")
local resp = require("util.response")
local authDao = require("dao.auth")
local jwt = require("resty.jwt")
local conf = require("config")
local _M = {}
-- 定义一个JSON Schema
local schema = {
{type = "object", properties = {
{name = "username", type = "string"},
{name = "password", type = "string"},
{name = "captcha", type = "string"},
{name = "checkKey", type = "string"},
}, required = {"username", "password"}}
}
--设置JWT的有效载荷
local obj = {
header = {typ="JWT", alg="HS256"},
payload = { -- 自定义数据
username = "",
role = "",
--iss = "your_issuer", -- 签发者
--sub = "1234567890", -- 主题
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时
iat = os.time() -- 签发时间
}
}
--用户登录业务逻辑处理
function _M.login()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok, err = jsonschema:generate_validator(body_data, schema)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = authDao.login(body_data)
--读取数据错误
if code ~= 0 or table.getn(ret) < 0 then
local result = resp:json(0x000001)
resp:send(result)
return
end
--获取的登陆的用户信息返回tocken
obj.payload.username = body_data["name"]
obj.payload.role = ""
local jwt_token = jwt:sign(conf.secret_key, obj)
ngx.say(jwt_token)
local result = resp:json(code, ret)
resp:send(result)
end
--用户登出业务逻辑处理
function _M.logout()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--判断请求体数据是否为空
if body_data == nil then
local result = resp:json(0x000001)
resp:send(result)
return
end
--ngx.say(body_data)
local code, ret = authDao.logout(body_data)
local result = resp:json(code, ret)
resp:send(result)
end
return _M