-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: auth plugins respond with www-authenticate header with realm
#12864
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
Merged
shreemaan-abhishek
merged 6 commits into
apache:master
from
shreemaan-abhishek:feat/www-auth-realm-in-auth-plugis
Jan 8, 2026
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26ff7a3
feat: auth plugins respond with `www-authenticate` header with realm
shreemaan-abhishek cd33069
other plugins
shreemaan-abhishek cbba647
rfc compliant
shreemaan-abhishek 315f659
nit
shreemaan-abhishek ce57ee7
docs
shreemaan-abhishek 499da8c
fix
shreemaan-abhishek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,8 @@ local schema = { | |
| ldap_uri = { type = "string" }, | ||
| use_tls = { type = "boolean", default = false }, | ||
| tls_verify = { type = "boolean", default = false }, | ||
| uid = { type = "string", default = "cn" } | ||
| uid = { type = "string", default = "cn" }, | ||
| realm = { type = "string", default = "ldap" } | ||
|
||
| }, | ||
| required = {"base_dn","ldap_uri"}, | ||
| } | ||
|
|
@@ -106,7 +107,7 @@ function _M.rewrite(conf, ctx) | |
| -- 1. extract authorization from header | ||
| local auth_header = core.request.header(ctx, "Authorization") | ||
| if not auth_header then | ||
| core.response.set_header("WWW-Authenticate", "Basic realm='.'") | ||
| core.response.set_header("WWW-Authenticate", "Basic realm='" .. (conf.realm or "ldap") .. "'") | ||
| return 401, { message = "Missing authorization in request" } | ||
| end | ||
|
|
||
|
|
@@ -117,6 +118,7 @@ function _M.rewrite(conf, ctx) | |
| else | ||
| core.log.warn("nil user") | ||
| end | ||
| core.response.set_header("WWW-Authenticate", "Basic realm='" .. (conf.realm or "ldap") .. "'") | ||
| return 401, { message = "Invalid authorization in request" } | ||
| end | ||
|
|
||
|
|
@@ -136,6 +138,7 @@ function _M.rewrite(conf, ctx) | |
| local res, err = ldap.ldap_authenticate(user.username, user.password, ldapconf) | ||
| if not res then | ||
| core.log.warn("ldap-auth failed: ", err) | ||
| core.response.set_header("WWW-Authenticate", "Basic realm='" .. (conf.realm or "ldap") .. "'") | ||
| return 401, { message = "Invalid user authorization" } | ||
| end | ||
|
|
||
|
|
@@ -144,12 +147,14 @@ function _M.rewrite(conf, ctx) | |
| -- 3. Retrieve consumer for authorization plugin | ||
| local consumer_conf = consumer_mod.plugin(plugin_name) | ||
| if not consumer_conf then | ||
| core.response.set_header("WWW-Authenticate", "Basic realm='" .. (conf.realm or "ldap") .. "'") | ||
| return 401, { message = "Missing related consumer" } | ||
| end | ||
|
|
||
| local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "user_dn") | ||
| local consumer = consumers[user_dn] | ||
| if not consumer then | ||
| core.response.set_header("WWW-Authenticate", "Basic realm='" .. (conf.realm or "ldap") .. "'") | ||
| return 401, {message = "Invalid user authorization"} | ||
| end | ||
| consumer_mod.attach_consumer(ctx, consumer, consumer_conf) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| use t::APISIX 'no_plan'; | ||
|
|
||
| repeat_each(1); | ||
| no_long_string(); | ||
| no_root_location(); | ||
|
|
||
| run_tests; | ||
|
|
||
| __DATA__ | ||
|
|
||
| === TEST 1: sanity, default realm | ||
| --- 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": { | ||
| "basic-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 | ||
|
|
||
| === TEST 2: verify default realm | ||
| --- request | ||
| GET /hello | ||
| --- error_code: 401 | ||
| --- response_headers | ||
| WWW-Authenticate: Basic realm='basic' | ||
|
|
||
| === TEST 3: set custom realm | ||
| --- 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": { | ||
| "basic-auth": { | ||
| "realm": "secure-zone" | ||
| } | ||
| }, | ||
| "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 | ||
|
|
||
| === TEST 4: verify custom realm | ||
| --- request | ||
| GET /hello | ||
| --- error_code: 401 | ||
| --- response_headers | ||
| WWW-Authenticate: Basic realm='secure-zone' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| use t::APISIX 'no_plan'; | ||
|
|
||
| repeat_each(1); | ||
| no_long_string(); | ||
| no_root_location(); | ||
|
|
||
| run_tests; | ||
|
|
||
| __DATA__ | ||
|
|
||
| === TEST 1: sanity, default realm | ||
| --- 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": { | ||
| "hmac-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 | ||
|
|
||
| === TEST 2: verify default realm | ||
| --- request | ||
| GET /hello | ||
| --- error_code: 401 | ||
| --- response_headers | ||
| WWW-Authenticate: hmac realm='hmac' | ||
|
|
||
| === TEST 3: set custom realm | ||
| --- 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": { | ||
| "hmac-auth": { | ||
| "realm": "my-hmac-realm" | ||
| } | ||
| }, | ||
| "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 | ||
|
|
||
| === TEST 4: verify custom realm | ||
| --- request | ||
| GET /hello | ||
| --- error_code: 401 | ||
| --- response_headers | ||
| WWW-Authenticate: hmac realm='my-hmac-realm' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can not contain any
',"maybe we should limit it