render.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Package render implements functionality related to response rendering.
  2. package render
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "github.com/pkg/errors"
  9. "google.golang.org/protobuf/encoding/protojson"
  10. "google.golang.org/protobuf/proto"
  11. "github.com/smallstep/certificates/acme"
  12. "github.com/smallstep/certificates/api/log"
  13. "github.com/smallstep/certificates/authority/admin"
  14. "github.com/smallstep/certificates/errs"
  15. "github.com/smallstep/certificates/logging"
  16. "github.com/smallstep/certificates/scep"
  17. )
  18. // JSON writes the passed value into the http.ResponseWriter.
  19. func JSON(w http.ResponseWriter, v interface{}) {
  20. JSONStatus(w, v, http.StatusOK)
  21. }
  22. // JSONStatus writes the given value into the http.ResponseWriter and the
  23. // given status is written as the status code of the response.
  24. func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
  25. w.Header().Set("Content-Type", "application/json")
  26. w.WriteHeader(status)
  27. if err := json.NewEncoder(w).Encode(v); err != nil {
  28. log.Error(w, err)
  29. return
  30. }
  31. log.EnabledResponse(w, v)
  32. }
  33. // ProtoJSON writes the passed value into the http.ResponseWriter.
  34. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  35. ProtoJSONStatus(w, m, http.StatusOK)
  36. }
  37. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  38. // given status is written as the status code of the response.
  39. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  40. w.Header().Set("Content-Type", "application/json")
  41. w.WriteHeader(status)
  42. b, err := protojson.Marshal(m)
  43. if err != nil {
  44. log.Error(w, err)
  45. return
  46. }
  47. if _, err := w.Write(b); err != nil {
  48. log.Error(w, err)
  49. return
  50. }
  51. // log.EnabledResponse(w, v)
  52. }
  53. // Error encodes the JSON representation of err to w.
  54. func Error(w http.ResponseWriter, err error) {
  55. switch k := err.(type) {
  56. case *acme.Error:
  57. acme.WriteError(w, k)
  58. return
  59. case *admin.Error:
  60. admin.WriteError(w, k)
  61. return
  62. case *scep.Error:
  63. w.Header().Set("Content-Type", "text/plain")
  64. default:
  65. w.Header().Set("Content-Type", "application/json")
  66. }
  67. cause := errors.Cause(err)
  68. if sc, ok := err.(errs.StatusCoder); ok {
  69. w.WriteHeader(sc.StatusCode())
  70. } else {
  71. if sc, ok := cause.(errs.StatusCoder); ok {
  72. w.WriteHeader(sc.StatusCode())
  73. } else {
  74. w.WriteHeader(http.StatusInternalServerError)
  75. }
  76. }
  77. // Write errors in the response writer
  78. if rl, ok := w.(logging.ResponseLogger); ok {
  79. rl.WithFields(map[string]interface{}{
  80. "error": err,
  81. })
  82. if os.Getenv("STEPDEBUG") == "1" {
  83. if e, ok := err.(errs.StackTracer); ok {
  84. rl.WithFields(map[string]interface{}{
  85. "stack-trace": fmt.Sprintf("%+v", e),
  86. })
  87. } else if e, ok := cause.(errs.StackTracer); ok {
  88. rl.WithFields(map[string]interface{}{
  89. "stack-trace": fmt.Sprintf("%+v", e),
  90. })
  91. }
  92. }
  93. }
  94. if err := json.NewEncoder(w).Encode(err); err != nil {
  95. log.Error(w, err)
  96. }
  97. }
  98. // BadRequest renders the JSON representation of err into w and sets its
  99. // status code to http.StatusBadRequest.
  100. func BadRequest(w http.ResponseWriter, err error) {
  101. codedError(w, http.StatusBadRequest, err)
  102. }
  103. func codedError(w http.ResponseWriter, code int, err error) {
  104. var wrapper = struct {
  105. Status int `json:"status"`
  106. Message string `json:"message"`
  107. }{
  108. Status: code,
  109. Message: err.Error(),
  110. }
  111. data, err := json.Marshal(wrapper)
  112. if err != nil {
  113. log.Error(w, err)
  114. return
  115. }
  116. w.Header().Set("Content-Type", "application/json")
  117. w.WriteHeader(code)
  118. w.Write(data)
  119. }