增加应用程序生成token存储到数据表中,修改第三方认证的业务流程,优化部分函数代码和函数的名称

This commit is contained in:
wanglei 2025-11-15 16:07:07 +08:00
parent e6c50c15c9
commit 23826f0cd5
28 changed files with 194 additions and 160 deletions

View File

@ -31,12 +31,6 @@ local routes = {
methods = { "POST" },
handler = oauthService.userinfo,
},
--根据Refresh-Token刷新Access-Token
{
paths = { "/yum/v1/oauth/v2/refresh" },
methods = { "GET", "POST" },
handler = oauthService.refresh,
},
}
-- 初始化路由

View File

@ -5,6 +5,7 @@
---
local userDao = require("dao.system.user")
local applicationDao = require("dao.system.application")
local applicationTokenDao = require("dao.system.applicationtoken")
local _M = {}
@ -12,13 +13,10 @@ local _M = {}
function _M.authenticateUserPasswd(username, passwd)
--验证用户名是否为空
local code, res = userDao:getUserByUsername(username) --authenticate(name, passwd)
if code ~= 0 then
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
local num = table.getn(res)
--用户不存在时返回
if num <= 0 then
return 0x01000C,nil
@ -40,13 +38,10 @@ function _M.login(jsonData)
local checkKey = jsonData["checkKey"]
--验证用户名是否为空
local code, res = userDao:getUserByUsername(name) --authenticate(name, passwd)
if code ~= 0 then
if code ~= 0 or res == nil then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
local num = table.getn(res)
--用户不存在时返回
if num <= 0 then
return 0x01000C,nil
@ -93,4 +88,8 @@ function _M.getApplicationByUserid(user_id, client_id, client_secret)
return applicationDao.getApplicationByUserid(user_id, client_id, client_secret)
end
function _M.updateApplicationToken(client_id, ret)
return applicationTokenDao.updateApplicationToken(client_id, ret)
end
return _M

View File

@ -16,13 +16,10 @@ local _M = {}
local function isExistAccount(id)
--根据账户id进行验证账户是否存在
local code, res = accountModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--账户不存在返回错误
if num <= 0 then
return false
@ -47,13 +44,10 @@ function _M.addSystemAccount(jsonData)
--根据账户进行验证账户是否存在
local code, res = accountModel:where("name", "=", name):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--账户存在时返回账户已经存在
if num > 0 then
return 0x01000C,nil

View File

@ -59,17 +59,10 @@ function _M.addSystemApplication(jsonData)
--根据应用进行验证是否存在
local code, res = applicationModel:where("name", "=", name):get()
if code ~= 0 then
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--用户存在时返回用户已经存在
if num > 0 then
return 0x01000C,nil
end
local num = table.getn(res)
--应用存在时返回应用已经存在
if num > 0 then
return 0x01000C, nil

View File

@ -0,0 +1,86 @@
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/11/15 09:38
--- 应用程序token数据表模型文件
local helpers = require("share.helpers")
--引用使用的库文件
local model = require("share.model")
--创建一个数据表相关的模型
local applicationTokenModel = model:new('sys_application_token')
local _M = {}
--判断应用是否存在
local function isExistApplicationToken(client_id)
--根据应用id进行验证应用是否存在
local code, res = applicationTokenModel:find(client_id)
if code ~= 0 or res == nil then
return false
end
local num = table.getn(res)
--应用不存在返回错误
if num <= 0 then
return false
end
return true
end
-- 查询数据表中的所有应用信息
function _M.getApplicationTokens(pageNum, pageSize)
return applicationTokenModel:paginate(pageNum, pageSize)
end
--根据应用id获取应用信息
function _M.getApplicationToken(id)
return applicationTokenModel.find(id)
end
--增加应用信息到数据表
function _M.addSystemApplicationToken(client_id, jsonData)
--根据应用进行验证是否存在
local code, res = applicationTokenModel:where("application_id", "=", client_id):get()
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--应用存在时返回应用已经存在
if num > 0 then
return 0x01000C, nil
end
-- 创建一个应用
return applicationTokenModel:create(jsonData)
end
--删除应用信息到数据表
function _M.deleteApplicationToken(id)
--根据用户id进行验证用户是否存在
local ok = isExistApplicationToken(id)
--用户不存在则返回
if ok == false then
return 0x000001,nil
end
return applicationTokenModel:delete(id)
end
--更新应用信息到数据表
function _M.updateApplicationToken(id, jsonData)
--根据用户id进行验证用户是否存在
local ok = isExistApplicationToken(id)
--用户不存在则返回
if ok == false then
return 0x000001,nil
end
--对数据内容进行更
jsonData.update_time = ngx.time()
return applicationTokenModel:where('application_id', '=', id):update(jsonData)
end
--根据客户端id和重定向地址获取应用程序
function _M.getApplicationTokenBy(client_id, redirect_uri)
--print("getApplicationTokenBy client_id:", client_id, " redirect_uri:", redirect_uri)
return applicationTokenModel:where('app_id', '=', client_id):where('redirect_uris', '=', redirect_uri):get()
end
return _M

