-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(jwt-auth): support configuring key_claim_name
#11772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shreemaan-abhishek
merged 7 commits into
apache:master
from
shreemaan-abhishek:make-key-claim-name-configurable
Nov 27, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
42254a4
feat(jwt-auth): support configuring `key_claim_name`
shreemaan-abhishek c7dd557
lint
shreemaan-abhishek 064c9c1
Merge branch 'master' of github.com:apache/apisix into make-key-claim…
shreemaan-abhishek 1e3a554
clean
shreemaan-abhishek 1371e7d
add doc
shreemaan-abhishek 10d83e3
add min length
shreemaan-abhishek acb6824
minlenght should be number
shreemaan-abhishek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| use t::APISIX 'no_plan'; | ||
|
|
||
| repeat_each(1); | ||
| no_long_string(); | ||
| no_root_location(); | ||
| no_shuffle(); | ||
|
|
||
| add_block_preprocessor(sub { | ||
| my ($block) = @_; | ||
|
|
||
| if ((!defined $block->error_log) && (!defined $block->no_error_log)) { | ||
| $block->set_value("no_error_log", "[error]"); | ||
| } | ||
|
|
||
| if (!defined $block->request) { | ||
| $block->set_value("request", "GET /t"); | ||
| if (!$block->response_body) { | ||
| $block->set_value("response_body", "passed\n"); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| run_tests; | ||
|
|
||
| __DATA__ | ||
|
|
||
| === TEST 1: add consumer with username and plugins | ||
| --- config | ||
| location /t { | ||
| content_by_lua_block { | ||
| local t = require("lib.test_admin").test | ||
| local code, body = t('/apisix/admin/consumers', | ||
| ngx.HTTP_PUT, | ||
| [[{ | ||
| "username": "jack", | ||
| "plugins": { | ||
| "jwt-auth": { | ||
| "key": "user-key", | ||
| "secret": "my-secret-key" | ||
| } | ||
| } | ||
| }]] | ||
| ) | ||
|
|
||
| if code >= 300 then | ||
| ngx.status = code | ||
| end | ||
| ngx.say(body) | ||
| } | ||
| } | ||
| --- response_body | ||
| passed | ||
|
|
||
|
|
||
|
|
||
| === TEST 2: enable jwt auth plugin using admin api | ||
| --- config | ||
| location /t { | ||
| content_by_lua_block { | ||
| local t = require("lib.test_admin").test | ||
| local code, body = t('/apisix/admin/routes/1', | ||
| ngx.HTTP_PUT, | ||
| [[{ | ||
| "plugins": { | ||
| "jwt-auth": { | ||
| "key": "user-key", | ||
| "secret": "my-secret-key", | ||
| "key_claim_name": "iss" | ||
| } | ||
| }, | ||
| "upstream": { | ||
| "nodes": { | ||
| "127.0.0.1:1980": 1 | ||
| }, | ||
| "type": "roundrobin" | ||
| }, | ||
| "uri": "/hello" | ||
| }]] | ||
| ) | ||
|
|
||
| if code >= 300 then | ||
| ngx.status = code | ||
| end | ||
| ngx.say(body) | ||
| } | ||
| } | ||
| --- response_body | ||
| passed | ||
|
|
||
|
|
||
|
|
||
| === TEST 3: verify (in header) | ||
| --- config | ||
| location /t { | ||
| content_by_lua_block { | ||
| local function gen_token(payload) | ||
| local buffer = require "string.buffer" | ||
| local openssl_mac = require "resty.openssl.mac" | ||
|
|
||
| local base64 = require "ngx.base64" | ||
| local base64_encode = base64.encode_base64url | ||
|
|
||
| local json = require("cjson") | ||
|
|
||
| local function sign(data, key) | ||
| return openssl_mac.new(key, "HMAC", nil, "sha256"):final(data) | ||
| end | ||
| local header = { typ = "JWT", alg = "HS256" } | ||
| local buf = buffer.new() | ||
|
|
||
| buf:put(base64_encode(json.encode(header))):put("."):put(base64_encode(json.encode(payload))) | ||
|
|
||
| local ok, signature = pcall(sign, buf:tostring(), "my-secret-key") | ||
| if not ok then | ||
| return nil, signature | ||
| end | ||
|
|
||
| buf:put("."):put(base64_encode(signature)) | ||
|
|
||
| return buf:get() | ||
| end | ||
|
|
||
| local payload = { | ||
| sub = "1234567890", | ||
| iss = "user-key", | ||
| exp = 9916239022 | ||
| } | ||
|
|
||
| local token = gen_token(payload) | ||
|
|
||
| local http = require("resty.http") | ||
| local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello" | ||
| local opt = {method = "POST", headers = {["Authorization"] = "Bearer " .. token}} | ||
| local httpc = http.new() | ||
| local res = httpc:request_uri(uri, opt) | ||
| assert(res.status == 200) | ||
|
|
||
| ngx.print(res.body) | ||
| } | ||
| } | ||
| --- request | ||
| GET /t | ||
| --- more_headers | ||
| --- response_body | ||
| hello world |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a limitation about the length, the minimum length should be 1
empty string isn’t acceptable