Files
bj_power/bj_power_mes/common/token/token_gen.go
T

34 lines
740 B
Go
Raw Normal View History

package token
import (
"encoding/base64"
"fmt"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
)
const RefreshTokenKeyPrefix = "token:refresh:"
func GenToken(secretKey string, iat int64, expireSeconds int64, payloads map[string]any) (string, error) {
claims := make(jwt.MapClaims)
claims["exp"] = iat + expireSeconds
claims["iat"] = iat
for k, v := range payloads {
claims[k] = v
}
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
return token.SignedString([]byte(secretKey))
}
func GenRefreshToken() string {
return base64.StdEncoding.EncodeToString([]byte(uuid.New().String()))
}
func GenRefreshTokenCacheKey(userId int64) string {
return fmt.Sprintf("%s%d", RefreshTokenKeyPrefix, userId)
}