You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/09/14 13:31:36 UTC

[GitHub] [apisix] Firstsawyou commented on a change in pull request #2192: feat: add AK/SK auth plugin

Firstsawyou commented on a change in pull request #2192:
URL: https://github.com/apache/apisix/pull/2192#discussion_r487791781



##########
File path: apisix/plugins/hmac-auth.lua
##########
@@ -0,0 +1,298 @@
+--
+-- 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 ngx        = ngx
+local type       = type
+local select     = select
+local abs        = math.abs
+local ngx_time   = ngx.time
+local str_fmt    = string.format
+local ngx_re     = require("ngx.re")
+local ngx_req    = ngx.req
+local pairs      = pairs
+local ipairs     = ipairs
+local hmac_sha1  = ngx.hmac_sha1
+local escape_uri = ngx.escape_uri
+local core       = require("apisix.core")
+local hmac       = require("resty.hmac")
+local consumer   = require("apisix.consumer")
+local ngx_decode_base64 = ngx.decode_base64
+
+local SIGNATURE_KEY = "X-HMAC-SIGNATURE"
+local ALGORITHM_KEY = "X-HMAC-ALGORITHM"
+local TIMESTAMP_KEY = "X-HMAC-TIMESTAMP"
+local ACCESS_KEY    = "X-HMAC-ACCESS-KEY"
+local plugin_name   = "hmac-auth"
+
+local schema = {
+    type = "object",
+    oneOf = {
+        {
+            title = "work with consumer object",
+            properties = {
+                access_key = {type = "string", minLength = 1, maxLength = 256},
+                secret_key = {type = "string", minLength = 1, maxLength = 256},
+                algorithm = {
+                    type = "string",
+                    enum = {"hmac-sha1", "hmac-sha256", "hmac-sha512"},
+                    default = "hmac-sha256"
+                },
+                clock_skew = {
+                    type = "integer",
+                    default = 300
+                }
+            },
+            required = {"access_key", "secret_key"},
+            additionalProperties = false,
+        },
+        {
+            title = "work with route or service object",
+            properties = {},
+            additionalProperties = false,
+        }
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 2530,
+    type = 'auth',
+    name = plugin_name,
+    schema = schema,
+}
+
+local hmac_funcs = {
+    ["hmac-sha1"] = function(secret_key, message)
+        return hmac_sha1(secret_key, message)
+    end,
+    ["hmac-sha256"] = function(secret_key, message)
+        return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message)
+    end,
+    ["hmac-sha512"] = function(secret_key, message)
+        return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message)
+    end,
+}
+
+
+local function try_attr(t, ...)
+    local conf
+    local count = select('#', ...)
+    for i = 1, count do
+        local attr = select(i, ...)
+        conf = t[attr]
+        if type(conf) ~= "table" then
+            return false
+        end
+    end
+
+    return true
+end
+
+
+local create_consumer_cache
+do
+    local consumer_ids = {}
+
+    function create_consumer_cache(consumers)
+        core.table.clear(consumer_ids)
+
+        for _, consumer in ipairs(consumers.nodes) do
+            core.log.info("consumer node: ", core.json.delay_encode(consumer))
+            consumer_ids[consumer.auth_conf.access_key] = consumer
+        end
+
+        return consumer_ids
+    end
+
+end -- do
+
+
+function _M.check_schema(conf)
+    core.log.info("input conf: ", core.json.delay_encode(conf))
+
+    return core.schema.check(schema, conf)

Review comment:
       Is there no need for error handling here? For example "local ok, err = core.schema.check(schema, conf)".




----------------------------------------------------------------
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:
users@infra.apache.org