You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "dspo (via GitHub)" <gi...@apache.org> on 2024/09/23 03:54:22 UTC

[PR] (WIP) feat: remove /jwt/sign [apisix]

dspo opened a new pull request, #11597:
URL: https://github.com/apache/apisix/pull/11597

   ### Description
   
   <!-- Please include a summary of the change and which issue is fixed. -->
   <!-- Please also include relevant motivation and context. -->
   
   Fixes # (issue)
   
   ### Checklist
   
   - [ ] I have explained the need for this PR and the problem it solves
   - [ ] I have explained the changes or the new features added to this PR
   - [ ] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [ ] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] refactor: remove API `/apisix/plugin/jwt/sign` [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1772865465


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -138,14 +137,10 @@ function _M.check_schema(conf, schema_type)
     end
 
     if conf.algorithm == "RS256" or conf.algorithm == "ES256" then
-        -- Possible options are a) public key is missing
-        -- b) private key is missing
+        -- Possible options is: public key is missing

Review Comment:
   This comment doesn't seem to make much sense, it feels like it can be removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774552644


##########
docs/en/latest/plugin-develop.md:
##########
@@ -439,20 +439,21 @@ end
 
 ## register public API
 
-A plugin can register API which exposes to the public. Take jwt-auth plugin as an example, this plugin registers `GET /apisix/plugin/jwt/sign` to allow client to sign its key:
+A plugin can register API which exposes to the public. Take wolf-rbac plugin as an example, this plugin registers `POST /apisix/plugin/wolf-rbac/login` to allow a client to login and get the wolf rbac_token:

Review Comment:
   we can use `batch-request` as new example for developer



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "dspo (via GitHub)" <gi...@apache.org>.
dspo commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774284361


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -235,15 +230,10 @@ local function get_rsa_or_ecdsa_keypair(conf)
     local public_key = conf.public_key
     local private_key = conf.private_key
 
-    if public_key and private_key then
-        return public_key, private_key
-    elseif public_key and not private_key then
-        return nil, nil, "missing private key"
-    elseif not public_key and private_key then
+    if not public_key then
         return nil, nil, "missing public key"
-    else
-        return nil, nil, "public and private keys are missing"
     end
+    return public_key, private_key

Review Comment:
   To facilitate generating jwt tokens in tests, I added a `_M.gen_token` method. In the test, I can use `local gen_token = require("apisix.plugins.jwt-auth").gen_token`. `gen_token` depends on this function, so there is still a private_key.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774308816


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -235,15 +230,10 @@ local function get_rsa_or_ecdsa_keypair(conf)
     local public_key = conf.public_key
     local private_key = conf.private_key
 
-    if public_key and private_key then
-        return public_key, private_key
-    elseif public_key and not private_key then
-        return nil, nil, "missing private key"
-    elseif not public_key and private_key then
+    if not public_key then
         return nil, nil, "missing public key"
-    else
-        return nil, nil, "public and private keys are missing"
     end
+    return public_key, private_key

Review Comment:
   I think you need to completely distinguish between verifying the legality of the token in client requests and `gen_token` in tests, otherwise this function makes it very misleading that a `private_key` field is still needed in the `jwt-auth` configuration.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "dspo (via GitHub)" <gi...@apache.org>.
dspo commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774861393


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,88 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)
+    local auth_secret, err = get_secret(auth_conf)
+    if not auth_secret then
+        core.log.error("failed to sign jwt, err: ", err)
+        return nil, "failed to sign jwt: failed to get auth_secret"
     end
