spacewander commented on a change in pull request #6382:
URL: https://github.com/apache/apisix/pull/6382#discussion_r828660685
##########
File path: conf/config-default.yaml
##########
@@ -340,6 +340,7 @@ plugins: # plugin list (sorted by
priority)
- request-validation # priority: 2800
- openid-connect # priority: 2599
- authz-casbin # priority: 2560
+ - authz-casdoor # priority: 2559
Review comment:
```suggestion
- authz-casdoor # priority: 2559
```
##########
File path: t/plugin/authz-casdoor.t
##########
@@ -0,0 +1,338 @@
+#
+# 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';
Review comment:
Let's add tests to cover:
1. `return nil, "invalid state"`
2.
```
local access_token, err =
fetch_access_token(ctx, conf, state_in_session)
if err then
core.log.error(err)
return 503, err
end
```
##########
File path: docs/en/latest/plugins/authz-casdoor.md
##########
@@ -0,0 +1,94 @@
+---
+title: authz-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
Review comment:
Let's remove the Summary like other plugins now
##########
File path: docs/en/latest/plugins/authz-casdoor.md
##########
@@ -0,0 +1,94 @@
+---
+title: authz-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
Review comment:
```suggestion
## Description
```
##########
File path: apisix/plugins/authz-casdoor.lua
##########
@@ -0,0 +1,150 @@
+--
+-- 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 http = require("resty.http")
+local session = require("resty.session")
+local ngx = ngx
+local rand = math.random
+
+local plugin_name = "authz-casdoor"
+local schema = {
+ type = "object",
+ properties = {
+ -- Note: endpoint_addr and callback_url should not end with '/'
+ endpoint_addr = {type = "string", pattern = "^[^%?]+[^/]$"},
+ client_id = {type = "string"},
+ client_secret = {type = "string"},
+ callback_url = {type = "string", pattern = "^[^%?]+[^/]$"}
+ },
+ 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, state_in_session)
+ 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 code or state"
+ end
+ if not args.state == state_in_session then
+ return nil, "invalid state"
+ end
+ local client = http.new()
+ local url = conf.endpoint_addr .. "/api/login/oauth/access_token"
+
+ local res, err = client:request_uri(url, {
+ method = "POST",
+ query = {
+ code = args.code,
+ grant_type = "authorization_code",
+ client_id = conf.client_id,
+ client_secret = conf.client_secret
+ }
+ })
+
+ if not res then return nil, err end
+ local data, err = core.json.decode(res.body)
+
+ if err or not data then
+ err = "failed to parse casdoor response data: " .. err
+ return nil, err
+ end
+
+ if not data.access_token then
+ return nil, "failed when accessing token: no access_token contained"
+ end
+ if not data.expires_in or data.expires_in == 0 then
+ return nil, "failed when accessing token: invalid access_token"
+ end
+
+ return data.access_token, nil
+end
+
+function _M.check_schema(conf) return core.schema.check(schema, conf) end
+
+function _M.access(conf, ctx)
+ local current_uri = ctx.var.uri
+ local session_obj_read, session_present = session.open()
+ -- step 1: check whether hits the callback
+ local m, err = ngx.re.match(conf.callback_url, ".+//[^/]+(/.*)")
+ if err or not m then
+ core.log.error(err)
+ return 503, err
+ end
+ local real_callback_url = m[1]
+ if current_uri == real_callback_url then
+ if not session_present then
+ err = "no session found"
+ core.log.error(err)
+ return 503, err
+ end
+ local state_in_session = session_obj_read.data.state
+ if not state_in_session then
+ err = "no state found in session"
+ core.log.error(err)
+ return 503, err
+ end
+ local access_token, err =
+ fetch_access_token(ctx, conf, state_in_session)
+ if err then
+ core.log.error(err)
+ return 503, err
+ end
+ if access_token then
+ local original_url = session_obj_read.data.original_uri
+ if not original_url then
+ err = "no original_url found in session"
+ core.log.error(err)
+ return 503, err
+ end
+ local session_obj_write = session.start()
+ session_obj_write.data.access_token = access_token
+ session_obj_write:save()
+ core.response.set_header("Location", original_url)
+ return 302
+ else
+ return 503, err
Review comment:
err is nil in this branch?
##########
File path: apisix/plugins/authz-casdoor.lua
##########
@@ -0,0 +1,150 @@
+--
+-- 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 http = require("resty.http")
+local session = require("resty.session")
+local ngx = ngx
+local rand = math.random
+
+local plugin_name = "authz-casdoor"
+local schema = {
+ type = "object",
+ properties = {
+ -- Note: endpoint_addr and callback_url should not end with '/'
+ endpoint_addr = {type = "string", pattern = "^[^%?]+[^/]$"},
+ client_id = {type = "string"},
+ client_secret = {type = "string"},
+ callback_url = {type = "string", pattern = "^[^%?]+[^/]$"}
+ },
+ 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, state_in_session)
+ 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 code or state"
+ end
+ if not args.state == state_in_session then
+ return nil, "invalid state"
+ end
+ local client = http.new()
+ local url = conf.endpoint_addr .. "/api/login/oauth/access_token"
+
+ local res, err = client:request_uri(url, {
+ method = "POST",
+ query = {
+ code = args.code,
+ grant_type = "authorization_code",
+ client_id = conf.client_id,
+ client_secret = conf.client_secret
+ }
+ })
+
+ if not res then return nil, err end
+ local data, err = core.json.decode(res.body)
+
+ if err or not data then
+ err = "failed to parse casdoor response data: " .. err
+ return nil, err
+ end
+
+ if not data.access_token then
+ return nil, "failed when accessing token: no access_token contained"
+ end
+ if not data.expires_in or data.expires_in == 0 then
+ return nil, "failed when accessing token: invalid access_token"
Review comment:
And will you support "expires_in" as the session's expiry later?
##########
File path: apisix/plugins/authz-casdoor.lua
##########
@@ -0,0 +1,150 @@
+--
+-- 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 http = require("resty.http")
+local session = require("resty.session")
+local ngx = ngx
+local rand = math.random
+
+local plugin_name = "authz-casdoor"
+local schema = {
+ type = "object",
+ properties = {
+ -- Note: endpoint_addr and callback_url should not end with '/'
+ endpoint_addr = {type = "string", pattern = "^[^%?]+[^/]$"},
+ client_id = {type = "string"},
+ client_secret = {type = "string"},
+ callback_url = {type = "string", pattern = "^[^%?]+[^/]$"}
+ },
+ 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, state_in_session)
+ 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 code or state"
+ end
+ if not args.state == state_in_session then
+ return nil, "invalid state"
+ end
+ local client = http.new()
+ local url = conf.endpoint_addr .. "/api/login/oauth/access_token"
+
+ local res, err = client:request_uri(url, {
+ method = "POST",
+ query = {
+ code = args.code,
+ grant_type = "authorization_code",
+ client_id = conf.client_id,
+ client_secret = conf.client_secret
+ }
+ })
+
+ if not res then return nil, err end
+ local data, err = core.json.decode(res.body)
+
+ if err or not data then
+ err = "failed to parse casdoor response data: " .. err
+ return nil, err
+ end
+
+ if not data.access_token then
+ return nil, "failed when accessing token: no access_token contained"
+ end
+ if not data.expires_in or data.expires_in == 0 then
+ return nil, "failed when accessing token: invalid access_token"
Review comment:
> Yes, it is not enough to just check whether the token is empty, if the
returned token is an error message, then expires_in information is 0.
Let's add comment for it
##########
File path: apisix/plugins/authz-casdoor.lua
##########
@@ -0,0 +1,150 @@
+--
+-- 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 http = require("resty.http")
+local session = require("resty.session")
+local ngx = ngx
+local rand = math.random
+
+local plugin_name = "authz-casdoor"
+local schema = {
+ type = "object",
+ properties = {
+ -- Note: endpoint_addr and callback_url should not end with '/'
+ endpoint_addr = {type = "string", pattern = "^[^%?]+[^/]$"},
+ client_id = {type = "string"},
+ client_secret = {type = "string"},
+ callback_url = {type = "string", pattern = "^[^%?]+[^/]$"}
+ },
+ 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, state_in_session)
+ 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 code or state"
+ end
+ if not args.state == state_in_session then
Review comment:
```suggestion
if args.state ~= state_in_session then
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]