You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/03/03 02:19:12 UTC

[GitHub] [apisix] spacewander commented on a change in pull request #6382: feat: add auth plugin for casdoor

spacewander commented on a change in pull request #6382:
URL: https://github.com/apache/apisix/pull/6382#discussion_r818258294



##########
File path: apisix/plugins/auth-casdoor.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local cjson = require("cjson")
+local http = require("resty.http")
+local session = require("resty.session")
+local log = core.log
+
+local plugin_name = "auth-casdoor"
+local schema = {
+    type = "object",
+    properties = {
+        endpoint_addr = {type = "string", pattern = "^[^%?]+[^/]$"},
+        client_id = {type = "string"},
+        client_secret = {type = "string"},
+        callback_url = {type = "string", pattern = "^[^%?]+[^/]$"},
+        test = {type = "boolean", default = false}
+    },
+    required = {
+        "callback_url", "endpoint_addr", "client_id", "client_secret"
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = 2559,
+    name = plugin_name,
+    schema = schema
+}
+
+local function fetch_access_token(ctx, conf)
+    local args = core.request.get_uri_args(ctx)
+    if not args or not args.code or not args.state then
+        return nil, "failed when accessing token. Invalid cofde or state"
+    end
+    local client = http.new()
+    local url = conf.endpoint_addr .. "/api/login/oauth/access_token"
+
+    if conf.test then
+        url = conf.endpoint_addr .. "/casdoor_fake_access_token_api"

Review comment:
       We can configure the endpoint directly in test?

##########
File path: t/plugin/auth-casdoor.t
##########
@@ -0,0 +1,284 @@
+#
+# 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();
+run_tests;
+

Review comment:
       Let's use
   https://github.com/apache/apisix/blob/54ffdeed114bdb4f63ff72c8a867377d122a6b84/t/plugin/clickhouse-logger.t#L28-L34
   and remove duplicate sections with
   ```
   #!/usr/bin/env python
   # coding: utf-8
   # Usage: ./simpify.py $test_filename
   import sys
   with open(sys.argv[1]) as f:
       lines = [l.rstrip() for l in f.readlines()]
   newlines = []
   for i, l in enumerate(lines):
       if ((l == "GET /t" and lines[i-1] == "--- request") or
           (l == "[error]" and lines[i-1] == "--- no_error_log")):
           newlines = newlines[:-1]
           continue
       newlines.append(l)
   res = "\n".join(newlines)
   # print(res)
   with open(sys.argv[1], "w") as f:
      f.write(res + '\n')
   ```

##########
File path: t/lib/server.lua
##########
@@ -482,6 +482,11 @@ function _M.google_logging_token()
     }))
 end
 
+function _M.casdoor_fake_access_token_api()

Review comment:
       We can put fake api in the test directly, like:
   https://github.com/apache/apisix/blob/54ffdeed114bdb4f63ff72c8a867377d122a6b84/t/plugin/clickhouse-logger.t#L36

##########
File path: docs/en/latest/plugins/auth-casdoor.md
##########
@@ -0,0 +1,93 @@
+---
+title: auth-casdoor
+---
+
+<!--
+#
+# 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.
+#
+-->
+
+## Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**Metadata**](#metadata)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+- [**Examples**](#examples)
+
+## Name
+
+`auth-casdoor` is an authorization plugin based on [Casdoor](https://casdoor.org/). Casdoor is a centralized authentication / Single-Sign-On (SSO) platform supporting OAuth 2.0, OIDC and SAML, integrated with Casbin RBAC and ABAC permission management
+
+## Attributes
+
+| Name        | Type   | Requirement | Default | Valid | Description                                                  |
+| ----------- | ------ | ----------- | ------- | ----- | ------------------------------------------------------------ |
+| casdoor_endpoint  | string | required    |         |       | The url of casdoor.             |
+| client_id | string | required    |         |       | The client id in casdoor.                          |
+| client_secret       | string | required    |         |       | The client secret in casdoor.               |
+| callback_url      | string | required    |         |       | The callback url which is used to receive state and code.                            |
+|test|boolean|not required|false| |used for unit test, no need to set it for users|
+
+## How To Enable
+
+You can enable the plugin on any route by giving out all four attributes mentioned above.
+
+### Example
+
+```shell
+curl "http://127.0.0.1:9080/apisix/admin/routes/1" -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" -X PUT -d '
+{
+  "methods": ["GET"],
+  "uri": "/anything/*",
+  "plugins": {
+    "auth-casdoor": {
+        "casdoor_endpoint":"http://localhost:8000",
+        "callback_url":"http://localhost:9080/anything/callback",
+        "client_id":"7ceb9b7fda4a9061ec1c",
+        "client_secret":"3416238e1edf915eac08b8fe345b2b95cdba7e04"
+    }
+  },
+  "upstream": {
+    "type": "roundrobin",
+    "nodes": {
+      "httpbin.org:80": 1
+    }
+  }
+}'
+
+```
+
+In this example, using apisix's admin API we created a route "/anything/*" pointed to "httpbin.org:80", and with "auth-casdoor" enabled. This route is now under authentication protection of casdoor.
+
+#### explanations about parameters of this plugin
+
+In the configuration of "auth-casdoor" plugin we can see four parameters.
+
+The first one is "callback_url". This is exactly the callback url in OAuth2. It should be emphasized that this callback url **must belong to the "uri" you specified for the route**, for example, in this example, http://localhost:9080/anything/callback obviously belong to "/anything/*" Only by this way can the visit toward callback_url can be intercepted and utilized by the plugin(so that the plugin can get the code and state in Oauth2). The logic of callback_url is implemented competely by plugin so that there is no need to modify the server implement this callback.
+
+The second parameter "callback_url" is obviously the url of Casdoor. The third and fourth parameters are "client_id" and "client_secret", which you can acquire from Casdoor when you register an app.

Review comment:
       Missing this one?

##########
File path: apisix/plugins/auth-casdoor.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+local core = require("apisix.core")
+local cjson = require("cjson")
+local http = require("resty.http")
+local session = require("resty.session")
+local log = core.log
+
+local plugin_name = "auth-casdoor"
+local schema = {
+    type = "object",
+    properties = {
+        endpoint_addr = {type = "string", pattern = "^[^%?]+[^/]$"},
+        client_id = {type = "string"},
+        client_secret = {type = "string"},
+        callback_url = {type = "string", pattern = "^[^%?]+[^/]$"},

Review comment:
       Why add `[^/]$`?




-- 
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