Skip to content

Commit

Permalink
post data support jwe decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran committed Aug 30, 2024
1 parent 6cae2da commit 4cd76ba
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cmd/sequencer/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"encoding/base64"
"io"
"log/slog"
"net/http"
Expand Down Expand Up @@ -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

Check warning on line 98 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L94-L98

Added lines #L94 - L98 were not covered by tests
}
defer c.Request.Body.Close()

Check warning on line 100 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L100

Added line #L100 was not covered by tests

// 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

Check warning on line 110 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L104-L110

Added lines #L104 - L110 were not covered by tests
}
slog.Info("encrypted payload", "payload", string(payload))

Check warning on line 112 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L112

Added line #L112 was not covered by tests
}
payload, err = base64.RawURLEncoding.DecodeString(string(payload))

Check warning on line 114 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L114

Added line #L114 was not covered by tests
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")))

Check warning on line 117 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L116-L117

Added lines #L116 - L117 were not covered by tests
return
}

e := &event.DeviceData{}
if err := e.Unmarshal(data); err != nil {
if err := e.Unmarshal(payload); err != nil {

Check warning on line 122 in cmd/sequencer/api/http.go

View check run for this annotation

Codecov / codecov/patch

cmd/sequencer/api/http.go#L122

Added line #L122 was not covered by tests
c.JSON(http.StatusBadRequest, apitypes.NewErrRsp(errors.Wrap(err, "failed to unmarshal request body")))
return
}
Expand Down

0 comments on commit 4cd76ba

Please sign in to comment.