package rest import ( "time" "git.ali33.ru/fcg-xvii/go-tools/json" "github.com/dgrijalva/jwt-go" ) func NewTokenGenerator(secret string) *TokenGenerator { return &TokenGenerator{ secret: secret, } } type TokenGenerator struct { secret string } func (s *TokenGenerator) Generate(m json.Map, expire int64) (string, error) { token := jwt.New(jwt.SigningMethodHS256) claims := token.Claims.(jwt.MapClaims) for key, val := range m { claims[key] = val } if expire > 0 { claims["exp"] = time.Now().Add(time.Minute * 30).Unix() } tokenString, err := token.SignedString(s.secret) return tokenString, err }