@@ -28,6 +28,7 @@ local ngx_encode_base64 = ngx.encode_base64
2828local plugin_name = " hmac-auth"
2929local ALLOWED_ALGORITHMS = {" hmac-sha1" , " hmac-sha256" , " hmac-sha512" }
3030local resty_sha256 = require (" resty.sha256" )
31+ local schema_def = require (" apisix.schema_def" )
3132local auth_utils = require (" apisix.utils.auth" )
3233
3334local 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
140136end
141137
142138
187183
188184
189185local 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
322322end
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 )
352369end
353370
354371
0 commit comments