AuthPlatform/src/validator/oauth/oauth.lua

139 lines
3.8 KiB
Lua

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/30 08:09
---业务逻辑 对账户登录的参数进行数据的验证
local jsonschema = require("jsonschema")
local _M = {}
-- 定义一个JSON Schema
local schemaLogin = {
type = "object",
properties = {
client_id = { type = "string" },
client_secret = { type = "string" },
},
required = { "client_id", "client_secret" }
}
--通过用户名和密码进行认证
function _M.validateLogin(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaLogin)
local result = validator(jsonData)
return result
end
-- 定义一个JSON Schema
local schemaAuth = {
type = "object",
properties = {
response_type = { type = "string" },
client_id = { type = "string" },
redirect_uri = { type = "string" },
scope = { type = "string" },
state = { type = "string" },
},
required = { "response_type", "client_id", "redirect_uri" }
}
--获取授权码
function _M.validateAuthorize(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaAuth)
local result = validator(jsonData)
return result
end
-- 定义一个JSON Schema
local schemaUserPasswd = {
type = "object",
properties = {
grant_type = { type = "string" },
client_id = { type = "string" },
client_secret = { type = "string" },
username = { type = "string" },
password = { type = "string" },
},
required = { "grant_type", "client_id", "client_secret", "username", "password" }
}
--通过用户名和密码进行认证
function _M.validateUserPasswd(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaUserPasswd)
local result = validator(jsonData)
return result
end
local schemaToken = {
type = "object",
properties = {
grant_type = { type = "string" },
code = { type = "string" },
},
required = { "grant_type", "code" }
}
--根据授权码获取Access-Token
function _M.validateToken(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaToken)
local result = validator(jsonData)
return result
end
local schemaUserInfo = {
type = 'object',
properties = {
Authorization = {type = 'string', minLength = 8, pattern = 'Bearer\\s+(.+)$'},
}, required = {"Authorization"}
}
--根据Access-Token获取相应用户的账户信息
function _M.validateUserinfo(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaUserInfo)
local result = validator(jsonData)
return result
end
local schemaRefresh = {
type = "object",
properties = {
grant_type = { type = "string" },
refresh_token = { type = "string" },
client_id = { type = "string" },
client_secret = { type = "string" },
},
required = { "grant_type", "refresh_token", "client_id", "client_secret" }
}
--根据Refresh-Token刷新Access-Token
function _M.validateRefresh(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaRefresh)
local result = validator(jsonData)
return result
end
local schemaLogout = {
type = "object",
properties = {
access_token = { type = "string" },
client_id = { type = "string" },
client_secret = { type = "string" },
},
required = { "access_token", "client_id", "client_secret" }
}
--回收Token
function _M.validateLogout(jsonData)
-- 验证数据是否符合schema
local validator = jsonschema.generate_validator(schemaLogout)
local result = validator(jsonData)
return result
end
return _M