-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathapi_rewrite.lua
58 lines (52 loc) · 1.36 KB
/
api_rewrite.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
-- 仅在调用api时进行鉴权
local ngx_var = ngx.var
local ngx_ctx = ngx.ctx
local stool = require "stool"
local modcache = require("modcache")
local jwt = require "resty.jwt"
local ngx_say = ngx.say
local ngx_exit = ngx.exit
local config = modcache.keys["config"].cache
local base = config.base
if not base then
ngx_say([=[{"code":"error","msg":"config.base is error"}]=])
ngx_exit(200)
end
local jwt_Mod = base.jwt_Mod
local uri_white_list = {
}
if stool.isInArrayTb(ngx_var.uri,uri_white_list) then
return
end
local function header_jwt_check()
if jwt_Mod.state == "off" then
return true
end
local client_token = ngx_var[(jwt_Mod.header_name or "zj_jwt_token")]
if not client_token then
return false
end
local jwt_obj = jwt:load_jwt(client_token)
if type(jwt_obj.payload) ~= "table" then
return false
end
local aud = jwt_obj.payload.aud
local app_id = jwt_Mod.appList[aud]
if not app_id then
return false
end
--- 签名检查
local jwt_verify = jwt:verify(jwt_Mod.hmac, client_token)
if not jwt_verify.verified then
return false
end
-- 后续添加 角色 权限等判断
return true
end
if header_jwt_check() then
-- ngx_ctx.api_pass = "yes"
return
else
ngx_say([=[{"code":"error","msg":"sign error"}]=])
ngx_exit(200)
end