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