token.go 752 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package token
  2. import (
  3. "net/url"
  4. "time"
  5. "github.com/golang-jwt/jwt/v4"
  6. )
  7. var _ Token = (*token)(nil)
  8. type Token interface {
  9. // i 为了避免被其他包实现
  10. i()
  11. // JwtSign JWT 签名方式
  12. JwtSign(userId uint64, userName string, expireDuration time.Duration) (tokenString string, err error)
  13. JwtParse(tokenString string) (*claims, error)
  14. // UrlSign URL 签名方式,不支持解密
  15. UrlSign(path string, method string, params url.Values) (tokenString string, err error)
  16. }
  17. type token struct {
  18. secret string
  19. domain []string
  20. }
  21. type claims struct {
  22. UserID uint64 `json:"uid"`
  23. jwt.RegisteredClaims
  24. }
  25. func New(secret string, domain ...string) Token {
  26. return &token{
  27. secret: secret,
  28. domain: domain,
  29. }
  30. }
  31. func (t *token) i() {}