forked from TrueCloudLab/certificates
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
// Package read implements request object readers.
|
|
package read
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/smallstep/certificates/api/render"
|
|
"github.com/smallstep/certificates/errs"
|
|
)
|
|
|
|
// JSON reads JSON from the request body and stores it in the value
|
|
// pointed by v.
|
|
func JSON(r io.Reader, v interface{}) error {
|
|
if err := json.NewDecoder(r).Decode(v); err != nil {
|
|
return errs.BadRequestErr(err, "error decoding json")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ProtoJSON reads JSON from the request body and stores it in the value
|
|
// pointed to by m.
|
|
func ProtoJSON(r io.Reader, m proto.Message) error {
|
|
data, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return errs.BadRequestErr(err, "error reading request body")
|
|
}
|
|
|
|
switch err := protojson.Unmarshal(data, m); {
|
|
case errors.Is(err, proto.Error):
|
|
// trim the proto prefix for the message
|
|
s := strings.TrimSpace(strings.TrimPrefix(err.Error(), "proto:"))
|
|
return badProtoJSONError(s)
|
|
default:
|
|
return err
|
|
}
|
|
}
|
|
|
|
// badProtoJSONError is an error type that is returned by ProtoJSON
|
|
// when a proto message cannot be unmarshaled. Usually this is caused
|
|
// by an error in the request body.
|
|
type badProtoJSONError string
|
|
|
|
// Error implements error for badProtoJSONError
|
|
func (e badProtoJSONError) Error() string {
|
|
return string(e)
|
|
}
|
|
|
|
// Render implements render.RenderableError for badProtoJSONError
|
|
func (e badProtoJSONError) Render(w http.ResponseWriter) {
|
|
v := struct {
|
|
Type string `json:"type"`
|
|
Detail string `json:"detail"`
|
|
Message string `json:"message"`
|
|
}{
|
|
Type: "badRequest",
|
|
Detail: "bad request",
|
|
Message: e.Error(),
|
|
}
|
|
render.JSONStatus(w, v, http.StatusBadRequest)
|
|
}
|