From 4cd76ba21c2917d4906c7c9c12121fc630b44231 Mon Sep 17 00:00:00 2001 From: huangzhiran Date: Fri, 30 Aug 2024 18:17:32 +0800 Subject: [PATCH] post data support jwe decrypt --- cmd/sequencer/api/http.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/cmd/sequencer/api/http.go b/cmd/sequencer/api/http.go index 92a5dbf..f8464e1 100644 --- a/cmd/sequencer/api/http.go +++ b/cmd/sequencer/api/http.go @@ -2,6 +2,7 @@ package api import ( "context" + "encoding/base64" "io" "log/slog" "net/http" @@ -90,14 +91,35 @@ func (s *httpServer) verifyToken(c *gin.Context) { } func (s *httpServer) receiveDeviceData(c *gin.Context) { + payload, err := io.ReadAll(c.Request.Body) + if err != nil { + slog.Error("failed to read request body", "error", err) + c.JSON(http.StatusInternalServerError, apitypes.NewErrRsp(errors.Wrap(err, "failed to read request body"))) + return + } + defer c.Request.Body.Close() + + // decrypt did comm message client := clients.ClientIDFrom(c.Request.Context()) - data, err := io.ReadAll(c.Request.Body) + if client != nil { + slog.Info("decrypted payload", "payload", string(payload)) + payload, err = s.jwk.Decrypt(payload, client.DID()) + if err != nil { + slog.Error("failed to decrypt didcomm cipher data", "error", err) + c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to decrypt didcomm cipher data"))) + return + } + slog.Info("encrypted payload", "payload", string(payload)) + } + payload, err = base64.RawURLEncoding.DecodeString(string(payload)) if err != nil { - c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to read request body"))) + slog.Error("failed to decode base64 data", "error", err) + c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to decode base64 data"))) return } + e := &event.DeviceData{} - if err := e.Unmarshal(data); err != nil { + if err := e.Unmarshal(payload); err != nil { c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to unmarshal request body"))) return }