utils.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "net/http"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. "google.golang.org/protobuf/proto"
  9. "github.com/smallstep/certificates/api/log"
  10. )
  11. // EnableLogger is an interface that enables response logging for an object.
  12. type EnableLogger interface {
  13. ToLog() (interface{}, error)
  14. }
  15. // JSON writes the passed value into the http.ResponseWriter.
  16. func JSON(w http.ResponseWriter, v interface{}) {
  17. JSONStatus(w, v, http.StatusOK)
  18. }
  19. // JSONStatus writes the given value into the http.ResponseWriter and the
  20. // given status is written as the status code of the response.
  21. func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
  22. w.Header().Set("Content-Type", "application/json")
  23. w.WriteHeader(status)
  24. if err := json.NewEncoder(w).Encode(v); err != nil {
  25. log.Error(w, err)
  26. return
  27. }
  28. log.EnabledResponse(w, v)
  29. }
  30. // JSONNotFound writes a HTTP Not Found response with empty body.
  31. func JSONNotFound(w http.ResponseWriter) {
  32. w.Header().Set("Content-Type", "application/json")
  33. w.WriteHeader(http.StatusNotFound)
  34. log.EnabledResponse(w, nil)
  35. }
  36. // ProtoJSON writes the passed value into the http.ResponseWriter.
  37. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  38. ProtoJSONStatus(w, m, http.StatusOK)
  39. }
  40. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  41. // given status is written as the status code of the response.
  42. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  43. w.Header().Set("Content-Type", "application/json")
  44. w.WriteHeader(status)
  45. b, err := protojson.Marshal(m)
  46. if err != nil {
  47. log.Error(w, err)
  48. return
  49. }
  50. if _, err := w.Write(b); err != nil {
  51. log.Error(w, err)
  52. return
  53. }
  54. // log.EnabledResponse(w, v)
  55. }
  56. // ReadProtoJSONWithCheck reads JSON from the request body and stores it in the value
  57. // pointed by v. TODO(hs): move this to and integrate with render package.
  58. func ReadProtoJSONWithCheck(w http.ResponseWriter, r io.Reader, m proto.Message) bool {
  59. data, err := io.ReadAll(r)
  60. if err != nil {
  61. var wrapper = struct {
  62. Status int `json:"code"`
  63. Message string `json:"message"`
  64. }{
  65. Status: http.StatusBadRequest,
  66. Message: err.Error(),
  67. }
  68. errData, err := json.Marshal(wrapper)
  69. if err != nil {
  70. panic(err)
  71. }
  72. w.Header().Set("Content-Type", "application/json")
  73. w.WriteHeader(http.StatusBadRequest)
  74. w.Write(errData)
  75. return false
  76. }
  77. if err := protojson.Unmarshal(data, m); err != nil {
  78. if errors.Is(err, proto.Error) {
  79. var wrapper = struct {
  80. Message string `json:"message"`
  81. }{
  82. Message: err.Error(),
  83. }
  84. errData, err := json.Marshal(wrapper)
  85. if err != nil {
  86. panic(err)
  87. }
  88. w.Header().Set("Content-Type", "application/json")
  89. w.WriteHeader(http.StatusBadRequest)
  90. w.Write(errData)
  91. return false
  92. }
  93. // fallback to the default error writer
  94. WriteError(w, err)
  95. return false
  96. }
  97. return true
  98. }