This commit is contained in:
2021-11-27 09:44:08 +08:00
parent 75fb44a268
commit ca287ebc16
+31 -43
View File
@@ -1,8 +1,9 @@
local upload = require "resty.upload" local upload = require "resty.upload"
local uuid = require "resty.jit-uuid" local uuid = require "resty.jit-uuid"
local cjson = require "cjson" local cjson = require "cjson"
local jwt = require "resty.jwt" --# https://github.com/SkyLothar/lua-resty-jwt
local redis = require "resty.redis" --$ opm get SkyLothar/lua-resty-jwt
local jwt = require "resty.jwt"
local chunk_size = 4096 local chunk_size = 4096
local form = upload:new(chunk_size) local form = upload:new(chunk_size)
@@ -23,31 +24,19 @@ local files = {}
local http = require "resty.http" local http = require "resty.http"
local httpc = http.new() local httpc = http.new()
---- Redis init function cleanupString(str, remove)
------ Connect to redis
-- local redis_task_database = redis:new()
-- redis_task_database:set_timeouts(1000, 1000, 1000)
-- local ok, error = redis_task_database:connect("127.0.0.1", 6379)
-- if not ok then
-- ngx.log(ngx.ERR, "Fail to connect redis: ", error)
-- ngx.exit(500)
-- end
function LuaReomve(str,remove)
local lcSubStrTab = {} local lcSubStrTab = {}
while true do while true do
local lcPos = string.find(str,remove) local lcPos = string.find(str, remove)
if not lcPos then if not lcPos then
lcSubStrTab[#lcSubStrTab+1] = str lcSubStrTab[#lcSubStrTab + 1] = str
break break
end end
local lcSubStr = string.sub(str,1,lcPos-1) local lcSubStr = string.sub(str, 1, lcPos - 1)
lcSubStrTab[#lcSubStrTab+1] = lcSubStr lcSubStrTab[#lcSubStrTab + 1] = lcSubStr
str = string.sub(str,lcPos+1,#str) str = string.sub(str, lcPos + 1, #str)
end end
local lcMergeStr ="" local lcMergeStr = ""
local lci = 1 local lci = 1
while true do while true do
if lcSubStrTab[lci] then if lcSubStrTab[lci] then
@@ -60,15 +49,14 @@ function LuaReomve(str,remove)
return lcMergeStr return lcMergeStr
end end
function verifyJwtToken(encrypted_token)
function VerifyJwtToken(encrypted_token)
local http = require "resty.http" local http = require "resty.http"
local httpc = http.new() local httpc = http.new()
local remote_url = string.format( "%s/api/v1/uc/verify_jwt_token", hty_uc_host) local remote_url = string.format("%s/api/v1/uc/verify_jwt_token", hty_uc_host)
ngx.log(ngx.INFO, 'htyuc remote_url -> ', remote_url) ngx.log(ngx.INFO, 'htyuc remote_url -> ', remote_url)
-- ngx.log(ngx.INFO, 'Authorization -> ', encrypted_token) ngx.log(ngx.INFO, 'Authorization -> ', encrypted_token)
local res, err = httpc:request_uri( local res, err = httpc:request_uri(
remote_url, remote_url,
@@ -97,12 +85,12 @@ function VerifyJwtToken(encrypted_token)
end end
function DecoderJwtToken(encrypted_token) function decodeJwtToken(encrypted_token)
local jwt_key = "0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE" local jwt_key = "0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE"
local jwt_obj = jwt:verify(jwt_key, encrypted_token) local jwt_obj = jwt:verify(jwt_key, encrypted_token)
if jwt_obj.verified == false then if jwt_obj.verified == false then
ngx.log(ngx.WARN, "Invalid token: ".. jwt_obj.reason) ngx.log(ngx.WARN, "Invalid token: " .. jwt_obj.reason)
ngx.status = ngx.HTTP_UNAUTHORIZED ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.header.content_type = "application/json; charset=utf-8" ngx.header.content_type = "application/json; charset=utf-8"
ngx.say(cjson.encode(jwt_obj)) ngx.say(cjson.encode(jwt_obj))
@@ -130,22 +118,22 @@ while true do
return return
end end
local authorization = ngx.req.get_headers().Authorization local authHeader = ngx.req.get_headers().Authorization
if authorization then if authHeader then
ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authorization) ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authHeader)
-- local htytoken = DecoderJwtToken(authorization) -- local htytoken = DecoderJwtToken(authorization)
VerifyJwtToken(authorization) verifyJwtToken(authHeader)
else else
ngx.log(ngx.ERR, 'Request header no authorization ! ') ngx.log(ngx.ERR, 'Request header no authorization ! ')
ngx.status = 500 ngx.status = 500
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end end
local authorization = ngx.req.get_headers().HtySudoerToken local sudoerToken = ngx.req.get_headers().HtySudoerToken
if authorization then if sudoerToken then
ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', authorization) ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', sudoerToken)
-- local htytoken = DecoderJwtToken(authorization) -- local htytoken = DecoderJwtToken(authorization)
VerifyJwtToken(authorization) verifyJwtToken(sudoerToken)
else else
ngx.log(ngx.ERR, 'Request header no hty sudoer token ! ') ngx.log(ngx.ERR, 'Request header no hty sudoer token ! ')
@@ -166,7 +154,7 @@ while true do
if file_name then if file_name then
file = io.open(file_dir .. "/" .. file_name, "w+") file = io.open(file_dir .. "/" .. file_name, "w+")
ngx.log(ngx.INFO, "FILENAME -> ", file_name) ngx.log(ngx.INFO, "FILENAME -> ", file_name)
if not file then if not file then
ngx.say("failed to open file ", file_name) ngx.say("failed to open file ", file_name)
return return
@@ -195,7 +183,7 @@ while true do
ngx.log(ngx.INFO, 'Authorization -> ', ngx.req.get_headers().Authorization) ngx.log(ngx.INFO, 'Authorization -> ', ngx.req.get_headers().Authorization)
ngx.log(ngx.INFO, 'HtySudoerToken -> ', ngx.req.get_headers().HtySudoerToken) ngx.log(ngx.INFO, 'HtySudoerToken -> ', ngx.req.get_headers().HtySudoerToken)
local body_text = cjson.encode({task_type = TaskTypes.UPLOAD_PICTURE, data = {images = files}}) local body_text = cjson.encode({ task_type = TaskTypes.UPLOAD_PICTURE, data = { images = files } })
ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', body_text) ngx.log(ngx.INFO, 'UPLOAD_PICTURE *body_text* ->', body_text)
local res, err = httpc:request_uri( local res, err = httpc:request_uri(
@@ -203,9 +191,9 @@ while true do
{ {
method = "POST", method = "POST",
headers = { headers = {
["Content-Type"] = "application/json", ["Content-Type"] = "application/json",
["Authorization"] = ngx.req.get_headers().Authorization, ["Authorization"] = ngx.req.get_headers().Authorization,
["HtySudoerToken"] = ngx.req.get_headers().HtySudoerToken ["HtySudoerToken"] = ngx.req.get_headers().HtySudoerToken
}, },
body = body_text, body = body_text,
} }