mirror of https://github.com/Xhofe/alist
				
				
				
			chore: set guest while token is empty
							parent
							
								
									54ca68e4b3
								
							
						
					
					
						commit
						7cbfe93a02
					
				| 
						 | 
				
			
			@ -22,6 +22,7 @@ func initUser() {
 | 
			
		|||
				Password: random.RandomStr(8),
 | 
			
		||||
				Role:     model.ADMIN,
 | 
			
		||||
				BasePath: "/",
 | 
			
		||||
				Webdav:   true,
 | 
			
		||||
			}
 | 
			
		||||
			if err := db.CreateUser(admin); err != nil {
 | 
			
		||||
				panic(err)
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +36,9 @@ func initUser() {
 | 
			
		|||
		if errors.Is(err, gorm.ErrRecordNotFound) {
 | 
			
		||||
			guest = &model.User{
 | 
			
		||||
				Username: "guest",
 | 
			
		||||
				Password: "guest",
 | 
			
		||||
				ReadOnly: true,
 | 
			
		||||
				Webdav:   true,
 | 
			
		||||
				Role:     model.GUEST,
 | 
			
		||||
				BasePath: "/",
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,7 +16,8 @@ type User struct {
 | 
			
		|||
	Username string `json:"username" gorm:"unique"` // username
 | 
			
		||||
	Password string `json:"password"`               // password
 | 
			
		||||
	BasePath string `json:"base_path"`              // base path
 | 
			
		||||
	ReadOnly bool   `json:"read_only"`              // allow upload
 | 
			
		||||
	ReadOnly bool   `json:"read_only"`              // read only
 | 
			
		||||
	Webdav   bool   `json:"webdav"`                 // allow webdav
 | 
			
		||||
	Role     int    `json:"role"`                   // user's role
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,8 +11,8 @@ type Resp struct {
 | 
			
		|||
	Data    interface{} `json:"data"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ErrorResp(c *gin.Context, err error, code int, noLog ...bool) {
 | 
			
		||||
	if len(noLog) != 0 && noLog[0] {
 | 
			
		||||
func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
 | 
			
		||||
	if len(l) != 0 && l[0] {
 | 
			
		||||
		log.Errorf("%+v", err)
 | 
			
		||||
	}
 | 
			
		||||
	c.JSON(200, Resp{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ package controllers
 | 
			
		|||
import (
 | 
			
		||||
	"github.com/Xhofe/go-cache"
 | 
			
		||||
	"github.com/alist-org/alist/v3/internal/db"
 | 
			
		||||
	"github.com/alist-org/alist/v3/internal/model"
 | 
			
		||||
	"github.com/alist-org/alist/v3/internal/server/common"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"time"
 | 
			
		||||
| 
						 | 
				
			
			@ -23,7 +24,7 @@ func Login(c *gin.Context) {
 | 
			
		|||
	// check count of login
 | 
			
		||||
	ip := c.ClientIP()
 | 
			
		||||
	count, ok := loginCache.Get(ip)
 | 
			
		||||
	if ok && count > defaultTimes {
 | 
			
		||||
	if ok && count >= defaultTimes {
 | 
			
		||||
		common.ErrorStrResp(c, "Too many unsuccessful sign-in attempts have been made using an incorrect password. Try again later.", 403)
 | 
			
		||||
		loginCache.Expire(ip, defaultDuration)
 | 
			
		||||
		return
 | 
			
		||||
| 
						 | 
				
			
			@ -36,12 +37,12 @@ func Login(c *gin.Context) {
 | 
			
		|||
	}
 | 
			
		||||
	user, err := db.GetUserByName(req.Username)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		common.ErrorResp(c, err, 400, true)
 | 
			
		||||
		common.ErrorResp(c, err, 400)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// validate password
 | 
			
		||||
	if err := user.ValidatePassword(req.Password); err != nil {
 | 
			
		||||
		common.ErrorResp(c, err, 400, true)
 | 
			
		||||
		common.ErrorResp(c, err, 400)
 | 
			
		||||
		loginCache.Set(ip, count+1)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -54,3 +55,10 @@ func Login(c *gin.Context) {
 | 
			
		|||
	common.SuccessResp(c, gin.H{"token": token})
 | 
			
		||||
	loginCache.Del(ip)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CurrentUser get current user by token
 | 
			
		||||
// if token is empty, return guest user
 | 
			
		||||
func CurrentUser(c *gin.Context) {
 | 
			
		||||
	user := c.MustGet("user").(*model.User)
 | 
			
		||||
	common.SuccessResp(c, gin.H{"user": user})
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,8 +6,16 @@ import (
 | 
			
		|||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func AuthAdmin(c *gin.Context) {
 | 
			
		||||
// Auth is a middleware that checks if the user is logged in.
 | 
			
		||||
// if token is empty, set user to guest
 | 
			
		||||
func Auth(c *gin.Context) {
 | 
			
		||||
	token := c.GetHeader("Authorization")
 | 
			
		||||
	if token == "" {
 | 
			
		||||
		guest, _ := db.GetGuest()
 | 
			
		||||
		c.Set("user", guest)
 | 
			
		||||
		c.Next()
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	userClaims, err := common.ParseToken(token)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		common.ErrorResp(c, err, 401)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,7 @@ import (
 | 
			
		|||
	"github.com/alist-org/alist/v3/internal/conf"
 | 
			
		||||
	"github.com/alist-org/alist/v3/internal/server/common"
 | 
			
		||||
	"github.com/alist-org/alist/v3/internal/server/controllers"
 | 
			
		||||
	"github.com/alist-org/alist/v3/internal/server/middlewares"
 | 
			
		||||
	"github.com/gin-contrib/cors"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -12,8 +13,9 @@ func Init(r *gin.Engine) {
 | 
			
		|||
	common.SecretKey = []byte(conf.Conf.JwtSecret)
 | 
			
		||||
	Cors(r)
 | 
			
		||||
 | 
			
		||||
	api := r.Group("/api")
 | 
			
		||||
	api := r.Group("/api", middlewares.Auth)
 | 
			
		||||
	api.POST("/user/login", controllers.Login)
 | 
			
		||||
	api.GET("/user/current", controllers.CurrentUser)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Cors(r *gin.Engine) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue