format.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package jwx
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. )
  6. type FormatKind int
  7. const (
  8. UnknownFormat FormatKind = iota
  9. JWE
  10. JWS
  11. JWK
  12. JWKS
  13. JWT
  14. )
  15. type formatHint struct {
  16. Payload json.RawMessage `json:"payload"` // Only in JWS
  17. Signatures json.RawMessage `json:"signatures"` // Only in JWS
  18. Ciphertext json.RawMessage `json:"ciphertext"` // Only in JWE
  19. KeyType json.RawMessage `json:"kty"` // Only in JWK
  20. Keys json.RawMessage `json:"keys"` // Only in JWKS
  21. Audience json.RawMessage `json:"aud"` // Only in JWT
  22. }
  23. // GuessFormat is used to guess the format the given payload is in
  24. // using heuristics. See the type FormatKind for a full list of
  25. // possible types.
  26. //
  27. // This may be useful in determining your next action when you may
  28. // encounter a payload that could either be a JWE, JWS, or a plain JWT.
  29. //
  30. // Because JWTs are almost always JWS signed, you may be thrown off
  31. // if you pass what you think is a JWT payload to this function.
  32. // If the function is in the "Compact" format, it means it's a JWS
  33. // signed message, and its payload is the JWT. Therefore this function
  34. // will reuturn JWS, not JWT.
  35. //
  36. // This function requires an extra parsing of the payload, and therefore
  37. // may be inefficient if you call it every time before parsing.
  38. func GuessFormat(payload []byte) FormatKind {
  39. // The check against kty, keys, and aud are something this library
  40. // made up. for the distinctions between JWE and JWS, we used
  41. // https://datatracker.ietf.org/doc/html/rfc7516#section-9.
  42. //
  43. // The above RFC described several ways to distinguish between
  44. // a JWE and JWS JSON, but we're only using one of them
  45. payload = bytes.TrimSpace(payload)
  46. if len(payload) <= 0 {
  47. return UnknownFormat
  48. }
  49. if payload[0] != '{' {
  50. // Compact format. It's probably a JWS or JWE
  51. sep := []byte{'.'} // I want to const this :/
  52. // Note: this counts the number of occurrences of the
  53. // separator, but the RFC talks about the number of segments.
  54. // number of '.' == segments - 1, so that's why we have 2 and 4 here
  55. switch count := bytes.Count(payload, sep); count {
  56. case 2:
  57. return JWS
  58. case 4:
  59. return JWE
  60. default:
  61. return UnknownFormat
  62. }
  63. }
  64. // If we got here, we probably have JSON.
  65. var h formatHint
  66. if err := json.Unmarshal(payload, &h); err != nil {
  67. return UnknownFormat
  68. }
  69. if h.Audience != nil {
  70. return JWT
  71. }
  72. if h.KeyType != nil {
  73. return JWK
  74. }
  75. if h.Keys != nil {
  76. return JWKS
  77. }
  78. if h.Ciphertext != nil {
  79. return JWE
  80. }
  81. if h.Signatures != nil && h.Payload != nil {
  82. return JWS
  83. }
  84. return UnknownFormat
  85. }