-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
269 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/cmd/args" | ||
"github.com/alist-org/alist/v3/internal/conf" | ||
"github.com/alist-org/alist/v3/internal/store" | ||
log "github.com/sirupsen/logrus" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
stdlog "log" | ||
"time" | ||
) | ||
|
||
func InitDB() { | ||
newLogger := logger.New( | ||
stdlog.New(log.StandardLogger().Out, "\r\n", stdlog.LstdFlags), | ||
logger.Config{ | ||
SlowThreshold: time.Second, | ||
LogLevel: logger.Silent, | ||
IgnoreRecordNotFoundError: true, | ||
Colorful: true, | ||
}, | ||
) | ||
gormConfig := &gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: conf.Conf.Database.TablePrefix, | ||
}, | ||
Logger: newLogger, | ||
} | ||
if args.Dev { | ||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), gormConfig) | ||
if err != nil { | ||
panic("failed to connect database") | ||
} | ||
store.Init(db) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ var ( | |
Version bool | ||
Password bool | ||
NoPrefix bool | ||
Dev bool | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/golang-jwt/jwt/v4" | ||
"github.com/pkg/errors" | ||
"time" | ||
) | ||
|
||
var SecretKey []byte | ||
|
||
type UserClaims struct { | ||
Username string `json:"username"` | ||
jwt.RegisteredClaims | ||
} | ||
|
||
func GenerateToken(username string) (tokenString string, err error) { | ||
claim := UserClaims{ | ||
Username: username, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(12 * time.Hour)), | ||
IssuedAt: jwt.NewNumericDate(time.Now()), | ||
NotBefore: jwt.NewNumericDate(time.Now()), | ||
}} | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim) | ||
tokenString, err = token.SignedString(SecretKey) | ||
return tokenString, err | ||
} | ||
|
||
func ParseToken(tokenString string) (*UserClaims, error) { | ||
token, err := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { | ||
return SecretKey, nil | ||
}) | ||
if err != nil { | ||
if ve, ok := err.(*jwt.ValidationError); ok { | ||
if ve.Errors&jwt.ValidationErrorMalformed != 0 { | ||
return nil, errors.New("that's not even a token") | ||
} else if ve.Errors&jwt.ValidationErrorExpired != 0 { | ||
return nil, errors.New("token is expired") | ||
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 { | ||
return nil, errors.New("token not active yet") | ||
} else { | ||
return nil, errors.New("couldn't handle this token") | ||
} | ||
} | ||
} | ||
if claims, ok := token.Claims.(*UserClaims); ok && token.Valid { | ||
return claims, nil | ||
} | ||
return nil, errors.New("couldn't handle this token") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Resp struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
func ErrorResp(c *gin.Context, err error, code int) { | ||
log.Error(err.Error()) | ||
c.JSON(200, Resp{ | ||
Code: code, | ||
Message: err.Error(), | ||
Data: nil, | ||
}) | ||
c.Abort() | ||
} | ||
|
||
func ErrorStrResp(c *gin.Context, str string, code int) { | ||
log.Error(str) | ||
c.JSON(200, Resp{ | ||
Code: code, | ||
Message: str, | ||
Data: nil, | ||
}) | ||
c.Abort() | ||
} | ||
|
||
func SuccessResp(c *gin.Context, data ...interface{}) { | ||
if len(data) == 0 { | ||
c.JSON(200, Resp{ | ||
Code: 200, | ||
Message: "success", | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
c.JSON(200, Resp{ | ||
Code: 200, | ||
Message: "success", | ||
Data: data[0], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/Xhofe/go-cache" | ||
"github.com/alist-org/alist/v3/internal/server/common" | ||
"github.com/alist-org/alist/v3/internal/store" | ||
"github.com/gin-gonic/gin" | ||
"time" | ||
) | ||
|
||
var loginCache = cache.NewMemCache[int]() | ||
var ( | ||
defaultDuration = time.Minute * 5 | ||
defaultTimes = 5 | ||
) | ||
|
||
type LoginReq struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func Login(c *gin.Context) { | ||
// check count of login | ||
ip := c.ClientIP() | ||
count, ok := loginCache.Get(ip) | ||
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 | ||
} | ||
// check username | ||
var req LoginReq | ||
if err := c.ShouldBind(&req); err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} | ||
user, err := store.GetUserByName(req.Username) | ||
if err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} | ||
// validate password | ||
if err := user.ValidatePassword(req.Password); err != nil { | ||
common.ErrorResp(c, err, 400) | ||
loginCache.Set(ip, count+1) | ||
return | ||
} | ||
// generate token | ||
token, err := common.GenerateToken(user.Username) | ||
if err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} | ||
common.SuccessResp(c, gin.H{"token": token}) | ||
loginCache.Del(ip) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/internal/server/common" | ||
"github.com/alist-org/alist/v3/internal/store" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func AuthAdmin(c *gin.Context) { | ||
token := c.GetHeader("Authorization") | ||
userClaims, err := common.ParseToken(token) | ||
if err != nil { | ||
common.ErrorResp(c, err, 401) | ||
c.Abort() | ||
return | ||
} | ||
user, err := store.GetUserByName(userClaims.Username) | ||
if err != nil { | ||
common.ErrorResp(c, err, 401) | ||
c.Abort() | ||
return | ||
} | ||
c.Set("user", user) | ||
c.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.