View File

@ -16,13 +16,10 @@ local _M = {}
local function isExistDepartment(id)
--根据组织id进行验证组织是否存在
local code, res = departmentModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--组织不存在返回错误
if num <= 0 then
return false
@ -47,13 +44,10 @@ function _M.addSystemDepartment(jsonData)
--根据组织名称进行验证组织是否存在
local code, res = departmentModel:where("name", "=", name):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--组织架构存在时返回组织架构已经存在
if num > 0 then
return 0x01000C, nil

View File

@ -34,13 +34,10 @@ function _M.login(jsonData)
local checkKey = jsonData["checkKey"]
--验证用户名是否为空
local code, res = userDao:getUserByUsername(name) --authenticate(name, passwd)
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--用户不存在时返回
if num <= 0 then
return 0x01000C,nil

View File

@ -16,13 +16,10 @@ local _M = {}
local function isExistPermission(id)
--根据权限id进行验证权限是否存在
local code, res = permissionModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--权限不存在返回错误
if num <= 0 then
return false
@ -54,13 +51,10 @@ function _M.addSystemPermission(jsonData)
--根据权限名称进行验证权限是否存在
local code, res = permissionModel:where("name", "=", name):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--权限存在时返回权限已经存在
if num > 0 then
return 0x01000C,nil

View File

@ -16,13 +16,10 @@ local _M = {}
local function isExistPosition(id)
--根据岗位id进行验证岗位是否存在
local code, res = positionModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--岗位不存在返回错误
if num <= 0 then
return false
@ -47,13 +44,10 @@ function _M.addSystemPosition(jsonData)
--根据岗位id进行验证岗位是否存在
local code, res = positionModel:where("post_id", "=", post_id):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--岗位存在时返回岗位已经存在
if num > 0 then
return 0x01000C, nil

View File

@ -16,13 +16,10 @@ local _M = {}
local function isExistRole(id)
--根据角色id进行验证角色是否存在
local code, res = roleModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--角色不存在返回错误
if num <= 0 then
return false
@ -52,13 +49,10 @@ function _M.addSystemRole(jsonData)
--根据角色、手机号、邮箱进行验证角色是否存在
local code, res = roleModel:where("role_name", "=", roleName):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--角色存在时返回角色已经存在
if num > 0 then
return 0x01000C,nil

View File

