Skip to content

Commit

Permalink
feat(key-auth): support get key from querystring
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdom-yzh committed Jun 30, 2021
1 parent 0c70327 commit ef1e038
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
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 = {
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
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]



=== TEST13: valid consumer
--- request
GET /hello?auth=auth-one
--- response_body
hello world
--- no_error_log
[error]

0 comments on commit ef1e038

Please sign in to comment.