-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.lua
More file actions
49 lines (42 loc) · 1.55 KB
/
Copy pathhandler.lua
File metadata and controls
49 lines (42 loc) · 1.55 KB
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
local access = require "kong.plugins.request-encrypt.access"
local rc4_encrypt = require "kong.plugins.request-encrypt.encrypt"
local rsa_encrypt = require "kong.plugins.request-encrypt.rsa"
local kong = kong
local RequestEncryptHandler = {
VERSION = "1.0.0",
PRIORITY = 900,
}
function RequestEncryptHandler:access(conf)
access.execute(conf)
end
function RequestEncryptHandler:header_filter(conf)
if kong.response.get_source() ~= "exit"
and conf.response_enabled and kong.request.get_method() ~= "OPTIONS" then
kong.response.clear_header("Content-Length")
kong.response.set_header("Content-Type", "application/kndy; charset=utf-8")
end
end
local function encrypt(secret, body, algorithm)
if algorithm == "rc4" then
return pcall(rc4_encrypt.encrypt, secret, body)
end
if algorithm == "rsa" then
return rsa_encrypt.public_encrypt(body, secret)
end
return nil, nil
end
function RequestEncryptHandler:body_filter(conf)
if kong.response.get_source() ~= "exit"
and conf.response_enabled and kong.request.get_method() ~= "OPTIONS" then
local body = kong.response.get_raw_body()
if body then
local ok, encrypt_body = encrypt(conf.secret, body, conf.algorithm)
if ok and encrypt_body then
kong.response.set_raw_body(encrypt_body)
else
kong.response.exit(200, { code = conf.fail_encrypt_status, massage = conf.fail_encrypt_message })
end
end
end
end
return RequestEncryptHandler