ShaoZeMing commented on a change in pull request #3554:
URL: https://github.com/apache/apisix/pull/3554#discussion_r572644893



##########
File path: apisix/plugins/auth-hook.lua
##########
@@ -0,0 +1,337 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core = require("apisix.core")
+local json = require("apisix.core.json")
+local http = require("resty.http")
+local ck = require("resty.cookie")
+local ngx = ngx
+local rawget = rawget
+local rawset = rawset
+local setmetatable = setmetatable
+local string = string
+
+local plugin_name = "auth-hook"
+local hook_lrucache = core.lrucache.new({
+    ttl = 60, count = 1024
+})
+
+local schema = {
+    type = "object",
+    properties = {
+        auth_hook_id = { type = "string", minLength = 1, maxLength = 100, 
default = "unset" },
+        auth_hook_uri = { type = "string", minLength = 1, maxLength = 4096 },
+        auth_hook_method = {
+            type = "string",
+            default = "GET",
+            enum = { "GET", "POST" },
+        },
+        hook_headers = {
+            type = "array",
+            items = {
+                type = "string",
+                minLength = 1, maxLength = 100
+            },
+            uniqueItems = true
+        },
+        hook_args = {
+            type = "array",
+            items = {
+                type = "string",
+                minLength = 1, maxLength = 100
+            },
+            uniqueItems = true
+        },
+        hook_res_to_headers = {
+            type = "array",
+            items = {
+                type = "string",
+                minLength = 1, maxLength = 100
+            },
+            uniqueItems = true
+        },
+        hook_keepalive = { type = "boolean", default = true },
+        hook_keepalive_timeout = { type = "integer", minimum = 1000, default = 
60000 },
+        hook_keepalive_pool = { type = "integer", minimum = 1, default = 5 },
+        hook_res_to_header_prefix = { type = "string", default = "X-", 
minLength = 1, maxLength = 100 },
+        hook_cache = { type = "boolean", default = false },
+        check_termination = { type = "boolean", default = true },
+    },
+    required = { "auth_hook_uri"},
+}
+
+local _M = {
+    version = 0.1,
+    priority = 1000,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return false, err
+    end
+
+    return true
+end
+
+local function get_auth_token(ctx)
+    local token = ctx.var.http_x_auth_token
+    if token then
+        return token
+    end
+
+    token = ctx.var.http_authorization
+    if token then
+        return token
+    end
+
+    token = ctx.var.arg_auth_token
+    if token then
+        return token
+    end
+
+    local cookie, err = ck:new()
+    if not cookie then
+        return nil, err
+    end
+
+    local val, error = cookie:get("auth-token")
+    return val, error
+end
+
+local function fail_response(message, init_values)
+    local response = init_values or {}
+    response.message = message
+    return response
+end
+
+--初始化headers
+local function new_table()
+    local t = {}
+    local lt = {}
+    local _mt = {
+        __index = function(t, k)
+            return rawget(lt, string.lower(k))
+        end,
+        __newindex = function(t, k, v)
+            rawset(t, k, v)
+            rawset(lt, string.lower(k), v)
+        end,
+    }
+    return setmetatable(t, _mt)
+end
+
+
+--获取需要传输的headers
+local function request_headers(config, ctx)
+    local req_headers = new_table();
+    local headers = core.request.headers(ctx);
+    local hook_headers = config.hook_headers
+    if not hook_headers then
+        return req_headers
+    end
+    for field in pairs(hook_headers) do
+        local v = headers[field]
+        if v then
+            req_headers[field] = v
+        end
+    end
+    return req_headers;
+end
+
+
+--获取需要传输的headers
+local function res_init_headers(config)
+
+    local prefix = config.hook_res_to_header_prefix or ''
+    local hook_res_to_headers = config.hook_res_to_headers;
+
+    if type(hook_res_to_headers) ~= "table" then
+        return
+    end
+    core.request.set_header(prefix .. "auth-data", nil)
+    for field, val in pairs(hook_res_to_headers) do
+        local f = string.gsub(val, '_', '-')

Review comment:
       I just tried it, and it doesn’t work. If we remove this code, the 
upstream service layer cannot get the underlined field data.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to