Skip to content

Commit 44777a4

Browse files
authored
Merge branch 'master' into jwt-auth_ctx
2 parents 9acb99e + 6a84576 commit 44777a4

21 files changed

+1809
-890
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ uwsgi_temp
5656
proxy_temp
5757
fastcgi_temp
5858
client_body_temp
59+
utils/lj-releng
5960
utils/reindex
6061
*.etcd/
6162
t/lib/dubbo*/**/target/

.licenserc.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,5 @@ header:
5555
- '.luacheckrc'
5656
# Exclude file contains certificate revocation information
5757
- 't/certs/ocsp/index.txt'
58-
- 'utils/lj-releng'
5958

6059
comment: on-failure

apisix/consumer.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ function _M.plugin(plugin_name)
163163
return plugin_conf[plugin_name]
164164
end
165165

166+
function _M.consumers_conf(plugin_name)
167+
return _M.plugin(plugin_name)
168+
end
169+
166170

167171
-- attach chosen consumer to the ctx, used in auth plugin
168172
function _M.attach_consumer(ctx, consumer, conf)
@@ -208,6 +212,20 @@ function _M.consumers_kv(plugin_name, consumer_conf, key_attr)
208212
return consumers
209213
end
210214

215+
216+
function _M.find_consumer(plugin_name, key, key_value)
217+
local consumer
218+
local consumer_conf
219+
consumer_conf = _M.plugin(plugin_name)
220+
if not consumer_conf then
221+
return nil, nil, "Missing related consumer"
222+
end
223+
local consumers = _M.consumers_kv(plugin_name, consumer_conf, key)
224+
consumer = consumers[key_value]
225+
return consumer, consumer_conf
226+
end
227+
228+
211229
local function check_consumer(consumer, key)
212230
local data_valid
213231
local err
@@ -251,5 +269,33 @@ function _M.init_worker()
251269
end
252270
end
253271

272+
local function get_anonymous_consumer_from_local_cache(name)
273+
local anon_consumer_raw = consumers:get(name)
274+
275+
if not anon_consumer_raw or not anon_consumer_raw.value or
276+
not anon_consumer_raw.value.id or not anon_consumer_raw.modifiedIndex then
277+
return nil, nil, "failed to get anonymous consumer " .. name
278+
end
279+
280+
-- make structure of anon_consumer similar to that of consumer_mod.consumers_kv's response
281+
local anon_consumer = anon_consumer_raw.value
282+
anon_consumer.consumer_name = anon_consumer_raw.value.id
283+
anon_consumer.modifiedIndex = anon_consumer_raw.modifiedIndex
284+
285+
local anon_consumer_conf = {
286+
conf_version = anon_consumer_raw.modifiedIndex
287+
}
288+
289+
return anon_consumer, anon_consumer_conf
290+
end
291+
292+
293+
function _M.get_anonymous_consumer(name)
294+
local anon_consumer, anon_consumer_conf, err
295+
anon_consumer, anon_consumer_conf, err = get_anonymous_consumer_from_local_cache(name)
296+
297+
return anon_consumer, anon_consumer_conf, err
298+
end
299+
254300

255301
return _M

apisix/plugins/basic-auth.lua

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local core = require("apisix.core")
1818
local ngx = ngx
1919
local ngx_re = require("ngx.re")
2020
local consumer = require("apisix.consumer")
21+
local schema_def = require("apisix.schema_def")
2122
local auth_utils = require("apisix.utils.auth")
2223

2324
local lrucache = core.lrucache.new({
@@ -33,6 +34,7 @@ local schema = {
3334
default = false,
3435
}
3536
},
37+
anonymous_consumer = schema_def.anonymous_consumer_schema,
3638
}
3739

3840
local consumer_schema = {
@@ -122,47 +124,59 @@ local function extract_auth_header(authorization)
122124
end
123125

124126

125-
function _M.rewrite(conf, ctx)
126-
core.log.info("plugin access phase, conf: ", core.json.delay_encode(conf))
127-
128-
-- 1. extract authorization from header
127+
local function find_consumer(ctx)
129128
local auth_header = core.request.header(ctx, "Authorization")
130129
if not auth_header then
131130
core.response.set_header("WWW-Authenticate", "Basic realm='.'")
132-
return 401, { message = "Missing authorization in request" }
131+
return nil, nil, "Missing authorization in request"
133132
end
134133

135134
local username, password, err = extract_auth_header(auth_header)
136135
if err then
137136
if auth_utils.is_running_under_multi_auth(ctx) then
138-
return 401, err
137+
return nil, nil, err
139138
end
140139
core.log.warn(err)
141-
return 401, { message = "Invalid authorization in request" }
140+
return nil, nil, "Invalid authorization in request"
142141
end
143142

144-
-- 2. get user info from consumer plugin
145-
local consumer_conf = consumer.plugin(plugin_name)
146-
if not consumer_conf then
147-
return 401, { message = "Missing related consumer" }
143+
local cur_consumer, consumer_conf, err = consumer.find_consumer(plugin_name,
144+
"username", username)
145+
if not cur_consumer then
146+
err = "failed to find user: " .. (err or "invalid user")
147+
if auth_utils.is_running_under_multi_auth(ctx) then
148+
return nil, nil, err
149+
end
150+
core.log.warn(err)
151+
return nil, nil, "Invalid user authorization"
148152
end
149153

150-
local consumers = consumer.consumers_kv(plugin_name, consumer_conf, "username")
151-
152-
-- 3. check user exists
153-
local cur_consumer = consumers[username]
154-
if not cur_consumer then
155-
return 401, { message = "Invalid user authorization" }
154+
if cur_consumer.auth_conf.password ~= password then
155+
return nil, nil, "Invalid user authorization"
156156
end
157-
core.log.info("consumer: ", core.json.delay_encode(cur_consumer))
158157

158+
return cur_consumer, consumer_conf, err
159+
end
159160

160-
-- 4. check the password is correct
161-
if cur_consumer.auth_conf.password ~= password then
162-
return 401, { message = "Invalid user authorization" }
161+
162+
function _M.rewrite(conf, ctx)
163+
core.log.info("plugin access phase, conf: ", core.json.delay_encode(conf))
164+
165+
local cur_consumer, consumer_conf, err = find_consumer(ctx)
166+
if not cur_consumer then
167+
if not conf.anonymous_consumer then
168+
return 401, { message = err }
169+
end
170+
cur_consumer, consumer_conf, err = consumer.get_anonymous_consumer(conf.anonymous_consumer)
171+
if not cur_consumer then
172+
err = "basic-auth failed to authenticate the request, code: 401. error: " .. err
173+
core.log.error(err)
174+
return 401, { message = "Invalid user authorization" }
175+
end
163176
end
164177

165-
-- 5. hide `Authorization` request header if `hide_credentials` is `true`
178+
core.log.info("consumer: ", core.json.delay_encode(cur_consumer))
179+
166180
if conf.hide_credentials then
167181
core.request.set_header(ctx, "Authorization", nil)
168182
end

apisix/plugins/hmac-auth.lua

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local ngx_encode_base64 = ngx.encode_base64
2828
local plugin_name = "hmac-auth"
2929
local ALLOWED_ALGORITHMS = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}
3030
local resty_sha256 = require("resty.sha256")
31+
local schema_def = require("apisix.schema_def")
3132
local auth_utils = require("apisix.utils.auth")
3233