-
-    local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
-
-    core.log.info("consumers: ", core.json.delay_encode(consumers))
-    local consumer = consumers[key]
-    if not consumer then
-        return core.response.exit(404)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_secret,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.error("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
+    return jwt_token
+end
 
-    core.log.info("consumer: ", core.json.delay_encode(consumer))
-
-    local sign_handler = algorithm_handler(consumer, true)
-    local jwt_token = sign_handler(key, consumer, payload)
-    if jwt_token then
-        return core.response.exit(200, jwt_token)
+local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_conf.private_key,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm,
+                    x5c = {
+                        auth_conf.public_key,
+                    }
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.warn("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
-
-    return core.response.exit(404)
+    return jwt_token
 end
 
+local function get_sign_handler(algorithm)
+    if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
+        return sign_jwt_with_HS
+    elseif algorithm == "RS256" or algorithm == "ES256" then
+        return sign_jwt_with_RS256_ES256
+    end
+end
 
-function _M.api()
-    return {
-        {
-            methods = {"GET"},
-            uri = "/apisix/plugin/jwt/sign",
-            handler = gen_token,
-        }
-    }
+local function gen_token(auth_conf, payload)
+    if not auth_conf.exp then
+        auth_conf.exp = 86400
+    end
+    if not auth_conf.lifetime_grace_period then
+        auth_conf.lifetime_grace_period = 0
+    end
+    if not auth_conf.algorithm then
+        auth_conf.algorithm = "HS256"
+    end
+    local sign_handler = get_sign_handler(auth_conf.algorithm)
+    local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
+    return jwt_token, err
 end
 
+-- only for test
+_M.gen_token = gen_token

Review Comment:
   I removed the `gen_token` related code from apisix/plugins/jwt-auth.lua.
   I put `gen_token` in `t/lib/apisix/plugins/jwt-auth.lua`, following the practice of other tests of putting functions used in tests in the `t/lib` directory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "moonming (via GitHub)" <gi...@apache.org>.
moonming commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774879204


##########
t/lib/apisix/plugins/jwt-auth.lua:
##########
@@ -0,0 +1,122 @@
+--

Review Comment:
   please use https://jwt.io/#debugger-io to make your test data



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "moonming (via GitHub)" <gi...@apache.org>.
moonming commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774561763


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,88 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)
+    local auth_secret, err = get_secret(auth_conf)
+    if not auth_secret then
+        core.log.error("failed to sign jwt, err: ", err)
+        return nil, "failed to sign jwt: failed to get auth_secret"
     end
-
-    local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
-
-    core.log.info("consumers: ", core.json.delay_encode(consumers))
-    local consumer = consumers[key]
-    if not consumer then
-        return core.response.exit(404)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_secret,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.error("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
+    return jwt_token
+end
 
-    core.log.info("consumer: ", core.json.delay_encode(consumer))
-
-    local sign_handler = algorithm_handler(consumer, true)
-    local jwt_token = sign_handler(key, consumer, payload)
-    if jwt_token then
-        return core.response.exit(200, jwt_token)
+local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_conf.private_key,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm,
+                    x5c = {
+                        auth_conf.public_key,
+                    }
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.warn("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
-
-    return core.response.exit(404)
+    return jwt_token
 end
 
+local function get_sign_handler(algorithm)
+    if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
+        return sign_jwt_with_HS
+    elseif algorithm == "RS256" or algorithm == "ES256" then
+        return sign_jwt_with_RS256_ES256
+    end
+end
 
-function _M.api()
-    return {
-        {
-            methods = {"GET"},
-            uri = "/apisix/plugin/jwt/sign",
-            handler = gen_token,
-        }
-    }
+local function gen_token(auth_conf, payload)
+    if not auth_conf.exp then
+        auth_conf.exp = 86400
+    end
+    if not auth_conf.lifetime_grace_period then
+        auth_conf.lifetime_grace_period = 0
+    end
+    if not auth_conf.algorithm then
+        auth_conf.algorithm = "HS256"
+    end
+    local sign_handler = get_sign_handler(auth_conf.algorithm)
+    local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
+    return jwt_token, err
 end
 
+-- only for test
+_M.gen_token = gen_token

Review Comment:
   we don't need this.



##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,88 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)
+    local auth_secret, err = get_secret(auth_conf)
+    if not auth_secret then
+        core.log.error("failed to sign jwt, err: ", err)
+        return nil, "failed to sign jwt: failed to get auth_secret"
     end
-
-    local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
-
-    core.log.info("consumers: ", core.json.delay_encode(consumers))
-    local consumer = consumers[key]
-    if not consumer then
-        return core.response.exit(404)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_secret,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.error("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
+    return jwt_token
+end
 
-    core.log.info("consumer: ", core.json.delay_encode(consumer))
-
-    local sign_handler = algorithm_handler(consumer, true)
-    local jwt_token = sign_handler(key, consumer, payload)
-    if jwt_token then
-        return core.response.exit(200, jwt_token)
+local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)

Review Comment:
   we don't need this.



##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,88 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)

Review Comment:
   ditto



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "moonming (via GitHub)" <gi...@apache.org>.
moonming commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1776669401


##########
t/fips/jwt-auth.t:
##########
@@ -150,7 +117,7 @@ JWT token invalid: invalid jwt string
 
 
 
-=== TEST 5: add consumer with username and plugins with public_key, private_key(private_key numbits = 1024)
+=== TEST 4: add consumer with username and plugins with public_key

Review Comment:
   the same as test 1?



##########
t/fips/jwt-auth.t:
##########
@@ -122,21 +95,15 @@ passed
 
 
 
-=== TEST 4: sign/verify use RS256 algorithm(private_key numbits = 512)
+=== TEST 3: sign/verify use RS256 algorithm(private_key numbits = 512)

Review Comment:
   why we need this test case?



##########
apisix/plugins/jwt-auth.lua:
##########
@@ -90,17 +88,16 @@ local consumer_schema = {
                 {
                     properties = {
                         public_key = {type = "string"},
-                        private_key= {type = "string"},
                         algorithm = {
                             enum = {"RS256", "ES256"},
                         },
                     },
-                    required = {"public_key", "private_key"},
+                    required = {"public_key"},

Review Comment:
   no test case for it



##########
t/fips/jwt-auth.t:
##########


Review Comment:
   I don't understand this test file



##########
t/fips/jwt-auth.t:
##########
@@ -180,7 +145,7 @@ passed
 
 
 
-=== TEST 6: JWT sign and verify use RS256 algorithm(private_key numbits = 1024)
+=== TEST 5: JWT sign and verify use RS256 algorithm

Review Comment:
   ?



##########
t/perf/test_http.py:
##########


Review Comment:
   why change this file? I think you can remove the perf CI



##########
t/plugin/jwt-auth.t:
##########


Review Comment:
   there are many `private_key` in this test file, I don't know why we need it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "dspo (via GitHub)" <gi...@apache.org>.
dspo commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1776196581


##########
t/lib/apisix/plugins/jwt-auth.lua:
##########
@@ -0,0 +1,122 @@
+--

Review Comment:
   I modified the PR to use https://jwt.io/#debugger-io to make test data in most cases.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774370433


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -138,14 +137,9 @@ function _M.check_schema(conf, schema_type)
     end
 
     if conf.algorithm == "RS256" or conf.algorithm == "ES256" then

Review Comment:
   It looks like this validation is redundant with the json schema, see if it can be removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "dspo (via GitHub)" <gi...@apache.org>.
dspo commented on PR #11597:
URL: https://github.com/apache/apisix/pull/11597#issuecomment-2378311198

   @moonming @membphis please review again


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 merged PR #11597:
URL: https://github.com/apache/apisix/pull/11597


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] refactor: remove API `/apisix/plugin/jwt/sign` [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1772866943


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -235,15 +230,10 @@ local function get_rsa_or_ecdsa_keypair(conf)
     local public_key = conf.public_key
     local private_key = conf.private_key
 
-    if public_key and private_key then
-        return public_key, private_key
-    elseif public_key and not private_key then
-        return nil, nil, "missing private key"
-    elseif not public_key and private_key then
+    if not public_key then
         return nil, nil, "missing public key"
-    else
-        return nil, nil, "public and private keys are missing"
     end
+    return public_key, private_key

Review Comment:
   Why is there still a `private_key`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774368094


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,87 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)
+    local auth_secret, err = get_secret(auth_conf)
+    if not auth_secret then
+        core.log.error("failed to sign jwt, err: ", err)
+        return nil, "failed to sign jwt: failed to get auth_secret"
     end
-
-    local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
-
-    core.log.info("consumers: ", core.json.delay_encode(consumers))
-    local consumer = consumers[key]
-    if not consumer then
-        return core.response.exit(404)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_secret,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.error("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
+    return jwt_token
+end
 
-    core.log.info("consumer: ", core.json.delay_encode(consumer))
-
-    local sign_handler = algorithm_handler(consumer, true)
-    local jwt_token = sign_handler(key, consumer, payload)
-    if jwt_token then
-        return core.response.exit(200, jwt_token)
+local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_conf.private_key,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = consumer.auth_conf.algorithm,
+                    x5c = {
+                        auth_conf.public_key,
+                    }
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.warn("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
-
-    return core.response.exit(404)
+    return jwt_token
 end
 
+local function get_sign_handler(algorithm)
+    if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
+        return sign_jwt_with_HS
+    elseif algorithm == "RS256" or algorithm == "ES256" then
+        return sign_jwt_with_RS256_ES256
+    end
+end
 
-function _M.api()
-    return {
-        {
-            methods = {"GET"},
-            uri = "/apisix/plugin/jwt/sign",
-            handler = gen_token,
-        }
-    }
+local function gen_token(auth_conf, payload)
+    if not auth_conf.exp then
+        auth_conf.exp = 86400
+    end
+    if not auth_conf.lifetime_grace_period then
+        auth_conf.lifetime_grace_period = 0
+    end
+    if not auth_conf.algorithm then
+        auth_conf.algorithm = "HS256"
+    end
+    local sign_handler = get_sign_handler(auth_conf.algorithm)
+    local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
+    return jwt_token, err
 end
 
+_M.gen_token = gen_token

Review Comment:
   ```suggestion
   -- only for test
   _M.gen_token = gen_token
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on PR #11597:
URL: https://github.com/apache/apisix/pull/11597#issuecomment-2373095353

   the `wolf-rbac` is not a popular auth plugin, using it for best practices is not good.
   
   I suggest another two ways:
   1. use JWT as before, but we generate the jwt-token by other popular tools
   2. change to `basic-auth` which is popular


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "dspo (via GitHub)" <gi...@apache.org>.
dspo commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774665146


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,88 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)
+    local auth_secret, err = get_secret(auth_conf)
+    if not auth_secret then
+        core.log.error("failed to sign jwt, err: ", err)
+        return nil, "failed to sign jwt: failed to get auth_secret"
     end
-
-    local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
-
-    core.log.info("consumers: ", core.json.delay_encode(consumers))
-    local consumer = consumers[key]
-    if not consumer then
-        return core.response.exit(404)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_secret,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.error("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
+    return jwt_token
+end
 
-    core.log.info("consumer: ", core.json.delay_encode(consumer))
-
-    local sign_handler = algorithm_handler(consumer, true)
-    local jwt_token = sign_handler(key, consumer, payload)
-    if jwt_token then
-        return core.response.exit(200, jwt_token)
+local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_conf.private_key,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm,
+                    x5c = {
+                        auth_conf.public_key,
+                    }
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.warn("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
-
-    return core.response.exit(404)
+    return jwt_token
 end
 
+local function get_sign_handler(algorithm)
+    if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
+        return sign_jwt_with_HS
+    elseif algorithm == "RS256" or algorithm == "ES256" then
+        return sign_jwt_with_RS256_ES256
+    end
+end
 
-function _M.api()
-    return {
-        {
-            methods = {"GET"},
-            uri = "/apisix/plugin/jwt/sign",
-            handler = gen_token,
-        }
-    }
+local function gen_token(auth_conf, payload)
+    if not auth_conf.exp then
+        auth_conf.exp = 86400
+    end
+    if not auth_conf.lifetime_grace_period then
+        auth_conf.lifetime_grace_period = 0
+    end
+    if not auth_conf.algorithm then
+        auth_conf.algorithm = "HS256"
+    end
+    local sign_handler = get_sign_handler(auth_conf.algorithm)
+    local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
+    return jwt_token, err
 end
 
+-- only for test
+_M.gen_token = gen_token

Review Comment:
   `_M.gen_token` is only for test. It is often necessary to generate jwt tokens for requests in tests. `_M.gen_token` can be introduced in tests (`local gen_token = require(‘apisix.plugins.jwt-auth’).gen_token`). This function is completely separate from the gateway authentication jwt.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: Jwt-auth plugin no longer requires a private_key to be uploaded. [apisix]

Posted by "dspo (via GitHub)" <gi...@apache.org>.
dspo commented on code in PR #11597:
URL: https://github.com/apache/apisix/pull/11597#discussion_r1774665146


##########
apisix/plugins/jwt-auth.lua:
##########
@@ -387,52 +287,88 @@ function _M.rewrite(conf, ctx)
 end
 
 
-local function gen_token()
-    local args = core.request.get_uri_args()
-    if not args or not args.key then
-        return core.response.exit(400)
-    end
-
-    local key = args.key
-    local payload = args.payload
+local function get_real_payload(key, exp, payload)
+    local real_payload = {
+        key = key,
+        exp = ngx_time() + exp
+    }
     if payload then
-        payload = ngx.unescape_uri(payload)
+        local extra_payload = core.json.decode(payload)
+        core.table.merge(extra_payload, real_payload)
+        return extra_payload
     end
+    return real_payload
+end
 
-    local consumer_conf = consumer_mod.plugin(plugin_name)
-    if not consumer_conf then
-        return core.response.exit(404)
+local function sign_jwt_with_HS(key, auth_conf, payload)
+    local auth_secret, err = get_secret(auth_conf)
+    if not auth_secret then
+        core.log.error("failed to sign jwt, err: ", err)
+        return nil, "failed to sign jwt: failed to get auth_secret"
     end
-
-    local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
-
-    core.log.info("consumers: ", core.json.delay_encode(consumers))
-    local consumer = consumers[key]
-    if not consumer then
-        return core.response.exit(404)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_secret,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.error("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
+    return jwt_token
+end
 
-    core.log.info("consumer: ", core.json.delay_encode(consumer))
-
-    local sign_handler = algorithm_handler(consumer, true)
-    local jwt_token = sign_handler(key, consumer, payload)
-    if jwt_token then
-        return core.response.exit(200, jwt_token)
+local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
+    local ok, jwt_token = pcall(jwt.sign, _M,
+            auth_conf.private_key,
+            {
+                header = {
+                    typ = "JWT",
+                    alg = auth_conf.algorithm,
+                    x5c = {
+                        auth_conf.public_key,
+                    }
+                },
+                payload = get_real_payload(key, auth_conf.exp, payload)
+            }
+    )
+    if not ok then
+        core.log.warn("failed to sign jwt, err: ", jwt_token.reason)
+        return nil, "failed to sign jwt"
     end
-
-    return core.response.exit(404)
+    return jwt_token
 end
 
+local function get_sign_handler(algorithm)
+    if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
+        return sign_jwt_with_HS
+    elseif algorithm == "RS256" or algorithm == "ES256" then
+        return sign_jwt_with_RS256_ES256
+    end
+end
 
-function _M.api()
-    return {
-        {
-            methods = {"GET"},
-            uri = "/apisix/plugin/jwt/sign",
-            handler = gen_token,
-        }
-    }
+local function gen_token(auth_conf, payload)
+    if not auth_conf.exp then
+        auth_conf.exp = 86400
+    end
+    if not auth_conf.lifetime_grace_period then
+        auth_conf.lifetime_grace_period = 0
+    end
+    if not auth_conf.algorithm then
+        auth_conf.algorithm = "HS256"
+    end
+    local sign_handler = get_sign_handler(auth_conf.algorithm)
+    local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
+    return jwt_token, err
 end
 
+-- only for test
+_M.gen_token = gen_token

Review Comment:
   `_M.gen_token` is only for test. It is often necessary to generate jwt tokens for requests in tests. `_M.gen_token` can be introduced in tests (`local gen_token = require(‘apisix.plugins.jwt-auth’).gen_token`). This function is completely separate from the gateway authentication jwt.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org