@ -23,13 +23,10 @@ local user = {
local function isExistUser(id)
--根据用户id进行验证用户是否存在
local code, res = userModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--用户不存在返回错误
if num <= 0 then
return false
@ -68,13 +65,10 @@ function _M:addSystemUser(jsonData)
--根据用户、手机号、邮箱进行验证用户是否存在
local code, res = self.getUserByUsername(userName)
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
if code ~= 0 or res == nil then
return 0x000001, res
end
local num = table.getn(res)
--用户存在时返回用户已经存在
if num > 0 then
return 0x01000C,nil

View File

@ -172,17 +172,36 @@ local function authorizateCode(args)
-- 生存id_token
local new_id_token = token.generate_id_token(priv_key, user_id, client_id, scope)
--ngx.say("Generated JWT: ", jwt_obj)
-- 6.返回结果
local ret = {}
ret.access_token = new_access_token
ret.token_type = "Bearer"
ret.expires_in = 10 * 60
ret.refresh_token = new_refresh_token
ret.id_token = new_id_token
-- 6.将生成的数据存储到数据库中
local code, res = oauthDao.addSystemApplicationToken(client_id, ret)
if code ~= 0 then
local result = resp:json(0x000001)
resp:send(result)
return
end
-- 7.返回结果
local result = resp:json(ngx.HTTP_OK, ret)
resp:send(result)
end
-- 刷新令牌
local function authorizateRefresh(args)
-- 1.校验必填参数验证数据是否符合json
local ok = validator.validateRefresh(args)
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
-- 2.
end
-- 根据授权码获取Access-Token
function _M:token()
-- 1. 解析请求参数(支持 form-data 和 json
@ -217,6 +236,8 @@ function _M:token()
authorizatePassword(args)
elseif grant_type == "authorization_code" then
authorizateCode(args)
elseif grant_type == "refresh_token" then
authorizateRefresh(args)
end
end
@ -292,27 +313,4 @@ function _M:userinfo()
resp:send(result)
end
--根据Refresh-Token刷新Access-Token
function _M:refresh()
--读取请求体的数据
ngx.req.read_body()
--获取请求数据
local body_data = ngx.req.get_body_data()
--验证json数据是否正确
local ok, data = pcall(cjson.decode, body_data)
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
-- 验证数据是否符合json
local ok = validator.validateRefresh(data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
resp:send(result)
return
end
end
return _M

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对账户数据表进行数据表业务处理
local resp = require("util.response")
local accountDao = require("dao.system.account")
local validatorJson = require("validator.system.account")
local validator = require("validator.system.account")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
@ -56,7 +56,7 @@ function _M.addSystemAccount()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -97,7 +97,7 @@ function _M.updateSystemAccount(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对应用数据表进行数据表业务处理
local resp = require("util.response")
local applicationDao = require("dao.system.application")
local validatorJson = require("validator.system.application")
local validator = require("validator.system.application")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
@ -84,7 +84,7 @@ function _M.addSystemApplication()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -125,7 +125,7 @@ function _M.updateSystemApplication(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对组织架构数据表进行数据表业务处理
local resp = require("util.response")
local departmentDao = require("dao.system.department")
local validatorJson = require("validator.system.department")
local validator = require("validator.system.department")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
@ -53,7 +53,7 @@ function _M.addSystemDepartment()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -95,7 +95,7 @@ function _M.updateSystemDepartment(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对权限数据表进行数据表业务处理
local resp = require("util.response")
local permissionDao = require("dao.system.permission")
local validatorJson = require("validator.system.permission")
local validator = require("validator.system.permission")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
@ -70,7 +70,7 @@ function _M.addSystemPermission()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -111,7 +111,7 @@ function _M.updateSystemPermission(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对岗位数据表进行数据表业务处理
local resp = require("util.response")
local positionDao = require("dao.system.position")
local validatorJson = require("validator.system.position")
local validator = require("validator.system.position")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
@ -56,7 +56,7 @@ function _M.addSystemPosition()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -97,7 +97,7 @@ function _M.updateSystemPosition(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对用户角色数据表进行数据表业务处理
local resp = require("util.response")
local roleDao = require("dao.system.role")
local validatorJson = require("validator.system.role")
local validator = require("validator.system.role")
local cjson = require("cjson.safe")
local perm = require("util.permissionfilter")
@ -57,7 +57,7 @@ function _M.addSystemRole()
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -98,7 +98,7 @@ function _M.updateSystemRole(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合schema
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -5,7 +5,7 @@
--- 业务逻辑 对用户数据表进行数据表业务处理
local resp = require("util.response")
local userDao = require("dao.system.user")
local validatorJson = require("validator.system.user")
local validator = require("validator.system.user")
local cjson = require("cjson.safe")
local token = require("util.token")
local perm = require("util.permissionfilter")
@ -86,7 +86,7 @@ function _M.addSystemUser(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)
@ -135,7 +135,7 @@ function _M.updateSystemUser(m)
--获取请求数据
local body_data = ngx.req.get_body_data()
-- 验证数据是否符合json
local ok = validatorJson.validatorJson(body_data)
local ok = validator.validateJson(body_data)
--验证失败则返回
if not ok then
local result = resp:json(0x000001)

View File

@ -19,7 +19,7 @@ local schema = {
--设置JWT的有效载荷
local obj = {
header = {typ="JWT", alg="HS256"},
header = { typ = "JWT", alg = "HS256" },
payload = { -- 自定义数据
userid = "", -- 用户id
username = "", -- 用户名
@ -103,7 +103,7 @@ function _M.generate_access_token(priv_key, sub, client_id, scope)
local payload = {
--iss = OP_DOMAIN,
sub = sub,
client_id = client_id,
aud = client_id,
exp = now + access_token_ttl,
iat = now,
scope = scope, --"openid profile email"
@ -122,7 +122,7 @@ function _M.generate_refresh_token(priv_key, sub, client_id, scope)
local payload = {
--iss = OP_DOMAIN,
sub = sub,
client_id = client_id,
aud = client_id,
exp = now + refresh_token_ttl,
iat = now,
scope = scope, --"openid profile email"

View File

@ -82,12 +82,15 @@ function _M.validateUserinfo(jsonData)
return result
end
--grant_type=refresh_token&refresh_token=fbde81ee-f419-42b1-1234-9191f1f95be9&client_id=demoClientId&client_secret=demoClientSecret
local schemaRefresh = {
type = "object",
properties = {
Authorization = { type = "string" },
grant_type = { type = "string" },
client_id = { type = "string" },
client_secret = { type = "string" },
},
required = { "Authorization" }
required = { "grant_type", "client_id", "client_secret" }
}
--根据Refresh-Token刷新Access-Token

View File

@ -22,7 +22,7 @@ local schema = {
required = { "name", "redirect_uris" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)

View File

@ -23,7 +23,7 @@ local schema = {
required = { "name", "redirect_uris" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)

View File

@ -23,7 +23,7 @@ local schema = {
required = { "name" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)

View File

@ -20,7 +20,7 @@ local schema = {
required = { "permission_name", "permission_code" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)

View File

@ -22,7 +22,7 @@ local schema = {
required = { "post_id" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)

View File

@ -19,7 +19,7 @@ local schema = {
required = { "role_name" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)

View File

@ -22,7 +22,7 @@ local schema = {
required = { "username", "phone", "email", "idcard" }
}
function _M.validatorJson(jsonData)
function _M.validateJson(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schema)
local result = validator(jsonData)