feat: integrate alist with casdoor (#1453)

* feat: integrate alist with casdoor

* fix: casdoor as an option for login

Co-authored-by: wenxuan70 <t736660416@gmail.com>
This commit is contained in:
wenxuan70
2022-08-06 16:47:39 +08:00
committed by GitHub
parent 5e59b0a697
commit 51b8b4380d
13 changed files with 449 additions and 30 deletions

51
bootstrap/auth.go Normal file
View File

@@ -0,0 +1,51 @@
package bootstrap
import (
"github.com/Xhofe/alist/conf"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
log "github.com/sirupsen/logrus"
"strings"
)
type AuthConfig struct {
OrganizationName string `json:"organization_name"`
ApplicationName string `json:"application_name"`
Endpoint string `json:"endpoint"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
JwtPublicKey string `json:"jwt_public_key"`
}
// InitAuth init auth
func InitAuth() {
log.Infof("init auth...")
enableCasdoor := conf.GetBool("Enable Casdoor")
if !enableCasdoor {
return
}
auth := readAuthConfig()
if !CheckAuthConfig(auth) {
panic("invalid auth config")
}
casdoorsdk.InitConfig(strings.TrimRight(auth.Endpoint, "/"), auth.ClientId, auth.ClientSecret, auth.JwtPublicKey, auth.OrganizationName, auth.ApplicationName)
}
func readAuthConfig() AuthConfig {
return AuthConfig{
OrganizationName: conf.GetStr("Casdoor Organization name"),
ApplicationName: conf.GetStr("Casdoor Application name"),
Endpoint: conf.GetStr("Casdoor Endpoint"),
ClientId: conf.GetStr("Casdoor Client id"),
ClientSecret: conf.GetStr("Casdoor Client secret"),
JwtPublicKey: conf.GetStr("Casdoor Jwt Public Key"),
}
}
func CheckAuthConfig(authConfig AuthConfig) bool {
return authConfig.Endpoint != "" &&
authConfig.ClientId != "" &&
authConfig.ClientSecret != "" &&
authConfig.OrganizationName != "" &&
authConfig.ApplicationName != "" &&
authConfig.JwtPublicKey != ""
}

View File

@@ -29,7 +29,6 @@ func init() {
flag.StringVar(&conf.ConfigFile, "conf", "data/config.json", "config file")
flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode")
flag.BoolVar(&conf.Version, "version", false, "print version info")
flag.BoolVar(&conf.Password, "password", false, "print current password")
flag.BoolVar(&conf.Docker, "docker", false, "is using docker")
flag.Parse()
InitLog()

View File

@@ -289,6 +289,62 @@ func InitSettings() {
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Enable Casdoor",
Value: "false",
Description: "Enable Casdoor login",
Type: "bool",
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Casdoor Organization name",
Value: "",
Description: "Casdoor Organization name",
Type: "string",
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Casdoor Application name",
Value: "",
Description: "Casdoor Application name",
Type: "string",
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Casdoor Endpoint",
Value: "",
Description: "Casdoor Endpoint, e.g. 'http://localhost:8000'",
Type: "string",
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Casdoor Client id",
Value: "",
Description: "Casdoor Client id",
Type: "string",
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Casdoor Client secret",
Value: "",
Description: "Casdoor Client secret",
Type: "string",
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "Casdoor Jwt Public Key",
Value: "",
Description: "Casdoor Jwt Public Key",
Type: "string",
Access: model.PRIVATE,
Group: model.BACK,
},
}
for i, _ := range settings {
v := settings[i]