2021-10-27 14:45:36 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/Xhofe/alist/model"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Auth(ctx *fiber.Ctx) error {
|
2021-10-29 16:35:29 +00:00
|
|
|
token := ctx.Get("Authorization")
|
2021-10-27 14:45:36 +00:00
|
|
|
password, err := model.GetSettingByKey("password")
|
|
|
|
if err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return ErrorResp(ctx, fmt.Errorf("password not set"), 400)
|
|
|
|
}
|
|
|
|
return ErrorResp(ctx, err, 500)
|
|
|
|
}
|
|
|
|
if token != utils.GetMD5Encode(password.Value) {
|
|
|
|
return ErrorResp(ctx, fmt.Errorf("wrong password"), 401)
|
|
|
|
}
|
|
|
|
return ctx.Next()
|
|
|
|
}
|
2021-10-28 04:37:31 +00:00
|
|
|
|
2021-10-29 16:35:29 +00:00
|
|
|
func Login(ctx *fiber.Ctx) error {
|
|
|
|
return SuccessResp(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetSuccess(ctx *fiber.Ctx) error {
|
|
|
|
ctx.Status(200)
|
|
|
|
return ctx.Next()
|
|
|
|
}
|
|
|
|
|
2021-10-28 04:37:31 +00:00
|
|
|
func CheckAccount(ctx *fiber.Ctx) error {
|
|
|
|
if model.AccountsCount() == 0 {
|
|
|
|
return ErrorResp(ctx,fmt.Errorf("no accounts,please add one first"),1001)
|
|
|
|
}
|
|
|
|
return ctx.Next()
|
|
|
|
}
|