2022-06-25 13:34:44 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-08-03 06:26:59 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-27 06:23:56 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
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"`
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateToken(username string) (tokenString string, err error) {
|
|
|
|
claim := UserClaims{
|
|
|
|
Username: username,
|
|
|
|
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)
|
|
|
|
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")
|
|
|
|
}
|