2022-06-25 13:34:44 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-08-03 06:26:59 +00:00
|
|
|
"time"
|
|
|
|
|
2024-08-04 04:32:39 +00:00
|
|
|
"github.com/Xhofe/go-cache"
|
2022-09-27 06:23:56 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2023-11-13 07:22:42 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-06-25 13:34:44 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var SecretKey []byte
|
|
|
|
|
|
|
|
type UserClaims struct {
|
|
|
|
Username string `json:"username"`
|
2023-11-13 07:22:42 +00:00
|
|
|
PwdTS int64 `json:"pwd_ts"`
|
2022-06-25 13:34:44 +00:00
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
2024-08-04 04:32:39 +00:00
|
|
|
var validTokenCache = cache.NewMemCache[bool]()
|
|
|
|
|
2023-11-13 07:22:42 +00:00
|
|
|
func GenerateToken(user *model.User) (tokenString string, err error) {
|
2022-06-25 13:34:44 +00:00
|
|
|
claim := UserClaims{
|
2023-11-13 07:22:42 +00:00
|
|
|
Username: user.Username,
|
|
|
|
PwdTS: user.PwdTS,
|
2022-06-25 13:34:44 +00:00
|
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
2022-09-27 06:23:56 +00:00
|
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Duration(conf.Conf.TokenExpiresIn) * time.Hour)),
|
2022-06-25 13:34:44 +00:00
|
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
|
|
NotBefore: jwt.NewNumericDate(time.Now()),
|
|
|
|
}}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim)
|
|
|
|
tokenString, err = token.SignedString(SecretKey)
|
2024-08-04 04:32:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
validTokenCache.Set(tokenString, true)
|
2022-06-25 13:34:44 +00:00
|
|
|
return tokenString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseToken(tokenString string) (*UserClaims, error) {
|
|
|
|
token, err := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return SecretKey, nil
|
|
|
|
})
|
2024-08-04 04:32:39 +00:00
|
|
|
if IsTokenInvalidated(tokenString) {
|
|
|
|
return nil, errors.New("token is invalidated")
|
|
|
|
}
|
2022-06-25 13:34:44 +00:00
|
|
|
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")
|
|
|
|
}
|
2024-08-04 04:32:39 +00:00
|
|
|
|
|
|
|
func InvalidateToken(tokenString string) error {
|
|
|
|
if tokenString == "" {
|
|
|
|
return nil // don't invalidate empty guest token
|
|
|
|
}
|
|
|
|
validTokenCache.Del(tokenString)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsTokenInvalidated(tokenString string) bool {
|
|
|
|
_, ok := validTokenCache.Get(tokenString)
|
|
|
|
return !ok
|
|
|
|
}
|