3334
local schema = {
@@ -62,6 +63,7 @@ local schema = {
6263
default = false,
6364
},
6465
hide_credentials = {type = "boolean", default = false},
66+
anonymous_consumer = schema_def.anonymous_consumer_schema,
6567
},
6668
}
6769

@@ -124,19 +126,13 @@ local function get_consumer(key_id)
124126
return nil, "missing key_id"
125127
end
126128

127-
local consumer_conf = consumer.plugin(plugin_name)
128-
if not consumer_conf then
129-
return nil, "Missing related consumer"
129+
local cur_consumer, _, err = consumer.find_consumer(plugin_name, "key_id", key_id)
130+
if not cur_consumer then
131+
return nil, err or "Invalid key_id"
130132
end
133+
core.log.info("consumer: ", core.json.delay_encode(consumer, true))
131134

132-
local consumers = consumer.consumers_kv(plugin_name, consumer_conf, "key_id")
133-
local consumer = consumers[key_id]
134-
if not consumer then
135-
return nil, "Invalid key_id"
136-
end
137-
core.log.info("consumer: ", core.json.delay_encode(consumer))
138-
139-
return consumer
135+
return cur_consumer
140136
end
141137

142138

@@ -187,6 +183,10 @@ end
187183

188184

189185
local function validate(ctx, conf, params)
186+
if not params then
187+
return nil
188+
end
189+
190190
if not params.keyId or not params.signature then
191191
return nil, "keyId or signature missing"
192192
end
@@ -321,34 +321,51 @@ local function retrieve_hmac_fields(ctx)
321321
return hmac_params
322322
end
323323

324-
325-
function _M.rewrite(conf, ctx)
324+
local function find_consumer(conf, ctx)
326325
local params,err = retrieve_hmac_fields(ctx)
327326
if err then
328-
err = "client request can't be validated: " .. err
329-
if auth_utils.is_running_under_multi_auth(ctx) then
330-
return 401, err
327+
if not auth_utils.is_running_under_multi_auth(ctx) then
328+
core.log.warn("client request can't be validated: ", err)
331329
end
332-
core.log.warn(err)
333-
return 401, {message = err}
330+
return nil, nil, "client request can't be validated: " .. err
334331
end
335332

336-
if conf.hide_credentials then
337-
core.request.set_header("Authorization", nil)
338-
end
339333
local validated_consumer, err = validate(ctx, conf, params)
340334
if not validated_consumer then
341335
err = "client request can't be validated: " .. (err or "Invalid signature")
342336
if auth_utils.is_running_under_multi_auth(ctx) then
343-
return 401, err
337+
return nil, nil, err
344338
end
345339
core.log.warn(err)
346-
return 401, {message = "client request can't be validated"}
340+
return nil, nil, "client request can't be validated"
341+
end
342+
343+
local consumers_conf = consumer.consumers_conf(plugin_name)
344+
return validated_consumer, consumers_conf, err
345+
end
346+
347+
348+
function _M.rewrite(conf, ctx)
349+
local cur_consumer, consumers_conf, err = find_consumer(conf, ctx)
350+
if not cur_consumer then
351+
if not conf.anonymous_consumer then
352+
return 401, { message = err }
353+
end
354+
cur_consumer, consumers_conf, err = consumer.get_anonymous_consumer(conf.anonymous_consumer)
355+
if not cur_consumer then
356+
if auth_utils.is_running_under_multi_auth(ctx) then
357+
return 401, err
358+
end
359+
core.log.error(err)
360+
return 401, { message = "Invalid user authorization" }
361+
end
362+
end
363+
364+
if conf.hide_credentials then
365+
core.request.set_header("Authorization", nil)
347366
end
348367

349-
local consumer_conf = consumer.plugin(plugin_name)
350-
consumer.attach_consumer(ctx, validated_consumer, consumer_conf)
351-
core.log.info("hit hmac-auth rewrite")
368+
consumer.attach_consumer(ctx, cur_consumer, consumers_conf)
352369
end
353370

354371

0 commit comments

Comments
 (0)