option.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package jws
  2. import (
  3. "net/http"
  4. "github.com/lestrrat-go/backoff/v2"
  5. "github.com/lestrrat-go/jwx/jwk"
  6. "github.com/lestrrat-go/option"
  7. )
  8. type Option = option.Interface
  9. type identPayloadSigner struct{}
  10. type identDetachedPayload struct{}
  11. type identHeaders struct{}
  12. type identMessage struct{}
  13. type identFetchBackoff struct{}
  14. type identFetchWhitelist struct{}
  15. type identHTTPClient struct{}
  16. type identJWKSetFetcher struct{}
  17. func WithSigner(signer Signer, key interface{}, public, protected Headers) Option {
  18. return option.New(identPayloadSigner{}, &payloadSigner{
  19. signer: signer,
  20. key: key,
  21. protected: protected,
  22. public: public,
  23. })
  24. }
  25. type SignOption interface {
  26. Option
  27. signOption()
  28. }
  29. type signOption struct {
  30. Option
  31. }
  32. func (*signOption) signOption() {}
  33. // WithHeaders allows you to specify extra header values to include in the
  34. // final JWS message
  35. func WithHeaders(h Headers) SignOption {
  36. return &signOption{option.New(identHeaders{}, h)}
  37. }
  38. // VerifyOption describes an option that can be passed to the jws.Verify function
  39. type VerifyOption interface {
  40. Option
  41. verifyOption()
  42. }
  43. type verifyOption struct {
  44. Option
  45. }
  46. func (*verifyOption) verifyOption() {}
  47. // WithMessage can be passed to Verify() to obtain the jws.Message upon
  48. // a successful verification.
  49. func WithMessage(m *Message) VerifyOption {
  50. return &verifyOption{option.New(identMessage{}, m)}
  51. }
  52. type SignVerifyOption interface {
  53. SignOption
  54. VerifyOption
  55. }
  56. type signVerifyOption struct {
  57. Option
  58. }
  59. func (*signVerifyOption) signOption() {}
  60. func (*signVerifyOption) verifyOption() {}
  61. // WithDetachedPayload can be used to both sign or verify a JWS message with a
  62. // detached payload.
  63. //
  64. // When this option is used for `jws.Sign()`, the first parameter (normally the payload)
  65. // must be set to `nil`.
  66. //
  67. // If you have to verify using this option, you should know exactly how and why this works.
  68. func WithDetachedPayload(v []byte) SignVerifyOption {
  69. return &signVerifyOption{option.New(identDetachedPayload{}, v)}
  70. }
  71. // WithFetchWhitelist specifies the whitelist object to be passed
  72. // to `jwk.Fetch()` when `jws.VerifyAuto()` is used. If you do not
  73. // specify a whitelist, `jws.VerifyAuto()` will ALWAYS fail.
  74. //
  75. // This option is ignored if WithJWKSetFetcher is specified.
  76. func WithFetchWhitelist(wl jwk.Whitelist) VerifyOption {
  77. return &verifyOption{option.New(identFetchWhitelist{}, wl)}
  78. }
  79. // WithFetchBackoff specifies the backoff.Policy object to be passed
  80. // to `jwk.Fetch()` when `jws.VerifyAuto()` is used.
  81. //
  82. // This option is ignored if WithJWKSetFetcher is specified.
  83. func WithFetchBackoff(b backoff.Policy) VerifyOption {
  84. return &verifyOption{option.New(identFetchBackoff{}, b)}
  85. }
  86. // WithHTTPClient specifies the *http.Client object to be passed
  87. // to `jwk.Fetch()` when `jws.VerifyAuto()` is used.
  88. //
  89. // This option is ignored if WithJWKSetFetcher is specified.
  90. func WithHTTPClient(httpcl *http.Client) VerifyOption {
  91. return &verifyOption{option.New(identHTTPClient{}, httpcl)}
  92. }
  93. // WithJWKSetFetcher specifies the JWKSetFetcher object to be
  94. // used when `jws.VerifyAuto()`, for example, to use `jwk.AutoRefetch`
  95. // instead of the default `jwk.Fetch()`
  96. func WithJWKSetFetcher(f JWKSetFetcher) VerifyOption {
  97. return &verifyOption{option.New(identJWKSetFetcher{}, f)}
  98. }