-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat: add auth plugin for casdoor #6382
Changes from 1 commit
3752e64
6097607
ea49e51
4dae511
f7dd77e
e2a242d
4407bb7
d3e5ac1
9b013f8
897c7a9
aea4d1a
3c61648
07bdf00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,121 @@ | ||||||
-- | ||||||
-- 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 plugin_name = "auth-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 = "^[^%?]+[^/]$"}, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to ensure that the url doesn't end up with '/', so I can directly extend this url by using something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get it. Let's add as a comment. |
||||||
}, | ||||||
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 code or 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.cjson.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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, when casdoor gets an accessToken error, it will return an accessToken like |
||||||
return nil, "failed when accessing token: no access_token contained" | ||||||
end | ||||||
|
||||||
return data.access_token, nil | ||||||
end | ||||||
|
||||||
function _M.check_schema(conf) return core.schema.check(schema, conf) end | ||||||
|
||||||
function _M.access(conf, ctx) | ||||||
-- log.info("hit auth-casdoor access") | ||||||
local current_uri = ctx.var.uri | ||||||
local session_obj_read, session_present = session.open() | ||||||
|
||||||
-- step 1: check whether hits the callback | ||||||
local real_callback_url=ngx.re.match(conf.callback_url, ".-//[^/]+(/.*)") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please refer to https://github.com/openresty/lua-nginx-module/tree/master#ngxrematch for how to use it. |
||||||
if current_uri == real_callback_url then | ||||||
local access_token, err = fetch_access_token(ctx, conf) | ||||||
if access_token then | ||||||
if not session_present then | ||||||
return 503, "no session found" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no test to cover this branch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in t/plugin/auth-casdoor.t Test7 is the special test case which will cover this, where no session was given. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please log down the err, like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
is better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that after this modification, the plugin is unable to pass the test |
||||||
end | ||||||
local original_url = session_obj_read.data.original_uri | ||||||
if not original_url then | ||||||
return 503, "no original_url found in session" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
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 | ||||||
end | ||||||
end | ||||||
|
||||||
-- step 2: check whether session exists | ||||||
if not (session_present and session_obj_read.data.access_token) then | ||||||
-- session not exists, redirect to login page | ||||||
local session_obj_write = session.start() | ||||||
session_obj_write.data.original_uri = current_uri | ||||||
session_obj_write:save() | ||||||
local redirect_url = conf.endpoint_addr .. | ||||||
"/login/oauth/authorize?response_type=code&scope=read" .. | ||||||
"&state=casdoor&client_id=" .. conf.client_id .. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The state should be generated randomly, and the state returned by casdoor should be checked to see if it is the same as the one generated. |
||||||
"&redirect_uri=" .. conf.callback_url | ||||||
core.response.set_header("Location", redirect_url) | ||||||
return 302 | ||||||
end | ||||||
|
||||||
end | ||||||
|
||||||
return _M |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,94 @@ | ||||||
--- | ||||||
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 | | ||||||
| ----------- | ------ | ----------- | ------- | ----- | ------------------------------------------------------------ | | ||||||
| endpoint_addr | 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. | | ||||||
|
||||||
*Note: endpoint_addr and callback_url should not end with '/'* | ||||||
|
||||||
## How To Enable | ||||||
|
||||||
You can enable the plugin on any route by giving out all four attributes mentioned above. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
would be better |
||||||
|
||||||
### 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": { | ||||||
"endpoint_addr":"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 completely by the plugin so that there is no need to modify the server to implement this callback. | ||||||
|
||||||
The second parameter "endpoint_addr" 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. | ||||||
|
||||||
#### How it works? | ||||||
|
||||||
Suppose a new user who has never visited this route before is going to visit it (http://localhost:9080/anything/d?param1=foo¶m2=bar), considering that "auth-casdoor" is enabled, this visit would be processed by "auth-casdoor" plugin first. After checking the session and confirming that this user hasn't been authenticated, the visit will be intercepted. With the original url user wants to visit kept, he will be redirected to the login page of Casdoor. | ||||||
|
||||||
After successfully logging in with username and password(or whatever method he uses), Casdoor will redirect this user to the "callback_url" with GET parameter "code" and "state" specified. Because the "callback_url" is known by the plugin, when the visit toward the "callback_url" is intercepted this time, the logic of "Authorization code Grant Flow" in Oauth2 will be triggered, which means this plugin will request the access token to confirm whether this user is really logged in. After this confirmation, this plugin will redirect this user to the original url user wants to visit, which was kept by us previously. The logged-in status will also be kept in the session. | ||||||
|
||||||
Next time this user want to visit url behind this route (for example, http://localhost:9080/anything/d), after discovering that this user has been authenticated previously, this plugin won't redirect this user anymore so that this user can visit whatever he wants under this route without being interfered. |
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.
Should be authz-casdoor like authz-keycloak and so on?
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.
Well I was given clear instruction that the name of this plugin should be this. It is because that authz means authorization, but we are actually doing is mostly (authn) authentication, while authorization is also involved. Therefore maybe auth is better.
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.
This plugin implements a variant of oauth2 which is for authorization, just like authz-keycloak.
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.
No, but we are actually doing is mostly (authz) authorization.
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.
Well perhaps I ... prefer to let the plugin's name remain as it is now, and I wonder whether it is ok.