serializer.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package jwe
  2. import (
  3. "context"
  4. "github.com/lestrrat-go/jwx/internal/base64"
  5. "github.com/lestrrat-go/jwx/internal/json"
  6. "github.com/lestrrat-go/jwx/internal/pool"
  7. "github.com/pkg/errors"
  8. )
  9. // Compact encodes the given message into a JWE compact serialization format.
  10. //
  11. // Currently `Compact()` does not take any options, but the API is
  12. // set up as such to allow future expansions
  13. func Compact(m *Message, _ ...SerializerOption) ([]byte, error) {
  14. if len(m.recipients) != 1 {
  15. return nil, errors.New("wrong number of recipients for compact serialization")
  16. }
  17. recipient := m.recipients[0]
  18. // The protected header must be a merge between the message-wide
  19. // protected header AND the recipient header
  20. // There's something wrong if m.protectedHeaders is nil, but
  21. // it could happen
  22. if m.protectedHeaders == nil {
  23. return nil, errors.New("invalid protected header")
  24. }
  25. ctx := context.TODO()
  26. hcopy, err := m.protectedHeaders.Clone(ctx)
  27. if err != nil {
  28. return nil, errors.Wrap(err, "failed to copy protected header")
  29. }
  30. hcopy, err = hcopy.Merge(ctx, m.unprotectedHeaders)
  31. if err != nil {
  32. return nil, errors.Wrap(err, "failed to merge unprotected header")
  33. }
  34. hcopy, err = hcopy.Merge(ctx, recipient.Headers())
  35. if err != nil {
  36. return nil, errors.Wrap(err, "failed to merge recipient header")
  37. }
  38. protected, err := hcopy.Encode()
  39. if err != nil {
  40. return nil, errors.Wrap(err, "failed to encode header")
  41. }
  42. encryptedKey := base64.Encode(recipient.EncryptedKey())
  43. iv := base64.Encode(m.initializationVector)
  44. cipher := base64.Encode(m.cipherText)
  45. tag := base64.Encode(m.tag)
  46. buf := pool.GetBytesBuffer()
  47. defer pool.ReleaseBytesBuffer(buf)
  48. buf.Grow(len(protected) + len(encryptedKey) + len(iv) + len(cipher) + len(tag) + 4)
  49. buf.Write(protected)
  50. buf.WriteByte('.')
  51. buf.Write(encryptedKey)
  52. buf.WriteByte('.')
  53. buf.Write(iv)
  54. buf.WriteByte('.')
  55. buf.Write(cipher)
  56. buf.WriteByte('.')
  57. buf.Write(tag)
  58. result := make([]byte, buf.Len())
  59. copy(result, buf.Bytes())
  60. return result, nil
  61. }
  62. // JSON encodes the message into a JWE JSON serialization format.
  63. //
  64. // If `WithPrettyFormat(true)` is passed as an option, the returned
  65. // value will be formatted using `json.MarshalIndent()`
  66. func JSON(m *Message, options ...SerializerOption) ([]byte, error) {
  67. var pretty bool
  68. for _, option := range options {
  69. //nolint:forcetypeassert
  70. switch option.Ident() {
  71. case identPrettyFormat{}:
  72. pretty = option.Value().(bool)
  73. }
  74. }
  75. if pretty {
  76. return json.MarshalIndent(m, "", " ")
  77. }
  78. return json.Marshal(m)
  79. }