-
Notifications
You must be signed in to change notification settings - Fork 25
/
auth_token.lua
78 lines (70 loc) · 1.75 KB
/
auth_token.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- auth_token.lua
-- 请求验证模块
local cjson = require "cjson"
local redis = require "redis_mcredis"
local red = redis:new()
local _reqnManager = {}
_reqnManager._VERSION = '1.0.0'
_reqnManager._AUTHOR = 'Chao'
-- 判断Redis值是否为空
local function is_redis_null( res )
if type(res) == "table" then
for k,v in pairs(res) do
if v ~= ngx.null then
return false
end
end
return true
elseif res == ngx.null then
return true
elseif res == nil then
return true
end
return false
end
-- 验证Auth Token合法性并返回User ID
function _reqnManager.check_auth_token(self, requst_headers)
local user_id, err = red:hget("token_"..requst_headers["token"], "userid")
if is_redis_null(user_id) then
ngx.log(ngx.INFO, string.format("Token:%s, 找不到对应UserID", requst_headers["token"]))
return nil
end
return user_id
end
-- 验证入口,验证请求合法性
-- 返回block结构休
--[[
_result = {
code = 0,
msg = "",
data = {}
}
code说明:
0:默认值
10001:token过期
--]]
function _reqnManager.check_requst_validity(self)
local _result = {
code = 0,
msg = "",
data = {}
}
local headers = ngx.req.get_headers()
ngx.log(ngx.INFO, string.format("请求接口传入headers :%s\n", cjson.encode(headers)))
-- 验证Token合法性并返回User ID
local user_id = self:check_auth_token(headers)
if not user_id then
_result.code = 10001
_result.msg = "token has been invalid"
return _result, nil
end
ngx.req.set_header("userid", user_id);
return _result, nil
end
function _reqnManager.new(self, req_entity)
local req_entity = req_entity or {}
setmetatable(req_entity, self)
self.__index = self
return req_entity
end
return _reqnManager