From 92bd0257ace006260366d96dd2171199ab8ef260 Mon Sep 17 00:00:00 2001 From: Michele Righi Date: Tue, 4 Jun 2024 17:01:42 +0200 Subject: [PATCH] Update jwt-auth4.t + Removed external httpbin upstream in favor of local endpoint (/hello); + Cleaned code; --- t/plugin/jwt-auth4.t | 73 +++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/t/plugin/jwt-auth4.t b/t/plugin/jwt-auth4.t index b5a825a3cd29..4eddaf92f021 100644 --- a/t/plugin/jwt-auth4.t +++ b/t/plugin/jwt-auth4.t @@ -123,58 +123,70 @@ safe-jws -=== TEST 2: verify that key_claim_name can be used to validate the Consumer JWT with a different -claim than 'key' +=== TEST 2: enable jwt auth plugin (with custom key_claim_name) using admin api --- config location /t { content_by_lua_block { - local core = require("apisix.core") local t = require("lib.test_admin").test - - -- prepare consumer - local csm_code, csm_body = t('/apisix/admin/consumers', + local code, body = t('/apisix/admin/routes/4', ngx.HTTP_PUT, [[{ - "username": "mike", "plugins": { "jwt-auth": { "key": "custom-user-key", - "secret": "custom-secret-key" + "secret": "custom-secret-key", + "key_claim_name": "iss" } - } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" }]] - ) + ) - if csm_code >= 300 then - ngx.status = csm_code - ngx.say(csm_body) + if code >= 300 then + ngx.status = code + ngx.say(body) return end - -- prepare route - local rot_code, rot_body = t('/apisix/admin/routes/3', + ngx.say(body) + } + } +--- response_body +passed + + + +=== TEST 3: verify that key_claim_name can be used to validate the Consumer JWT +with a different claim than 'key' +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin").test + + -- prepare consumer with a custom key claim name + local csm_code, csm_body = t('/apisix/admin/consumers', ngx.HTTP_PUT, [[{ - "uri": "/anything/*", - "upstream": { - "type": "roundrobin", - "nodes": { - "httpbin.org:80": 1 - } - }, + "username": "mike", "plugins": { "jwt-auth": { "key": "custom-user-key", - "secret": "custom-secret-key", - "key_claim_name": "iss" + "secret": "custom-secret-key" } } }]] ) - if rot_code >= 300 then - ngx.status = rot_code - ngx.say("FAILED: Test /verify route creation - Body:" .. rot_body) + if csm_code >= 300 then + ngx.status = csm_code + ngx.say(csm_body) return end @@ -185,19 +197,18 @@ claim than 'key' if sign_code > 200 then ngx.status = sign_code - ngx.say("FAILED: Test sign - Token: " .. token .. "\nBody: " .. sign_body) + ngx.say(sign_body) return end -- verify JWT using the custom key_claim_name - ngx.req.set_header("Authorization", "Bearer " .. token) - local ver_code, ver_body, ver_extra = t('/anything/get?jwt=' .. token, + local ver_code, ver_body = t('/hello?jwt=' .. token, ngx.HTTP_GET ) if ver_code > 200 then ngx.status = ver_code - ngx.say("FAILED: Test verify - Token: " .. token .. "\nBody: " .. ver_body .. "\nExtra: " .. ver_extra) + ngx.say(ver_body) return end