token.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 mysql_pool = require("mysql.pool")
  7. local M = {}
  8. local function make_key(id,token)
  9. if type(id) == "number" then
  10. id = string.format("%d",id)
  11. end
  12. return config.server_id.."_token_"..id.."_"..token
  13. end
  14. M.__delete_mysql_token = function(token,conn)
  15. local update = conn:update()
  16. update:table("fw_token")
  17. update:set("delete_time=NOW()")
  18. update:where_str("`fw_token`.`token`","=",token)
  19. update:where_expression("AND `fw_token`.`delete_time` IS NULL")
  20. local d = update:exec()
  21. return d == 1
  22. end
  23. M.__list_mysql_token = function(role_id,id,conn)
  24. local select = conn:select()
  25. select:table("fw_token")
  26. select:where_i32("`fw_token`.`role_id`","=",role_id)
  27. select:where_i32("`fw_token`.`custom_id`","=",id)
  28. select:where_expression("AND `fw_token`.`delete_time` IS NULL")
  29. local d = select:query():table()
  30. return d
  31. end
  32. M.__parse_token = function(token)
  33. if token == nil or token == "" then
  34. return false,"token is required"
  35. end
  36. local token_data = codec.aes_de(config.token.key,utils.hex_to_bytes(token),config.token.algorithm,config.token.mode)
  37. if token_data == nil or token_data == "" then
  38. return false,"token is invalid(a)"
  39. end
  40. local token_data_json = cjson.decode(utils.hex_to_bytes(token_data))
  41. if token_data_json == nil or token_data_json.id == nil or token_data_json.id <= 0 then
  42. return false,"token is invalid(no id)"
  43. end
  44. return true,token_data_json
  45. end
  46. M.get = function(token)
  47. -- 解析TOKEN
  48. local result,token_data_json = M.__parse_token(token)
  49. if not result then
  50. return false,token_data_json
  51. end
  52. -- 获取TOKEN数据
  53. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  54. local data = conn:get(make_key(token_data_json["id"],token))
  55. if data ~= nil and data ~= "" then
  56. return true,cjson.decode(data)
  57. end
  58. return false,"token is invalid"
  59. end
  60. M.set = function(token,data)
  61. -- 解析TOKEN
  62. local result,token_data_json = M.__parse_token(token)
  63. if not result then
  64. return false,token_data_json
  65. end
  66. if data == nil then
  67. return false,"data is required"
  68. end
  69. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  70. conn:setex(make_key(token_data_json["id"],token),config.token.expire,cjson.encode(data))
  71. return true
  72. end
  73. M.del_by_id = function(id)
  74. if id == nil or id <= 0 then
  75. return false,"id is required"
  76. end
  77. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  78. local conn_mysql = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
  79. local keys = conn:keys(config.server_id.."_token_"..string.format("%d",id).."_*")
  80. for _,key in ipairs(keys) do
  81. conn:del(key)
  82. M.__delete_mysql_token(key,conn_mysql)
  83. end
  84. return true
  85. end
  86. M.del_by_id_ex = function(server_id,id)
  87. if server_id == nil or server_id == "" then
  88. return false,"server_id is required"
  89. end
  90. if id == nil or id <= 0 then
  91. return false,"id is required"
  92. end
  93. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  94. local conn_mysql = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
  95. local keys = conn:keys(server_id.."_token_"..string.format("%d",id).."_*")
  96. for _,key in ipairs(keys) do
  97. conn:del(key)
  98. M.__delete_mysql_token(key,conn_mysql)
  99. end
  100. return true
  101. end
  102. M.del_by_token = function(token)
  103. -- 解析TOKEN
  104. local result,token_data_json = M.__parse_token(token)
  105. if not result then
  106. return false,token_data_json
  107. end
  108. local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
  109. local conn_mysql = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
  110. conn:del(make_key(token_data_json["id"],token))
  111. M.__delete_mysql_token(token,conn_mysql)
  112. return true
  113. end
  114. M.create = function(id,role_id,data)
  115. if id == nil or id <= 0 then
  116. return false,"id is required"
  117. end
  118. local key_data = {
  119. id = id,
  120. create_time = fw.now_msec()
  121. }
  122. local token = codec.aes_en(config.token.key,cjson.encode(key_data),config.token.algorithm,config.token.mode)
  123. if M.set(token,data) == false then
  124. return false,"create token failed"
  125. end
  126. -- 保存TOKEN到MYSQL
  127. local conn = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
  128. local ppst = conn:setsql("INSERT INTO fw_token (role_id,server_id,custom_id,token,create_time) VALUES (?,?,?,?,NOW())")
  129. ppst:set_i32(1,role_id)
  130. ppst:set_str(2,config.server_id)
  131. ppst:set_i32(3,id)
  132. ppst:set_str(4,token)
  133. ppst:update()
  134. return true,token
  135. end
  136. return M