mirror of
https://github.com/lejianwen/rustdesk-api.git
synced 2025-11-29 16:43:17 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package jwt
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"time"
|
|
)
|
|
|
|
type Jwt struct {
|
|
Key []byte
|
|
TokenExpireDuration time.Duration
|
|
}
|
|
|
|
type UserClaims struct {
|
|
UserId uint `json:"user_id"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func NewJwt(key string, tokenExpireDuration time.Duration) *Jwt {
|
|
return &Jwt{
|
|
Key: []byte(key),
|
|
TokenExpireDuration: tokenExpireDuration,
|
|
}
|
|
}
|
|
|
|
func (s *Jwt) GenerateToken(userId uint) string {
|
|
if len(s.Key) == 0 {
|
|
fmt.Println("jwt key is nil")
|
|
return ""
|
|
}
|
|
t := jwt.NewWithClaims(jwt.SigningMethodHS256,
|
|
UserClaims{
|
|
UserId: userId,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(s.TokenExpireDuration)),
|
|
},
|
|
})
|
|
token, err := t.SignedString(s.Key)
|
|
if err != nil {
|
|
fmt.Printf("jwt token generate error: %v", err)
|
|
return ""
|
|
}
|
|
return token
|
|
}
|
|
|
|
func (s *Jwt) ParseToken(tokenString string) (uint, error) {
|
|
token, err := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return s.Key, nil
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if claims, ok := token.Claims.(*UserClaims); ok && token.Valid {
|
|
return claims.UserId, nil
|
|
}
|
|
return 0, err
|
|
}
|