Skip to content

Commit

Permalink
feat(key-auth): supporting key-auth plugin to get key from query string
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdom-yzh committed Jun 29, 2021
1 parent 0c70327 commit 5721abc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
10 changes: 8 additions & 2 deletions apisix/plugins/key-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ local schema = {
type = "object",
additionalProperties = false,
properties = {
header = {
key_name = {
type = "string",
default = "apikey",
},
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/key-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/key-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ router 端配置:

| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
| ---- | ------ | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------- |
| header | string | 可选| apikey | | 设置我们从哪个 header 获取 key。 |
| key_name | string | 可选| apikey | | 设置我们从哪个 header 或 query string 获取 key。 |

## 如何启用

Expand Down
4 changes: 2 additions & 2 deletions t/plugin/key-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ GET /t



=== TEST 10: customize header
=== TEST 10: customize key_name
--- config
location /t {
content_by_lua_block {
Expand All @@ -272,7 +272,7 @@ GET /t
[[{
"plugins": {
"key-auth": {
"header": "Authorization"
"key_name": "Authorization"
}
},
"upstream": {
Expand Down

0 comments on commit 5721abc

Please sign in to comment.