jwt.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package options
  2. import (
  3. "time"
  4. "github.com/spf13/pflag"
  5. )
  6. // JwtOptions jwt配置
  7. type JwtOptions struct {
  8. Realm string `json:"realm" mapstructure:"realm"`
  9. Key string `json:"key" mapstructure:"key"`
  10. Timeout time.Duration `json:"timeout" mapstructure:"timeout"`
  11. MaxRefresh time.Duration `json:"max-refresh" mapstructure:"max-refresh"`
  12. }
  13. // NewJwtOptions creates a JwtOptions object with default parameters.
  14. func NewJwtOptions() *JwtOptions {
  15. return &JwtOptions{
  16. Realm: "JWT",
  17. Key: "abc123",
  18. Timeout: 24 * time.Hour,
  19. MaxRefresh: 24 * time.Hour,
  20. }
  21. }
  22. func (s *JwtOptions) Validate() []error {
  23. var errs []error
  24. return errs
  25. }
  26. func (s *JwtOptions) AddFlags(fs *pflag.FlagSet) {
  27. if fs == nil {
  28. return
  29. }
  30. fs.StringVar(&s.Realm, "jwt.realm", s.Realm, "Realm name to display to the user.")
  31. fs.StringVar(&s.Key, "jwt.key", s.Key, "Private key used to sign jwt token.")
  32. fs.DurationVar(&s.Timeout, "jwt.timeout", s.Timeout, "JWT token timeout.")
  33. fs.DurationVar(&s.MaxRefresh, "jwt.max-refresh", s.MaxRefresh, ""+
  34. "This field allows clients to refresh their token until MaxRefresh has passed.")
  35. }