Skip to content

Commit bd7bd46

Browse files
kilin
Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
1 parent 5214b9c commit bd7bd46

File tree

5 files changed

+24
-35
lines changed

5 files changed

+24
-35
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ install: runtime
391391
$(ENV_INSTALL) apisix/plugins/mcp/broker/*.lua $(ENV_INST_LUADIR)/apisix/plugins/mcp/broker
392392
$(ENV_INSTALL) apisix/plugins/mcp/transport/*.lua $(ENV_INST_LUADIR)/apisix/plugins/mcp/transport
393393

394+
$(ENV_INSTALL) -d $(ENV_INST_LUADIR)/apisix/plugins/jwt-auth
395+
$(ENV_INSTALL) apisix/plugins/jwt-auth/*.lua $(ENV_INST_LUADIR)/apisix/plugins/jwt-auth
396+
394397
$(ENV_INSTALL) bin/apisix $(ENV_INST_BINDIR)/apisix
395398

396399

apisix/plugins/jwt-auth.lua

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ local auth_utils = require("apisix.utils.auth")
2323
local ngx_decode_base64 = ngx.decode_base64
2424
local ngx_encode_base64 = ngx.encode_base64
2525
local ngx = ngx
26-
local ngx_time = ngx.time
2726
local sub_str = string.sub
2827
local table_insert = table.insert
2928
local table_concat = table.concat
@@ -278,19 +277,6 @@ local function get_secret(conf)
278277
end
279278

280279

281-
local function get_real_payload(key, auth_conf, payload)
282-
local real_payload = {
283-
key = key,
284-
exp = ngx_time() + auth_conf.exp
285-
}
286-
if payload then
287-
local extra_payload = core.json.decode(payload)
288-
core.table.merge(real_payload, extra_payload)
289-
end
290-
return real_payload
291-
end
292-
293-
294280
local function get_auth_secret(consumer)
295281
if not consumer.auth_conf.algorithm or consumer.auth_conf.algorithm:sub(1, 2) == "HS" then
296282
return get_secret(consumer.auth_conf)
@@ -300,25 +286,6 @@ local function get_auth_secret(consumer)
300286
end
301287

302288

303-
local function gen_jwt_header(consumer)
304-
local x5c
305-
if consumer.auth_conf.algorithm and consumer.auth_conf.algorithm:sub(1, 2) ~= "HS" then
306-
local public_key = consumer.auth_conf.public_key
307-
if not public_key then
308-
core.log.error("failed to sign jwt, err: missing public key")
309-
core.response.exit(503, "failed to sign jwt")
310-
end
311-
x5c = {public_key}
312-
end
313-
314-
return {
315-
typ = "JWT",
316-
alg = consumer.auth_conf.algorithm,
317-
x5c = x5c
318-
}
319-
end
320-
321-
322289
local function find_consumer(conf, ctx)
323290
-- fetch token and hide credentials if necessary
324291
local jwt_token, err = fetch_jwt_token(conf, ctx)

apisix/plugins/jwt-auth/parser.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
--
2+
-- Licensed to the Apache Software Foundation (ASF) under one or more
3+
-- contributor license agreements. See the NOTICE file distributed with
4+
-- this work for additional information regarding copyright ownership.
5+
-- The ASF licenses this file to You under the Apache License, Version 2.0
6+
-- (the "License"); you may not use this file except in compliance with
7+
-- the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing, software
12+
-- distributed under the License is distributed on an "AS IS" BASIS,
13+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-- See the License for the specific language governing permissions and
15+
-- limitations under the License.
16+
--
17+
118
local buffer = require "string.buffer"
219
local openssl_digest = require "resty.openssl.digest"
320
local openssl_mac = require "resty.openssl.mac"

docs/en/latest/plugins/jwt-auth.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ For Consumer/Credential:
4949
| key | string | True | | non-empty | Unique key for a Consumer. |
5050
| secret | string | False | | non-empty | Shared key used to sign and verify the JWT when the algorithm is symmetric. Required when using `HS256` or `HS512` as the algorithm. This field supports saving the value in Secret Manager using the [APISIX Secret](../terminology/secret.md) resource. |
5151
| public_key | string | True if `RS256` or `ES256` is set for the `algorithm` attribute. | | | RSA or ECDSA public key. This field supports saving the value in Secret Manager using the [APISIX Secret](../terminology/secret.md) resource. |
52-
| algorithm | string | False | HS256 | ["HS256","HS512","RS256","ES256"] | Encryption algorithm. |
52+
| algorithm | string | False | HS256 | ["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "PS256", "PS384", "PS512", "EdDSA"] | Encryption algorithm. |
5353
| exp | integer | False | 86400 | [1,...] | Expiry time of the token in seconds. |
5454
| base64_secret | boolean | False | false | | Set to true if the secret is base64 encoded. |
5555
| lifetime_grace_period | integer | False | 0 | [0,...] | Grace period in seconds. Used to account for clock skew between the server generating the JWT and the server validating the JWT. |
@@ -69,6 +69,7 @@ For Routes or Services:
6969
| anonymous_consumer | string | False | false | Anonymous Consumer name. If configured, allow anonymous users to bypass the authentication. |
7070
| store_in_ctx | boolean | False | false | Set to true will store the JWT payload in the request context (`ctx.jwt_auth_payload`). This allows lower-priority plugins that run afterwards on the same request to retrieve and use the JWT token. |
7171
| realm | string | False | jwt | The realm to include in the `WWW-Authenticate` header when authentication fails. |
72+
| claims_to_verify | array[string] | False | ["exp", "nbf"] | ["exp", "nbf"] | The claims that need to be verified in the JWT payload. |
7273

7374
You can implement `jwt-auth` with [HashiCorp Vault](https://www.vaultproject.io/) to store and fetch secrets and RSA keys pairs from its [encrypted KV engine](https://developer.hashicorp.com/vault/docs/secrets/kv) using the [APISIX Secret](../terminology/secret.md) resource.
7475

docs/zh/latest/plugins/jwt-auth.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Consumer/Credential 端:
4545
| key | string || | | 消费者的唯一密钥。 |
4646
| secret | string || | | 当使用对称算法时,用于对 JWT 进行签名和验证的共享密钥。使用 `HS256``HS512` 作为算法时必填。该字段支持使用 [APISIX Secret](../terminology/secret.md) 资源,将值保存在 Secret Manager 中。 |
4747
| public_key | string || | | RSA 或 ECDSA 公钥, `algorithm` 属性选择 `RS256``ES256` 算法时必选。该字段支持使用 [APISIX Secret](../terminology/secret.md) 资源,将值保存在 Secret Manager 中。 |
48-
| algorithm | string || "HS256" | ["HS256","HS512","RS256","ES256"] | 加密算法。 |
48+
| algorithm | string || "HS256" | ["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "PS256", "PS384", "PS512", "EdDSA"] | 加密算法。 |
4949
| exp | integer || 86400 | [1,...] | token 的超时时间。 |
5050
| base64_secret | boolean || false | | 当设置为 `true` 时,密钥为 base64 编码。 |
5151
| lifetime_grace_period | integer || 0 | [0,...] | 宽限期(以秒为单位)。用于解决生成 JWT 的服务器与验证 JWT 的服务器之间的时钟偏差。 |
@@ -64,6 +64,7 @@ Route 端:
6464
| key_claim_name | string || key | 包含用户密钥(对应消费者的密钥属性)的 JWT 声明的名称。|
6565
| anonymous_consumer | string || false | 匿名消费者名称。如果已配置,则允许匿名用户绕过身份验证。 |
6666
| store_in_ctx | boolean || false | 设置为 `true` 将会将 JWT 负载存储在请求上下文 (`ctx.jwt_auth_payload`) 中。这允许在同一请求上随后运行的低优先级插件检索和使用 JWT 令牌。 |
67+
| claims_to_verify | array[string] || ["exp", "nbf"] | ["exp", "nbf"] | 需要在 JWT 负载中验证的声明。 |
6768

6869
您可以使用 [HashiCorp Vault](https://www.vaultproject.io/) 实施 `jwt-auth`,以从其[加密的 KV 引擎](https://developer.hashicorp.com/vault/docs/secrets/kv) 使用 [APISIX Secret](../terminology/secret.md) 资源。
6970

0 commit comments

Comments
 (0)