2021-10-27 14:45:36 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/Xhofe/alist/model"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
2021-11-13 07:53:26 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-27 14:45:36 +00:00
|
|
|
"gorm.io/gorm"
|
2021-11-13 08:12:12 +00:00
|
|
|
"path/filepath"
|
2021-10-27 14:45:36 +00:00
|
|
|
)
|
|
|
|
|
2021-11-13 07:53:26 +00:00
|
|
|
func Auth(c *gin.Context) {
|
|
|
|
token := c.GetHeader("Authorization")
|
2021-10-27 14:45:36 +00:00
|
|
|
password, err := model.GetSettingByKey("password")
|
|
|
|
if err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
2021-11-13 07:53:26 +00:00
|
|
|
ErrorResp(c, fmt.Errorf("password not set"), 400)
|
|
|
|
return
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
2021-11-13 07:53:26 +00:00
|
|
|
ErrorResp(c, err, 500)
|
|
|
|
return
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
|
|
|
if token != utils.GetMD5Encode(password.Value) {
|
2021-11-13 07:53:26 +00:00
|
|
|
ErrorResp(c, fmt.Errorf("wrong password"), 401)
|
|
|
|
return
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
2021-11-13 07:53:26 +00:00
|
|
|
c.Next()
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|
2021-10-28 04:37:31 +00:00
|
|
|
|
2021-11-13 07:53:26 +00:00
|
|
|
func Login(c *gin.Context) {
|
|
|
|
SuccessResp(c)
|
2021-10-29 16:35:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 07:53:26 +00:00
|
|
|
func CheckAccount(c *gin.Context) {
|
2021-10-28 04:37:31 +00:00
|
|
|
if model.AccountsCount() == 0 {
|
2021-11-13 07:53:26 +00:00
|
|
|
ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
|
|
|
|
return
|
2021-10-28 04:37:31 +00:00
|
|
|
}
|
2021-11-13 07:53:26 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
2021-11-13 08:12:12 +00:00
|
|
|
|
|
|
|
func CheckParent(path string, password string) bool {
|
|
|
|
meta, err := model.GetMetaByPath(path)
|
|
|
|
if err == nil {
|
|
|
|
if meta.Password != "" && meta.Password != password {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
if path == "/" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return CheckParent(filepath.Dir(path), password)
|
|
|
|
}
|
|
|
|
}
|