92 lines
2.9 KiB
Lua
92 lines
2.9 KiB
Lua
local config = require("fwutils.config")
|
|
local codec = require("fastweb.codec")
|
|
local utils = require("utils")
|
|
local cjson = require("cjson")
|
|
local redis_pool = require("redis.pool")
|
|
local M = {}
|
|
local function make_key(id,token)
|
|
if type(id) == "number" then
|
|
id = string.format("%d",id)
|
|
end
|
|
return config.server_id.."_token_"..id.."_"..token
|
|
end
|
|
M.__parse_token = function(token)
|
|
if token == nil or token == "" then
|
|
return false,"token is required"
|
|
end
|
|
local token_data = codec.aes_de(config.token.key,utils.hex_to_bytes(token),config.token.algorithm,config.token.mode)
|
|
if token_data == nil or token_data == "" then
|
|
return false,"token is invalid(a)"
|
|
end
|
|
local token_data_json = cjson.decode(utils.hex_to_bytes(token_data))
|
|
if token_data_json == nil or token_data_json.id == nil or token_data_json.id <= 0 then
|
|
return false,"token is invalid(no id)"
|
|
end
|
|
return true,token_data_json
|
|
end
|
|
M.get = function(token)
|
|
-- 解析TOKEN
|
|
local result,token_data_json = M.__parse_token(token)
|
|
if not result then
|
|
return false,token_data_json
|
|
end
|
|
|
|
|
|
-- 获取TOKEN数据
|
|
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
|
|
|
|
local data = conn:get(make_key(token_data_json["id"],token))
|
|
if data ~= nil and data ~= "" then
|
|
return true,cjson.decode(data)
|
|
end
|
|
return false,"token is invalid"
|
|
end
|
|
M.set = function(token,data)
|
|
-- 解析TOKEN
|
|
local result,token_data_json = M.__parse_token(token)
|
|
if not result then
|
|
return false,token_data_json
|
|
end
|
|
if data == nil then
|
|
return false,"data is required"
|
|
end
|
|
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
|
|
conn:setex(make_key(token_data_json["id"],token),config.token.expire,cjson.encode(data))
|
|
return true
|
|
end
|
|
M.del_by_id = function(id)
|
|
if id == nil or id <= 0 then
|
|
return false,"id is required"
|
|
end
|
|
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
|
|
local keys = conn:keys(config.other.SERVER_ID.."_token_"..string.format("%d",id).."_*")
|
|
for _,key in ipairs(keys) do
|
|
conn:del(key)
|
|
end
|
|
return true
|
|
end
|
|
M.del_by_token = function(token)
|
|
-- 解析TOKEN
|
|
local result,token_data_json = M.__parse_token(token)
|
|
if not result then
|
|
return false,token_data_json
|
|
end
|
|
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
|
|
conn:del(make_key(token_data_json["id"],token))
|
|
return true
|
|
end
|
|
M.create = function(id,data)
|
|
if id == nil or id <= 0 then
|
|
return false,"id is required"
|
|
end
|
|
local key_data = {
|
|
id = id,
|
|
create_time = fw.now_msec()
|
|
}
|
|
local token = codec.aes_en(config.token.key,cjson.encode(key_data),config.token.algorithm,config.token.mode)
|
|
if M.set(token,data) == false then
|
|
return false,"create token failed"
|
|
end
|
|
return true,token
|
|
end
|
|
return M |