-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathhandler.lua
69 lines (56 loc) · 1.8 KB
/
handler.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
local BasePlugin = require "kong.plugins.base_plugin"
local jwt_decoder = require "kong.plugins.jwt.jwt_parser"
local req_set_header = ngx.req.set_header
local ngx_re_gmatch = ngx.re.gmatch
local JwtClaimsHeadersHandler = BasePlugin:extend()
local function retrieve_token(request, conf)
local uri_parameters = request.get_uri_args()
for _, v in ipairs(conf.uri_param_names) do
if uri_parameters[v] then
return uri_parameters[v]
end
end
local authorization_header = request.get_headers()["authorization"]
if authorization_header then
local iterator, iter_err = ngx_re_gmatch(authorization_header, "\\s*[Bb]earer\\s+(.+)")
if not iterator then
return nil, iter_err
end
local m, err = iterator()
if err then
return nil, err
end
if m and #m > 0 then
return m[1]
end
end
end
function JwtClaimsHeadersHandler:new()
JwtClaimsHeadersHandler.super.new(self, "jwt-claims-headers")
end
function JwtClaimsHeadersHandler:access(conf)
JwtClaimsHeadersHandler.super.access(self)
local continue_on_error = conf.continue_on_error
local token, err = retrieve_token(ngx.req, conf)
if err and not continue_on_error then
return kong.response.exit(500, { message = err })
end
if not token and not continue_on_error then
return kong.response.exit(401)
elseif not token and continue_on_error then
return
end
local jwt, err = jwt_decoder:new(token)
if err and not continue_on_error then
return kong.response.exit(500)
end
local claims = jwt.claims
for claim_key,claim_value in pairs(claims) do
for _,claim_pattern in pairs(conf.claims_to_include) do
if string.match(claim_key, "^"..claim_pattern.."$") then
req_set_header("X-"..claim_key, claim_value)
end
end
end
end
return JwtClaimsHeadersHandler