Skip to content

Commit f221ce9

Browse files
committed
feat(basic-auth) set generic X-Credential-Identifier (deprecating X-Credential-Username)
### Summary The PR Kong#4993 implemented `X-Credential-Identifier` for `JWT Plugin` and it was decided at time that we should add support for this less opinionated field name on other auth plugins too. This commit adds it to `Basic Auth Plugin`.
1 parent bbcf496 commit f221ce9

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

kong/plugins/basic-auth/access.lua

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ local function retrieve_credentials(header_name, conf)
6666
return username, password
6767
end
6868

69+
6970
--- Validate a credential in the Authorization header against one fetched from the database.
7071
-- @param credential The retrieved credential from the username passed in the request
7172
-- @param given_password The password as given in the Authorization header
@@ -79,6 +80,7 @@ local function validate_credentials(credential, given_password)
7980
return credential.password == digest
8081
end
8182

83+
8284
local function load_credential_into_memory(username)
8385
local credential, err = kong.db.basicauth_credentials:select_by_username(username)
8486
if err then
@@ -87,6 +89,7 @@ local function load_credential_into_memory(username)
8789
return credential
8890
end
8991

92+
9093
local function load_credential_from_db(username)
9194
if not username then
9295
return
@@ -104,7 +107,10 @@ local function load_credential_from_db(username)
104107
return credential
105108
end
106109

110+
107111
local function set_consumer(consumer, credential)
112+
kong.client.authenticate(consumer, credential)
113+
108114
local set_header = kong.service.request.set_header
109115
local clear_header = kong.service.request.clear_header
110116

@@ -126,23 +132,22 @@ local function set_consumer(consumer, credential)
126132
clear_header(constants.HEADERS.CONSUMER_USERNAME)
127133
end
128134

129-
kong.client.authenticate(consumer, credential)
135+
if credential and credential.username then
136+
set_header(constants.HEADERS.CREDENTIAL_IDENTIFIER, credential.username)
137+
set_header(constants.HEADERS.CREDENTIAL_USERNAME, credential.username)
138+
else
139+
clear_header(constants.HEADERS.CREDENTIAL_IDENTIFIER)
140+
clear_header(constants.HEADERS.CREDENTIAL_USERNAME)
141+
end
130142

131143
if credential then
132-
if credential.username then
133-
set_header(constants.HEADERS.CREDENTIAL_USERNAME, credential.username)
134-
else
135-
clear_header(constants.HEADERS.CREDENTIAL_USERNAME)
136-
end
137-
138144
clear_header(constants.HEADERS.ANONYMOUS)
139-
140145
else
141-
clear_header(constants.HEADERS.CREDENTIAL_USERNAME)
142146
set_header(constants.HEADERS.ANONYMOUS, true)
143147
end
144148
end
145149

150+
146151
local function do_authentication(conf)
147152
-- If both headers are missing, return 401
148153
if not (kong.request.get_header("authorization") or kong.request.get_header("proxy-authorization")) then
@@ -156,18 +161,18 @@ local function do_authentication(conf)
156161
end
157162

158163
local credential
159-
local given_username, given_password = retrieve_credentials("proxy-authorization", conf)
160-
if given_username then
161-
credential = load_credential_from_db(given_username)
164+
local username, password = retrieve_credentials("proxy-authorization", conf)
165+
if username then
166+
credential = load_credential_from_db(username)
162167
end
163168

164169
-- Try with the authorization header
165170
if not credential then
166-
given_username, given_password = retrieve_credentials("authorization", conf)
167-
credential = load_credential_from_db(given_username)
171+
username, password = retrieve_credentials("authorization", conf)
172+
credential = load_credential_from_db(username)
168173
end
169174

170-
if not credential or not validate_credentials(credential, given_password) then
175+
if not credential or not validate_credentials(credential, password) then
171176
return false, { status = 401, message = "Invalid authentication credentials" }
172177
end
173178

