From 5721abc23cd6b0128c733077e16d76119003769f Mon Sep 17 00:00:00 2001 From: wisdom Date: Tue, 29 Jun 2021 23:59:36 +0800 Subject: [PATCH] feat(key-auth): supporting key-auth plugin to get key from query string --- apisix/plugins/key-auth.lua | 10 ++++++++-- docs/en/latest/plugins/key-auth.md | 2 +- docs/zh/latest/plugins/key-auth.md | 2 +- t/plugin/key-auth.t | 4 ++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/apisix/plugins/key-auth.lua b/apisix/plugins/key-auth.lua index f7b225631cfb..c701b8223228 100644 --- a/apisix/plugins/key-auth.lua +++ b/apisix/plugins/key-auth.lua @@ -28,7 +28,7 @@ local schema = { type = "object", additionalProperties = false, properties = { - header = { + key_name = { type = "string", default = "apikey", }, @@ -83,7 +83,13 @@ end function _M.rewrite(conf, ctx) - local key = core.request.header(ctx, conf.header) + local key = core.request.header(ctx, conf.key_name) + + if not key then + local uri_args = core.request.get_uri_args(ctx) or {} + key = uri_args[conf.key_name] + 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..82f8bb75eaed 100644 --- a/docs/en/latest/plugins/key-auth.md +++ b/docs/en/latest/plugins/key-auth.md @@ -47,7 +47,7 @@ For route side: | Name | Type | Requirement | Default | Valid | Description | | ---- | ------ | ----------- | ------- | ----- | ---------------------------------------------------------------------------- | -| header | string | optional | apikey | | the header we get the key from | +| key_name | string | optional | apikey | | the name from header or query string we get the key from | ## How To Enable diff --git a/docs/zh/latest/plugins/key-auth.md b/docs/zh/latest/plugins/key-auth.md index cf9918ba71d2..1056688599fc 100644 --- a/docs/zh/latest/plugins/key-auth.md +++ b/docs/zh/latest/plugins/key-auth.md @@ -47,7 +47,7 @@ router 端配置: | 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | | ---- | ------ | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------- | -| header | string | 可选| apikey | | 设置我们从哪个 header 获取 key。 | +| key_name | string | 可选| apikey | | 设置我们从哪个 header 或 query string 获取 key。 | ## 如何启用 diff --git a/t/plugin/key-auth.t b/t/plugin/key-auth.t index f9ba9a24ab7a..13269cc97f9c 100644 --- a/t/plugin/key-auth.t +++ b/t/plugin/key-auth.t @@ -262,7 +262,7 @@ GET /t -=== TEST 10: customize header +=== TEST 10: customize key_name --- config location /t { content_by_lua_block { @@ -272,7 +272,7 @@ GET /t [[{ "plugins": { "key-auth": { - "header": "Authorization" + "key_name": "Authorization" } }, "upstream": {