token.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. local config = require("fwutils.config")
  2. local codec = require("fastweb.codec")
  3. local utils = require("utils")
  4. local cjson = require("cjson")
  5. local redis_pool = require("redis.pool")
  6. local M = {}
  7. local function make_key(id,token)
  8. if type(id) == "number" then
  9. id = string.format("%d",id)
  10. end
  11. return config.server_id.."_token_"..id.."_"..token
  12. end
  13. M.__parse_token = function(token)
  14. if token == nil or token == "" then
  15. return false,"token is required"
  16. end
  17. local token_data = codec.aes_de(config.token.key,utils.hex_to_bytes(token),config.token.algorithm,config.token.mode)
  18. if token_data == nil or token_data == "" then
  19. return false,"token is invalid(a)"
  20. end
  21. local token_data_json = cjson.decode(utils.hex_to_bytes(token_data))
  22. if token_data_json == nil or token_data_json.id == nil or token_data_json.id <= 0 then
  23. return false,"token is invalid(no id)"
  24. end
  25. return true,token_data_json
  26. end
  27. M.get = function(token)
  28. -- 解析TOKEN
  29. local result,token_data_json = M.__parse_token(token)
  30. if not result then
  31. return false,token_data_json
  32. end
  33. -- 获取TOKEN数据
  34. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  35. local data = conn:get(make_key(token_data_json["id"],token))
  36. if data ~= nil and data ~= "" then
  37. return true,cjson.decode(data)
  38. end
  39. return false,"token is invalid"
  40. end
  41. M.set = function(token,data)
  42. -- 解析TOKEN
  43. local result,token_data_json = M.__parse_token(token)
  44. if not result then
  45. return false,token_data_json
  46. end
  47. if data == nil then
  48. return false,"data is required"
  49. end
  50. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  51. conn:setex(make_key(token_data_json["id"],token),config.token.expire,cjson.encode(data))
  52. return true
  53. end
  54. M.del_by_id = function(id)
  55. if id == nil or id <= 0 then
  56. return false,"id is required"
  57. end
  58. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  59. local keys = conn:keys(config.other.SERVER_ID.."_token_"..string.format("%d",id).."_*")
  60. for _,key in ipairs(keys) do
  61. conn:del(key)
  62. end
  63. return true
  64. end
  65. M.del_by_token = function(token)
  66. -- 解析TOKEN
  67. local result,token_data_json = M.__parse_token(token)
  68. if not result then
  69. return false,token_data_json
  70. end
  71. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  72. conn:del(make_key(token_data_json["id"],token))
  73. return true
  74. end
  75. M.create = function(id,data)
  76. if id == nil or id <= 0 then
  77. return false,"id is required"
  78. end
  79. local key_data = {
  80. id = id,
  81. create_time = fw.now_msec()
  82. }
  83. local token = codec.aes_en(config.token.key,cjson.encode(key_data),config.token.algorithm,config.token.mode)
  84. if M.set(token,data) == false then
  85. return false,"create token failed"
  86. end
  87. return true,token
  88. end
  89. return M