@@ -207,7 +212,7 @@ function _M.execute(conf)
207212
return kong.response.exit(500, { message = "An unexpected error occurred" })
208213
end
209214

210-
set_consumer(consumer, nil)
215+
set_consumer(consumer)
211216

212217
else
213218
return kong.response.exit(err.status, { message = err.message }, err.headers)

kong/plugins/basic-auth/handler.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
local access = require "kong.plugins.basic-auth.access"
33

44

5-
local BasicAuthHandler = {}
5+
local BasicAuthHandler = {
6+
PRIORITY = 1001,
7+
VERSION = "2.2.0",
8+
}
69

710

811
function BasicAuthHandler:access(conf)
912
access.execute(conf)
1013
end
1114

1215

13-
BasicAuthHandler.PRIORITY = 1001
14-
BasicAuthHandler.VERSION = "2.1.0"
15-
16-
1716
return BasicAuthHandler

spec/03-plugins/10-basic-auth/03-access_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ for _, strategy in helpers.each_strategy() do
215215
})
216216
local body = cjson.decode(assert.res_status(200, res))
217217
assert.equal('bob', body.headers["x-consumer-username"])
218+
assert.equal('user123', body.headers["x-credential-identifier"])
219+
assert.equal('user123', body.headers["x-credential-username"])
218220
end)
219221

220222
it("authenticates with a password containing ':'", function()
@@ -228,6 +230,8 @@ for _, strategy in helpers.each_strategy() do
228230
})
229231
local body = cjson.decode(assert.res_status(200, res))
230232
assert.equal("bob", body.headers["x-consumer-username"])
233+
assert.equal("user321", body.headers["x-credential-identifier"])
234+
assert.equal('user321', body.headers["x-credential-username"])
231235
end)
232236

233237
it("returns 401 for valid Base64 encoding", function()
@@ -273,6 +277,8 @@ for _, strategy in helpers.each_strategy() do
273277
local json = cjson.decode(body)
274278
assert.is_string(json.headers["x-consumer-id"])
275279
assert.equal("bob", json.headers["x-consumer-username"])
280+
assert.equal("bob", json.headers["x-credential-identifier"])
281+
assert.equal('bob', json.headers["x-credential-username"])
276282
end)
277283

278284
end)
@@ -323,6 +329,8 @@ for _, strategy in helpers.each_strategy() do
323329
})
324330
local body = cjson.decode(assert.res_status(200, res))
325331
assert.equal('bob', body.headers["x-consumer-username"])
332+
assert.equal('user123', body.headers["x-credential-identifier"])
333+
assert.equal('user123', body.headers["x-credential-username"])
326334
assert.is_nil(body.headers["x-anonymous-consumer"])
327335
end)
328336

@@ -337,6 +345,8 @@ for _, strategy in helpers.each_strategy() do
337345
local body = cjson.decode(assert.res_status(200, res))
338346
assert.equal('true', body.headers["x-anonymous-consumer"])
339347
assert.equal('no-body', body.headers["x-consumer-username"])
348+
assert.equal(nil, body.headers["x-credential-identifier"])
349+
assert.equal(nil, body.headers["x-credential-username"])
340350
end)
341351

342352
it("errors when anonymous user doesn't exist", function()

spec/03-plugins/10-basic-auth/05-declarative_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ for _, strategy in helpers.each_strategy() do
180180
assert.equal(consumer_def.id, json.headers["x-consumer-id"])
181181
assert.equal(consumer_def.username, json.headers["x-consumer-username"])
182182
assert.equal(consumer_def.custom_id, json.headers["x-consumer-custom-id"])
183+
assert.equal(basicauth_credential_def.username, json.headers["x-credential-identifier"])
183184
assert.equal(basicauth_credential_def.username, json.headers["x-credential-username"])
184185
end)
185186
end)

0 commit comments

Comments
 (0)