package jwt import ( "maps" "time" "github.com/golang-jwt/jwt/v5" "github.com/google/uuid" ) type Service struct { signer Signer } func NewService(signer Signer) *Service { return &Service{ signer: signer, } } // Generate creates a new JWT token for a given user ID and // returns the token string along with its JTI(JWT IDentifier). func (s *Service) Generate(ttl time.Duration, extraClaims jwt.MapClaims) (string, string, error) { jti := uuid.NewString() claims := jwt.MapClaims{ "jti": jti, "exp": time.Now().Add(ttl).Unix(), "iat": time.Now().Unix(), } maps.Copy(claims, extraClaims) token, err := s.signer.Sign(claims) return token, jti, err } // Validate verifies the JWT token and extracts the claims and JTI(JWT IDentifier). func (s *Service) Validate(token string) (jwt.MapClaims, string, error) { claims, err := s.signer.Verify(token) if err != nil { return nil, "", err } jti := claims.(jwt.MapClaims)["jti"].(string) return claims.(jwt.MapClaims), jti, nil }