Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(key-auth): supporting key-auth plugin to get key from query string #4490

Merged
merged 2 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apisix/plugins/key-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ local schema = {
type = "string",
default = "apikey",
},
query = {
tokers marked this conversation as resolved.
Show resolved Hide resolved
type = "string",
default = "apikey",
},
},
}

Expand Down Expand Up @@ -84,6 +88,12 @@ end

function _M.rewrite(conf, ctx)
local key = core.request.header(ctx, conf.header)

if not key then
tokers marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
1 change: 1 addition & 0 deletions docs/en/latest/plugins/key-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/zh/latest/plugins/key-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ router 端配置:
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
| ---- | ------ | ------ | ------ | ------ | ------------------------------------------------------------------------------------------------------------- |
| header | string | 可选| apikey | | 设置我们从哪个 header 获取 key。 |
| query | string | 可选 | apikey | | 设置我们从哪个 querystring 获取 key,优先级低于header |

## 如何启用

Expand Down
48 changes: 48 additions & 0 deletions t/plugin/key-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -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]