This commit is contained in:
2021-11-27 09:44:08 +08:00
parent 75fb44a268
commit ca287ebc16
+23 -35
View File
@@ -1,8 +1,9 @@
local upload = require "resty.upload"
local uuid = require "resty.jit-uuid"
local cjson = require "cjson"
--# https://github.com/SkyLothar/lua-resty-jwt
--$ opm get SkyLothar/lua-resty-jwt
local jwt = require "resty.jwt"
local redis = require "resty.redis"
local chunk_size = 4096
local form = upload:new(chunk_size)
@@ -23,31 +24,19 @@ local files = {}
local http = require "resty.http"
local httpc = http.new()
---- Redis init
------ 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)
function cleanupString(str, remove)
local lcSubStrTab = {}
while true do
local lcPos = string.find(str,remove)
local lcPos = string.find(str, remove)
if not lcPos then
lcSubStrTab[#lcSubStrTab+1] = str
lcSubStrTab[#lcSubStrTab + 1] = str
break
end
local lcSubStr = string.sub(str,1,lcPos-1)
lcSubStrTab[#lcSubStrTab+1] = lcSubStr
str = string.sub(str,lcPos+1,#str)
local lcSubStr = string.sub(str, 1, lcPos - 1)
lcSubStrTab[#lcSubStrTab + 1] = lcSubStr
str = string.sub(str, lcPos + 1, #str)
end
local lcMergeStr =""
local lcMergeStr = ""
local lci = 1
while true do
if lcSubStrTab[lci] then
@@ -60,15 +49,14 @@ function LuaReomve(str,remove)
return lcMergeStr
end
function VerifyJwtToken(encrypted_token)
function verifyJwtToken(encrypted_token)
local http = require "resty.http"
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, 'Authorization -> ', encrypted_token)
ngx.log(ngx.INFO, 'Authorization -> ', encrypted_token)
local res, err = httpc:request_uri(
remote_url,
@@ -97,12 +85,12 @@ function VerifyJwtToken(encrypted_token)
end
function DecoderJwtToken(encrypted_token)
function decodeJwtToken(encrypted_token)
local jwt_key = "0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE0xCAFEBABE"
local jwt_obj = jwt:verify(jwt_key, encrypted_token)
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.header.content_type = "application/json; charset=utf-8"
ngx.say(cjson.encode(jwt_obj))
@@ -130,22 +118,22 @@ while true do
return
end
local authorization = ngx.req.get_headers().Authorization
if authorization then
ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authorization)
local authHeader = ngx.req.get_headers().Authorization
if authHeader then
ngx.log(ngx.INFO, 'Check request authorization Authorization -> ', authHeader)
-- local htytoken = DecoderJwtToken(authorization)
VerifyJwtToken(authorization)
verifyJwtToken(authHeader)
else
ngx.log(ngx.ERR, 'Request header no authorization ! ')
ngx.status = 500
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
local authorization = ngx.req.get_headers().HtySudoerToken
if authorization then
ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', authorization)
local sudoerToken = ngx.req.get_headers().HtySudoerToken
if sudoerToken then
ngx.log(ngx.INFO, 'Check request authorization HtySudoerToken -> ', sudoerToken)
-- local htytoken = DecoderJwtToken(authorization)
VerifyJwtToken(authorization)
verifyJwtToken(sudoerToken)
else
ngx.log(ngx.ERR, 'Request header no hty sudoer token ! ')
@@ -195,7 +183,7 @@ while true do
ngx.log(ngx.INFO, 'Authorization -> ', ngx.req.get_headers().Authorization)
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)
local res, err = httpc:request_uri(