Skip to content

Commit

Permalink
feat: integrate alist with casdoor (#1453)
Browse files Browse the repository at this point in the history
* feat: integrate alist with casdoor

* fix: casdoor as an option for login

Co-authored-by: wenxuan70 <[email protected]>
  • Loading branch information
wenxuan70 and wenxuan70 authored Aug 6, 2022
1 parent 5e59b0a commit 51b8b43
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 30 deletions.
11 changes: 1 addition & 10 deletions alist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/Xhofe/alist/bootstrap"
"github.com/Xhofe/alist/conf"
_ "github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
Expand All @@ -15,17 +14,9 @@ func Init() bool {
bootstrap.InitConf()
bootstrap.InitCron()
bootstrap.InitModel()
if conf.Password {
pass, err := model.GetSettingByKey("password")
if err != nil {
log.Errorf(err.Error())
return false
}
fmt.Printf("your password: %s\n", pass.Value)
return false
}
server.InitIndex()
bootstrap.InitSettings()
bootstrap.InitAuth()
bootstrap.InitAccounts()
bootstrap.InitCache()
return true
Expand Down
51 changes: 51 additions & 0 deletions bootstrap/auth.go
Original file line number Diff line number Diff line change
@@ -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 != ""
}
1 change: 0 additions & 1 deletion bootstrap/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
56 changes: 56 additions & 0 deletions bootstrap/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions conf/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ var (
"default page size", "load type",
"ocr api", "favicon",
"enable search",
"Enable Casdoor", "Casdoor Organization name", "Casdoor Application name",
"Casdoor Endpoint", "Casdoor Client id", "Casdoor Client secret",
"Casdoor Jwt Public Key",
}
)

Expand Down
2 changes: 1 addition & 1 deletion drivers/alist/alist.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (driver *Alist) Login(account *model.Account) error {
var resp BaseResp
_, err := base.RestyClient.R().SetResult(&resp).
SetHeader("Authorization", account.AccessToken).
Get(account.SiteUrl + "/api/admin/login")
Get(account.SiteUrl + "/api/admin/verify")
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/aws/aws-sdk-go v1.27.0
github.com/caarlos0/env/v6 v6.9.1
github.com/casdoor/casdoor-go-sdk v0.7.0
github.com/eko/gocache/v2 v2.1.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.4
Expand All @@ -24,7 +25,12 @@ require (
gorm.io/gorm v1.23.1
)

require github.com/kr/fs v0.1.0 // indirect
require (
github.com/golang-jwt/jwt/v4 v4.1.0 // indirect
github.com/kr/fs v0.1.0 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
google.golang.org/appengine v1.6.6 // indirect
)

require (
github.com/fatih/color v1.13.0
Expand Down
Loading

0 comments on commit 51b8b43

Please sign in to comment.