You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by to...@apache.org on 2021/07/02 03:05:56 UTC
[apisix] branch master updated: feat(key-auth): supporting key-auth
plugin to get key from query string (#4490)
This is an automated email from the ASF dual-hosted git repository.
tokers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new de20916 feat(key-auth): supporting key-auth plugin to get key from query string (#4490)
de20916 is described below
commit de20916e5f80bd2de315be1516a90a7ee7a57fb2
Author: wisdom <wi...@gmail.com>
AuthorDate: Fri Jul 2 11:05:49 2021 +0800
feat(key-auth): supporting key-auth plugin to get key from query string (#4490)
---
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 f7b2256..92a7c59 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 6a098c1..da6dacc 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 cf9918b..bc62ff2 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 f9ba9a2..bb37bb0 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]
+
+
+
+=== TEST 13: valid consumer
+--- request
+GET /hello?auth=auth-one
+--- response_body
+hello world
+--- no_error_log
+[error]