From ef1e0381f88fdcf33c44832b39b3ab9b4dae49d8 Mon Sep 17 00:00:00 2001 From: wisdom Date: Sun, 27 Jun 2021 00:07:05 +0800 Subject: [PATCH 1/2] feat(key-auth): support get key from querystring --- apisix/plugins/key-auth.lua | 10 +++++++ docs/en/latest/plugins/key-auth.md | 1 + docs/zh/latest/plugins/key-auth.md | 1 + t/plugin/key-auth.t | 48 ++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua index f7b225631cfb..92a7c59535f6 100644 --- a/apisix/plugins/key-auth.lua +++ b/apisix/plugins/key-auth.lua @@ -32,6 +32,10 @@ local schema = { type = "string", default = "apikey", }, + query = { + type = "string", + default = "apikey", + }, }, } @@ -84,6 +88,12 @@ end function _M.rewrite(conf, ctx) local key = core.request.header(ctx, conf.header) + + if not key then + local uri_args = core.request.get_uri_args(ctx) or {} + key = uri_args[conf.query] + end + if not key then return 401, {message = "Missing API key found in request"} end diff --git a/docs/en/latest/plugins/key-auth.md b/docs/en/latest/plugins/key-auth.md index 6a098c13c8d3..da6dacc99d13 100644 --- a/docs/en/latest/plugins/key-auth.md +++ b/docs/en/latest/plugins/key-auth.md @@ -48,6 +48,7 @@ For route side: | Name | Type | Requirement | Default | Valid | Description | | ---- | ------ | ----------- | ------- | ----- | ---------------------------------------------------------------------------- | | header | string | optional | apikey | | the header we get the key from | +| query | string | optional | apikey | | the querystring we get the key from, which priority is lower than header | ## How To Enable diff --git a/docs/zh/latest/plugins/key-auth.md b/docs/zh/latest/plugins/key-auth.md index cf9918ba71d2..bc62ff2ecb6b 100644 --- a/docs/zh/latest/plugins/key-auth.md +++ b/docs/zh/latest/plugins/key-auth.md @@ -48,6 +48,7 @@ router 端配置: | 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | | ---- | ------ | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------- | | header | string | 可选| apikey | | 设置我们从哪个 header 获取 key。 | +| query | string | 可选 | apikey | | 设置我们从哪个 querystring 获取 key,优先级低于header | ## 如何启用 diff --git a/t/plugin/key-auth.t b/t/plugin/key-auth.t index f9ba9a24ab7a..52fd2d668c82 100644 --- a/t/plugin/key-auth.t +++ b/t/plugin/key-auth.t @@ -309,3 +309,51 @@ Authorization: auth-one hello world --- no_error_log [error] + + + +=== TEST 12: customize query string +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "key-auth": { + "query": "auth" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST13: valid consumer +--- request +GET /hello?auth=auth-one +--- response_body +hello world +--- no_error_log +[error] From ae39f7ab606ec4a688d737a2d53bfeb89b3cae34 Mon Sep 17 00:00:00 2001 From: wisdom Date: Thu, 1 Jul 2021 22:44:54 +0800 Subject: [PATCH 2/2] fix: add space between TEST and No.13 --- t/plugin/key-auth.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/key-auth.t b/t/plugin/key-auth.t index 52fd2d668c82..bb37bb029734 100644 --- a/t/plugin/key-auth.t +++ b/t/plugin/key-auth.t @@ -350,7 +350,7 @@ passed -=== TEST13: valid consumer +=== TEST 13: valid consumer --- request GET /hello?auth=auth-one --- response_body