@@ -31,7 +31,7 @@ log_level('warn');
31
31
32
32
repeat_each(2);
33
33
34
- plan tests => repeat_each() * (blocks() * 8) - 4 ;
34
+ plan tests => repeat_each() * (blocks() * 9) + 10 ;
35
35
36
36
my $pwd = cwd();
37
37
@@ -271,3 +271,109 @@ __DATA__
271
271
[200,200,403,403,403,403]
272
272
--- no_error_log
273
273
[error]
274
+
275
+ === TEST 6: test HMAC signature validation and generation
276
+ --- http_config eval: $::HttpConfig
277
+ --- config
278
+ error_log ../test-logs/hmacGenericSignatureValidator_test6_error.log debug;
279
+ include ../../api-gateway/api_key_service.conf;
280
+ include ../../api-gateway/default_validators.conf;
281
+ # customize error response
282
+ set $validator_custom_error_responses '{
283
+ "MISSING_KEY" : { "http_status" : 403, "error_code" : 403000, "message" : "while (1) {}{\\"code\\":1033,\\"description\\":\\"Developer key missing or invalid\\"}" },
284
+ "INVALID_KEY" : { "http_status" : 403, "error_code" : 403003, "message" : "while (1) {}{\\"code\\":1033,\\"description\\":\\"Developer key missing or invalid\\"}" },
285
+ "INVALID_SIGNATURE" : { "http_status" : 403, "error_code" : 403030, "message" : "while (1) {}{\\"code\\":1033,\\"description\\":\\"Call signature missing or invalid\\"}" },
286
+ "INVALID_SIGNATURE" : { "http_status" : 403, "error_code" : 403033, "message" : "while (1) {}{\\"code\\":1033,\\"description\\":\\"Call signature missing or invalid\\"}" }
287
+ }';
288
+
289
+ location /validate-and-sign {
290
+ set $service_id 123456;
291
+
292
+ set $api_key $arg_api_key;
293
+ set_if_empty $api_key $http_x_api_key;
294
+
295
+ set_by_lua $hmac_source_string 'return string.lower(ngx.var.request_method .. ngx.var.uri .. ngx.var.api_key)';
296
+
297
+ set $hmac_target_string $arg_api_signature;
298
+ set $hmac_method sha1;
299
+
300
+ # Generate signature
301
+ set_by_lua $hmac_sign_source_string 'return string.lower(ngx.var.request_method .. ngx.var.uri)';
302
+ set $hmac_sign_method sha1;
303
+
304
+ set $validate_api_key on;
305
+ set $validate_hmac_signature on;
306
+ set $generate_hmac_signature on;
307
+ set $generated_digest "-";
308
+
309
+ access_by_lua "ngx.apiGateway.validation.validateRequest()";
310
+ content_by_lua 'ngx.say(ngx.var.generated_digest)';
311
+ }
312
+
313
+ --- pipelined_requests eval
314
+ [
315
+ "POST /cache/api_key?key=sZ28nvYnStSUS2dSzedgnwkJtUdLkNdR&service_id=123456&secret=mO2AIfdUQeQFiGQq",
316
+ "GET /validate-and-sign?api_key=sZ28nvYnStSUS2dSzedgnwkJtUdLkNdR&api_signature=XY1Y6BPr91I2gDbYmcahwA3mWzE=",
317
+ # negative scenario: missing api-key
318
+ "GET /validate-and-sign",
319
+ # negative scenario: api_key present but invalid
320
+ "GET /validate-and-sign?api_key=WRONG_KEY_WHICH_DOES_NOT_EXIST",
321
+ # negative scenario: api_key is valid but the signature is not
322
+ "GET /validate-and-sign?api_key=sZ28nvYnStSUS2dSzedgnwkJtUdLkNdR&api_signature=WRONG_SIGNATURE",
323
+ # negative scenario: api_key is valid , missing signature
324
+ "GET /validate-and-sign?api_key=sZ28nvYnStSUS2dSzedgnwkJtUdLkNdR"
325
+ ]
326
+ --- response_body eval
327
+ [
328
+ "+OK\r\n",
329
+ "5XPFapKr91/nLn3F+tzfkvSuE4A=\n",
330
+ 'while (1) {}{"code":1033,"description":"Developer key missing or invalid"}' . "\n",
331
+ 'while (1) {}{"code":1033,"description":"Developer key missing or invalid"}' . "\n",
332
+ 'while (1) {}{"code":1033,"description":"Call signature missing or invalid"}' . "\n",
333
+ 'while (1) {}{"code":1033,"description":"Call signature missing or invalid"}' . "\n"
334
+ ]
335
+ --- error_code_like eval
336
+ [200,200,403,403,403,403]
337
+ --- no_error_log
338
+ [error]
339
+
340
+ === TEST 7: test HMAC digest in isolation
341
+ --- http_config eval: $::HttpConfig
342
+ --- config
343
+ error_log ../test-logs/hmacGenericSignatureValidator_test7_error.log debug;
344
+ include ../../api-gateway/api_key_service.conf;
345
+ include ../../api-gateway/default_validators.conf;
346
+
347
+ location /generate_digest {
348
+ # Generate signature
349
+ set $hmac_sign_source_string $arg_source;
350
+ set $hmac_sign_secret $arg_secret;
351
+ set $hmac_sign_method sha1;
352
+
353
+ set $generate_hmac_signature on;
354
+ set $generated_digest "-";
355
+
356
+ access_by_lua "ngx.apiGateway.validation.validateRequest()";
357
+ content_by_lua 'ngx.say(ngx.var.generated_digest)';
358
+ }
359
+ --- pipelined_requests eval
360
+ [
361
+ "GET /generate_digest?source=SignThisLikeYouOwnIt&secret=mO2AIfdUQeQFiGQq",
362
+ "GET /generate_digest?source=SignThisLikeYouOwnIt",
363
+ "GET /generate_digest?secret=mO2AIfdUQeQFiGQq",
364
+ "GET /generate_digest"
365
+ ]
366
+ --- response_body eval
367
+ [
368
+ "DYUCC7E/MCyn+aNcCb5EhM7OPDE=\n",
369
+ '{"error_code":"400002","message"="Missing digest secret"}
370
+ ',
371
+ '{"error_code":"400001","message"="Missing digest source"}
372
+ ',
373
+ '{"error_code":"400001","message"="Missing digest source"}
374
+ '
375
+ ]
376
+ --- error_code_like eval
377
+ [200, 400, 400, 400]
378
+ --- no_error_log
379
+ [error]
0 commit comments