options.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package jwe
  2. import (
  3. "context"
  4. "github.com/lestrrat-go/option"
  5. )
  6. type Option = option.Interface
  7. type identMessage struct{}
  8. type identPostParser struct{}
  9. type identPrettyFormat struct{}
  10. type identProtectedHeader struct{}
  11. type DecryptOption interface {
  12. Option
  13. decryptOption()
  14. }
  15. type decryptOption struct {
  16. Option
  17. }
  18. func (*decryptOption) decryptOption() {}
  19. type SerializerOption interface {
  20. Option
  21. serializerOption()
  22. }
  23. type serializerOption struct {
  24. Option
  25. }
  26. func (*serializerOption) serializerOption() {}
  27. type EncryptOption interface {
  28. Option
  29. encryptOption()
  30. }
  31. type encryptOption struct {
  32. Option
  33. }
  34. func (*encryptOption) encryptOption() {}
  35. // WithPrettyFormat specifies if the `jwe.JSON` serialization tool
  36. // should generate pretty-formatted output
  37. func WithPrettyFormat(b bool) SerializerOption {
  38. return &serializerOption{option.New(identPrettyFormat{}, b)}
  39. }
  40. // Specify contents of the protected header. Some fields such as
  41. // "enc" and "zip" will be overwritten when encryption is performed.
  42. func WithProtectedHeaders(h Headers) EncryptOption {
  43. cloned, _ := h.Clone(context.Background())
  44. return &encryptOption{option.New(identProtectedHeader{}, cloned)}
  45. }
  46. // WithMessage provides a message object to be populated by `jwe.Decrpt`
  47. // Using this option allows you to decrypt AND obtain the `jwe.Message`
  48. // in one go.
  49. //
  50. // Note that you should NOT be using the message object for anything other
  51. // than inspecting its contents. Particularly, do not expect the message
  52. // reliable when you call `Decrypt` on it. `(jwe.Message).Decrypt` is
  53. // slated to be deprecated in the next major version.
  54. func WithMessage(m *Message) DecryptOption {
  55. return &decryptOption{option.New(identMessage{}, m)}
  56. }
  57. // WithPostParser specifies the handler to be called immediately
  58. // after the JWE message has been parsed, but before decryption
  59. // takes place during `jwe.Decrypt`.
  60. //
  61. // This option exists to allow advanced users that require the use
  62. // of information stored in the JWE message to determine how the
  63. // decryption should be handled.
  64. //
  65. // For security reasons it is highly recommended that you thoroughly
  66. // study how the process works before using this option. This is especially
  67. // true if you are trying to infer key algorithms and keys to use to
  68. // decrypt a message using non-standard hints.
  69. func WithPostParser(p PostParser) DecryptOption {
  70. return &decryptOption{option.New(identPostParser{}, p)}
  71. }