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 2022/03/04 12:14:42 UTC

[GitHub] [apisix] Dog-Lee commented on a change in pull request #6512: feat: support google reCAPTCHA

Dog-Lee commented on a change in pull request #6512:
URL: https://github.com/apache/apisix/pull/6512#discussion_r819520558



##########
File path: apisix/plugins/recaptcha.lua
##########
@@ -0,0 +1,122 @@
+local radix = require("resty.radixtree")
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local schema = {
+    type = "object",
+    properties = {
+        recaptcha_secret_key = { type = "string" },
+        apis = {
+            type = "array",
+            items = {
+                type = "object",
+                properties = {
+                    path = { type = "string" },
+                    methods = { type = "array", items = { type = "string" }, minItems = 1 },
+                    param_from = { type = "string", default = "header", enum = { "header", "query" } },
+                    param_name = { type = "string", default = "captcha" },
+                }
+            },
+            minItems = 1
+        },
+        response = {
+            type = "object",
+            properties = {
+                content_type = { type = "string", default = "application/json; charset=utf-8" },
+                status_code = { type = "number", default = 400 },
+                body = { type = "string", default = '{"message": "invalid captcha"}' }
+            }
+        },
+
+    },
+    additionalProperties = false,
+    required = { "recaptcha_secret_key" },
+}
+
+local recaptcha_url = "https://www.recaptcha.net"
+
+local _M = {
+    version = 0.1,
+    priority = 700,
+    name = "recaptcha",
+    schema = schema,
+}
+
+function _M.check_schema(conf, schema_type)
+    return core.schema.check(schema, conf)
+end
+
+local function build_radixtree(apis)
+    local items = {}
+    for _, api in ipairs(apis) do
+        local item = {
+            paths = { api.path },
+            methods = api.methods,
+            metadata = api,
+        }
+        table.insert(items, item)
+    end
+    return radix.new(items)
+end
+
+local function find_api(request, apis)
+    local rx = build_radixtree(apis)
+    return rx:match(request.path, { method = request.method })
+end

Review comment:
       Good question. 
   This is designed on purpose. In enterprise architecture, you may have hundreds of microservice in the backend. the api gateway dispatches the requests to those services based on the prefix of URL.
   
   For example
   ```
   /user/* -> user service
   /book/* -> book service
   ...
   ```
   Let's say if every service has one or two API need to be verified by recaptcha. If we try to add recaptcha plugin in every service/route, I think it will end up a mess, and it's not good for maintaining the plugin(or secret key in the plugin) in one place. 
   
   perhaps we can add an option property to verify every request once the plugin is executed. just like the other plugins. 
   




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org