@@ -3,6 +3,7 @@ local TokensCache = require('apicast.policy.token_introspection.tokens_cache')
3
3
local format = string.format
4
4
local test_backend_client = require (' resty.http_ng.backend.test' )
5
5
local cjson = require (' cjson' )
6
+ local resty_jwt = require " resty.jwt"
6
7
describe (" token introspection policy" , function ()
7
8
describe (" execute introspection" , function ()
8
9
local context
@@ -22,6 +23,7 @@ describe("token introspection policy", function()
22
23
test_backend = test_backend_client .new ()
23
24
ngx .var = {}
24
25
ngx .var .http_authorization = " Bearer " .. test_access_token
26
+ ngx .var .request_id = " 1234"
25
27
context = {
26
28
service = {
27
29
auth_failed_status = 403 ,
@@ -330,6 +332,93 @@ describe("token introspection policy", function()
330
332
331
333
end )
332
334
335
+ describe (' client_secret_jwt introspection auth type' , function ()
336
+ local auth_type = " client_secret_jwt"
337
+ local introspection_url = " http://example/token/introspection"
338
+ local audience = " http://example/auth/realm/basic"
339
+ local policy_config = {
340
+ auth_type = auth_type ,
341
+ introspection_url = introspection_url ,
342
+ client_id = test_client_id ,
343
+ client_secret = test_client_secret ,
344
+ client_jwt_assertion_audience = audience ,
345
+ }
346
+
347
+ describe (' success with valid token' , function ()
348
+ local token_policy = TokenIntrospection .new (policy_config )
349
+ before_each (function ()
350
+ test_backend
351
+ .expect {
352
+ url = introspection_url ,
353
+ method = ' POST' ,
354
+ }
355
+ .respond_with {
356
+ status = 200 ,
357
+ body = cjson .encode ({
358
+ active = true
359
+ })
360
+ }
361
+ token_policy .http_client .backend = test_backend
362
+ token_policy :access (context )
363
+ end )
364
+
365
+ it (' the request does not contains basic auth header' , function ()
366
+ assert .is_nil (test_backend .get_requests ()[1 ].headers [' Authorization' ])
367
+ end )
368
+
369
+ it (' the request does not contains client_secret in body' , function ()
370
+ local body = ngx .decode_args (test_backend .get_requests ()[1 ].body )
371
+ assert .is_nil (body .client_secret )
372
+ end )
373
+
374
+ it (' the request contains correct fields in body' , function ()
375
+ local body = ngx .decode_args (test_backend .get_requests ()[1 ].body )
376
+ assert .same (body .client_id , test_client_id )
377
+ assert .same (body .client_assertion_type , " urn:ietf:params:oauth:client-assertion-type:jwt-bearer" )
378
+ assert .is_not_nil (body .client_assertion )
379
+ end )
380
+
381
+ it (" has correct JWT headers" , function ()
382
+ local body = ngx .decode_args (test_backend .get_requests ()[1 ].body )
383
+ local jwt_obj = resty_jwt :load_jwt (body .client_assertion )
384
+ assert .same (jwt_obj .header .typ , " JWT" )
385
+ assert .same (jwt_obj .header .alg , " HS256" )
386
+ end )
387
+
388
+ it (" has correct JWT body" , function ()
389
+ local body = ngx .decode_args (test_backend .get_requests ()[1 ].body )
390
+ local jwt_obj = resty_jwt :load_jwt (body .client_assertion )
391
+ assert .same (jwt_obj .payload .sub , test_client_id )
392
+ assert .same (jwt_obj .payload .iss , test_client_id )
393
+ assert .truthy (jwt_obj .signature )
394
+ assert .truthy (jwt_obj .payload .jti )
395
+ assert .truthy (jwt_obj .payload .exp )
396
+ assert .is_true (jwt_obj .payload .exp > os.time ())
397
+ end )
398
+ end )
399
+
400
+ it (' failed with invalid token' , function ()
401
+ test_backend
402
+ .expect {
403
+ url = introspection_url ,
404
+ method = ' POST' ,
405
+ }
406
+ .respond_with {
407
+ status = 200 ,
408
+ body = cjson .encode ({
409
+ active = false
410
+ })
411
+ }
412
+ stub (ngx , ' say' )
413
+ stub (ngx , ' exit' )
414
+
415
+ local token_policy = TokenIntrospection .new (policy_config )
416
+ token_policy .http_client .backend = test_backend
417
+ token_policy :access (context )
418
+ assert_authentication_failed ()
419
+ end )
420
+ end )
421
+
333
422
describe (' when caching is enabled' , function ()
334
423
local introspection_url = " http://example/token/introspection"
335
424
local policy_config = {
0 commit comments