2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-02-28 20:23:50 +08:00
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
|
|
|
package authz
|
|
|
|
|
|
|
|
import (
|
2022-09-09 01:53:21 +08:00
|
|
|
"strings"
|
|
|
|
|
2021-02-28 20:23:50 +08:00
|
|
|
"github.com/casbin/casbin/v2"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
2022-10-07 15:59:23 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
2023-05-19 14:26:32 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-02-28 20:23:50 +08:00
|
|
|
stringadapter "github.com/qiangmzsx/string-adapter/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Enforcer *casbin.Enforcer
|
|
|
|
|
2023-07-29 15:07:04 +08:00
|
|
|
func InitApi() {
|
2023-08-11 12:31:49 +08:00
|
|
|
e, err := object.GetInitializedEnforcer(util.GetId("built-in", "api-enforcer-built-in"))
|
2021-02-28 20:23:50 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-07-31 00:20:53 +08:00
|
|
|
Enforcer = e.Enforcer
|
2021-05-13 22:50:50 +08:00
|
|
|
Enforcer.ClearPolicy()
|
|
|
|
|
2022-08-07 12:26:14 +08:00
|
|
|
// if len(Enforcer.GetPolicy()) == 0 {
|
2021-02-28 23:14:48 +08:00
|
|
|
if true {
|
2021-02-28 20:23:50 +08:00
|
|
|
ruleText := `
|
2021-02-28 23:14:48 +08:00
|
|
|
p, built-in, *, *, *, *, *
|
2021-09-05 01:03:29 +08:00
|
|
|
p, app, *, *, *, *, *
|
2021-04-27 22:47:44 +08:00
|
|
|
p, *, *, POST, /api/signup, *, *
|
2023-03-01 15:57:42 +08:00
|
|
|
p, *, *, GET, /api/get-email-and-phone, *, *
|
2021-02-28 23:14:48 +08:00
|
|
|
p, *, *, POST, /api/login, *, *
|
2021-03-21 00:38:00 +08:00
|
|
|
p, *, *, GET, /api/get-app-login, *, *
|
2021-02-28 23:14:48 +08:00
|
|
|
p, *, *, POST, /api/logout, *, *
|
2022-07-22 21:13:49 +08:00
|
|
|
p, *, *, GET, /api/logout, *, *
|
2021-02-28 23:14:48 +08:00
|
|
|
p, *, *, GET, /api/get-account, *, *
|
2022-01-26 11:56:01 +08:00
|
|
|
p, *, *, GET, /api/userinfo, *, *
|
2023-03-24 01:02:04 +08:00
|
|
|
p, *, *, GET, /api/user, *, *
|
2023-05-16 21:47:34 +08:00
|
|
|
p, *, *, GET, /api/health, *, *
|
2022-11-13 15:05:15 +08:00
|
|
|
p, *, *, POST, /api/webhook, *, *
|
|
|
|
p, *, *, GET, /api/get-webhook-event, *, *
|
2023-04-22 16:16:25 +08:00
|
|
|
p, *, *, GET, /api/get-captcha-status, *, *
|
2022-04-22 22:45:52 +08:00
|
|
|
p, *, *, *, /api/login/oauth, *, *
|
2021-02-28 23:14:48 +08:00
|
|
|
p, *, *, GET, /api/get-application, *, *
|
2022-08-07 16:05:05 +08:00
|
|
|
p, *, *, GET, /api/get-organization-applications, *, *
|
2021-03-06 00:40:11 +08:00
|
|
|
p, *, *, GET, /api/get-user, *, *
|
2021-07-11 23:49:06 +08:00
|
|
|
p, *, *, GET, /api/get-user-application, *, *
|
2021-09-06 00:49:10 +08:00
|
|
|
p, *, *, GET, /api/get-resources, *, *
|
2022-09-18 16:16:45 +08:00
|
|
|
p, *, *, GET, /api/get-records, *, *
|
2022-03-13 11:51:33 +08:00
|
|
|
p, *, *, GET, /api/get-product, *, *
|
2022-03-13 16:25:54 +08:00
|
|
|
p, *, *, POST, /api/buy-product, *, *
|
|
|
|
p, *, *, GET, /api/get-payment, *, *
|
2022-04-27 01:06:54 +08:00
|
|
|
p, *, *, POST, /api/update-payment, *, *
|
|
|
|
p, *, *, POST, /api/invoice-payment, *, *
|
2022-07-17 14:29:06 +08:00
|
|
|
p, *, *, POST, /api/notify-payment, *, *
|
2021-04-27 20:42:19 +08:00
|
|
|
p, *, *, POST, /api/unlink, *, *
|
2021-05-16 21:04:26 +08:00
|
|
|
p, *, *, POST, /api/set-password, *, *
|
2021-05-18 20:11:03 +08:00
|
|
|
p, *, *, POST, /api/send-verification-code, *, *
|
2022-06-18 16:00:31 +08:00
|
|
|
p, *, *, GET, /api/get-captcha, *, *
|
|
|
|
p, *, *, POST, /api/verify-captcha, *, *
|
2023-04-06 23:06:18 +08:00
|
|
|
p, *, *, POST, /api/verify-code, *, *
|
2021-05-23 23:38:38 +08:00
|
|
|
p, *, *, POST, /api/reset-email-or-phone, *, *
|
2021-08-28 11:13:38 +08:00
|
|
|
p, *, *, POST, /api/upload-resource, *, *
|
2021-09-25 14:54:13 +08:00
|
|
|
p, *, *, GET, /.well-known/openid-configuration, *, *
|
2022-02-21 23:17:16 +08:00
|
|
|
p, *, *, *, /.well-known/jwks, *, *
|
2021-12-06 21:46:50 +08:00
|
|
|
p, *, *, GET, /api/get-saml-login, *, *
|
|
|
|
p, *, *, POST, /api/acs, *, *
|
2022-04-08 23:06:48 +08:00
|
|
|
p, *, *, GET, /api/saml/metadata, *, *
|
2022-04-04 00:09:04 +08:00
|
|
|
p, *, *, *, /cas, *, *
|
2022-07-12 20:06:01 +08:00
|
|
|
p, *, *, *, /api/webauthn, *, *
|
2022-08-20 21:22:46 +08:00
|
|
|
p, *, *, GET, /api/get-release, *, *
|
2022-09-10 20:41:45 +08:00
|
|
|
p, *, *, GET, /api/get-default-application, *, *
|
2023-04-25 16:06:09 +08:00
|
|
|
p, *, *, GET, /api/get-prometheus-info, *, *
|
|
|
|
p, *, *, *, /api/metrics, *, *
|
2023-05-20 10:56:21 +03:00
|
|
|
p, *, *, GET, /api/get-pricing, *, *
|
|
|
|
p, *, *, GET, /api/get-plan, *, *
|
2023-08-24 23:20:50 +08:00
|
|
|
p, *, *, GET, /api/get-subscription, *, *
|
2023-09-07 15:45:54 +08:00
|
|
|
p, *, *, GET, /api/get-provider, *, *
|
2023-05-27 19:02:54 +08:00
|
|
|
p, *, *, GET, /api/get-organization-names, *, *
|
2021-02-28 20:23:50 +08:00
|
|
|
`
|
|
|
|
|
|
|
|
sa := stringadapter.NewAdapter(ruleText)
|
|
|
|
// load all rules from string adapter to enforcer's memory
|
|
|
|
err := sa.LoadPolicy(Enforcer.GetModel())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// save all rules from enforcer's memory to Xorm adapter (DB)
|
|
|
|
// same as:
|
|
|
|
// a.SavePolicy(Enforcer.GetModel())
|
|
|
|
err = Enforcer.SavePolicy()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 23:14:48 +08:00
|
|
|
func IsAllowed(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool {
|
2022-09-04 21:20:19 +08:00
|
|
|
if conf.IsDemoMode() {
|
|
|
|
if !isAllowedInDemoMode(subOwner, subName, method, urlPath, objOwner, objName) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err := object.GetUser(util.GetId(subOwner, subName))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-05-09 00:06:52 +08:00
|
|
|
if user != nil && user.IsAdmin && (subOwner == objOwner || (objOwner == "admin")) {
|
2022-10-07 15:59:23 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-28 23:14:48 +08:00
|
|
|
res, err := Enforcer.Enforce(subOwner, subName, method, urlPath, objOwner, objName)
|
2021-02-28 20:23:50 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2022-09-04 21:20:19 +08:00
|
|
|
|
|
|
|
func isAllowedInDemoMode(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool {
|
|
|
|
if method == "POST" {
|
2023-03-02 21:40:27 +08:00
|
|
|
if strings.HasPrefix(urlPath, "/api/login") || urlPath == "/api/logout" || urlPath == "/api/signup" || urlPath == "/api/send-verification-code" || urlPath == "/api/send-email" || urlPath == "/api/verify-captcha" {
|
2022-09-04 21:20:19 +08:00
|
|
|
return true
|
|
|
|
} else if urlPath == "/api/update-user" {
|
|
|
|
// Allow ordinary users to update their own information
|
|
|
|
if subOwner == objOwner && subName == objName && !(subOwner == "built-in" && subName == "admin") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If method equals GET
|
|
|
|
return true
|
|
|
|
}
|