43 lines
956 B
Go
43 lines
956 B
Go
package auth
|
|
|
|
import (
|
|
"FileRelay/internal/config"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
type Claims struct {
|
|
AdminID uint `json:"admin_id"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func GenerateToken(adminID uint) (string, error) {
|
|
claims := Claims{
|
|
AdminID: adminID,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString([]byte(config.GlobalConfig.Security.JWTSecret))
|
|
}
|
|
|
|
func ParseToken(tokenString string) (*Claims, error) {
|
|
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(config.GlobalConfig.Security.JWTSecret), nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
return claims, nil
|
|
}
|
|
|
|
return nil, jwt.ErrSignatureInvalid
|
|
}
|