signature.go 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package signature
  2. import (
  3. "net/http"
  4. "net/url"
  5. "time"
  6. )
  7. var _ Signature = (*signature)(nil)
  8. const (
  9. delimiter = "|"
  10. )
  11. // 合法的 Methods
  12. var methods = map[string]bool{
  13. http.MethodGet: true,
  14. http.MethodPost: true,
  15. http.MethodHead: true,
  16. http.MethodPut: true,
  17. http.MethodPatch: true,
  18. http.MethodDelete: true,
  19. http.MethodConnect: true,
  20. http.MethodOptions: true,
  21. http.MethodTrace: true,
  22. }
  23. type Signature interface {
  24. i()
  25. // Generate 生成签名
  26. Generate(path string, method string, params url.Values) (authorization, date string, err error)
  27. // Verify 验证签名
  28. Verify(authorization, date string, path string, method string, params url.Values) (ok bool, err error)
  29. }
  30. type signature struct {
  31. key string
  32. secret string
  33. ttl time.Duration
  34. }
  35. func New(key, secret string, ttl time.Duration) Signature {
  36. return &signature{
  37. key: key,
  38. secret: secret,
  39. ttl: ttl,
  40. }
  41. }
  42. func (s *signature) i() {}