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/09 13:57:07 UTC

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

shuaijinchao commented on a change in pull request #6512:
URL: https://github.com/apache/apisix/pull/6512#discussion_r822673629



##########
File path: apisix/plugins/recaptcha.lua
##########
@@ -0,0 +1,147 @@
+--
+-- 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 radix = require("resty.radixtree")
+local core = require("apisix.core")
+local http = require("resty.http")
+
+local ipairs = ipairs
+local table = table
+
+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
+
+local function retrieve_captcha(ctx, api)
+    local captcha
+    if api.param_from == "header" then
+        captcha = core.request.header(ctx, api.param_name)
+    elseif api.param_from == "query" then
+        local uri_args = core.request.get_uri_args(ctx) or {}
+        captcha = uri_args[api.param_name]
+    end
+    return captcha
+end
+
+function _M.access(conf, ctx)
+    local path = ctx.var.uri
+    local method = core.request.get_method()
+
+    core.log.debug("path: ", path, ", method: ", method, ", conf: ", core.json.encode(conf))
+
+    local api = find_api({ path = path, method = method }, conf.apis)
+    if not api then
+        return
+    end
+
+    core.log.debug("api found: ", core.json.encode(api))
+
+    local invalid_captcha = true
+    local captcha = retrieve_captcha(ctx, api)
+    if captcha ~= nil and captcha ~= "" then
+        local httpc = http.new()
+        local secret = conf.recaptcha_secret_key
+        local remote_ip = core.request.get_remote_client_ip(ctx)
+        local res, err = httpc:request_uri(recaptcha_url .. "/recaptcha/api/siteverify", {
+            method = "POST",
+            body = "secret=" .. secret .. "&response=" .. captcha .. "&remoteip=" .. remote_ip,
+            headers = {
+                ["Content-Type"] = "application/x-www-form-urlencoded",
+            },
+            ssl_verify = false
+        })
+        if err then
+            core.log.error("request failed: ", err)
+            return 500

Review comment:
       > I think here is more like an Internal server error(500) rather than Service Unavailable(503). The recaptcha layer is part of the request, and 500 seems will be more reasonable for the user.
   > 
   hi @Dog-Lee When responding with a 500 error, it is intercepted by Nginx's default error_page, so a 503 response would be more appropriate.
   Check it out here https://github.com/apache/apisix/blob/c84158c40f1bc3eeb0683310a5ae5e04cce275a9/apisix/cli/ngx_tpl.lua#L315




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