This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into support-key-access-in…
…-all-services-ose-122 * origin/main: SIP-4: DWN Routes (#113) Update README.md Add Sip 4 to table (#109) Adding SIP4 Doc (#108) # Conflicts: # config/compose.toml # config/config.go # config/config.toml # go.mod # go.sum # pkg/server/server_test.go # pkg/service/service.go
- Loading branch information
Showing
18 changed files
with
616 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dwn | ||
|
||
import ( | ||
"bytes" | ||
"github.com/TBD54566975/ssi-sdk/credential/manifest" | ||
"github.com/goccy/go-json" | ||
"github.com/tbd54566975/ssi-service/internal/util" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type DWNPublishManifestRequest struct { | ||
Manifest manifest.CredentialManifest `json:"manifest" validate:"required"` | ||
} | ||
|
||
type DWNPublishManifestResponse struct { | ||
Status int `json:"status" validate:"required"` | ||
Response string `json:"response" validate:"required"` | ||
} | ||
|
||
// PublishManifest publishes a CredentialManifest to a DWN | ||
func PublishManifest(endpoint string, manifest manifest.CredentialManifest) (*DWNPublishManifestResponse, error) { | ||
|
||
dwnReq := DWNPublishManifestRequest{Manifest: manifest} | ||
postResp, err := Post(endpoint, dwnReq) | ||
|
||
if err != nil { | ||
return nil, util.LoggingErrorMsg(err, "problem with posting to dwn") | ||
} | ||
|
||
defer postResp.Body.Close() | ||
|
||
b, _ := io.ReadAll(postResp.Body) | ||
body := string(b) | ||
|
||
return &DWNPublishManifestResponse{Status: postResp.StatusCode, Response: body}, nil | ||
|
||
} | ||
|
||
// Post does a post request with data to provided endpoint | ||
func Post(endpoint string, data interface{}) (*http.Response, error) { | ||
// convert response payload to json | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := http.Post(endpoint, "application/json", bytes.NewBuffer(jsonData)) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dwn | ||
|
||
import ( | ||
"github.com/TBD54566975/ssi-sdk/credential/exchange" | ||
manifestsdk "github.com/TBD54566975/ssi-sdk/credential/manifest" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestPublishManifest(t *testing.T) { | ||
t.Run("Test Publish Manifest", func(tt *testing.T) { | ||
|
||
resp, err := PublishManifest("test-endpoint", getValidManifest()) | ||
assert.Nil(tt, resp) | ||
assert.Error(tt, err) | ||
assert.ErrorContains(tt, err, "problem with posting to dwn") | ||
}) | ||
} | ||
|
||
func getValidManifest() manifestsdk.CredentialManifest { | ||
|
||
return manifestsdk.CredentialManifest{ | ||
ID: "WA-DL-CLASS-A", | ||
SpecVersion: "https://identity.foundation/credential-manifest/spec/v1.0.0/", | ||
Issuer: manifestsdk.Issuer{ | ||
ID: "did:abc:123", | ||
}, | ||
PresentationDefinition: &exchange.PresentationDefinition{ | ||
ID: "pres-def-id", | ||
InputDescriptors: []exchange.InputDescriptor{ | ||
{ | ||
ID: "test-id", | ||
Constraints: &exchange.Constraints{ | ||
Fields: []exchange.Field{ | ||
{ | ||
Path: []string{".vc.id"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
OutputDescriptors: []manifestsdk.OutputDescriptor{ | ||
{ | ||
ID: "id1", | ||
Schema: "https://test.com/schema", | ||
Name: "good ID", | ||
Description: "it's all good", | ||
}, | ||
{ | ||
ID: "id2", | ||
Schema: "https://test.com/schema", | ||
Name: "good ID", | ||
Description: "it's all good", | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package router | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/TBD54566975/ssi-sdk/credential/manifest" | ||
"github.com/tbd54566975/ssi-service/pkg/service/dwn" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
||
dwnpkg "github.com/tbd54566975/ssi-service/pkg/dwn" | ||
"github.com/tbd54566975/ssi-service/pkg/server/framework" | ||
svcframework "github.com/tbd54566975/ssi-service/pkg/service/framework" | ||
) | ||
|
||
type DWNRouter struct { | ||
service *dwn.Service | ||
} | ||
|
||
func NewDWNRouter(s svcframework.Service) (*DWNRouter, error) { | ||
if s == nil { | ||
return nil, errors.New("service cannot be nil") | ||
} | ||
dwnService, ok := s.(*dwn.Service) | ||
if !ok { | ||
return nil, fmt.Errorf("could not create dwn router with service type: %s", s.Type()) | ||
} | ||
|
||
return &DWNRouter{ | ||
service: dwnService, | ||
}, nil | ||
} | ||
|
||
type PublishManifestRequest struct { | ||
ManifestID string `json:"manifestId" validate:"required"` | ||
} | ||
|
||
func (req PublishManifestRequest) ToServiceRequest() dwn.DWNPublishManifestRequest { | ||
return dwn.DWNPublishManifestRequest{ | ||
ManifestID: req.ManifestID, | ||
} | ||
} | ||
|
||
type PublishManifestResponse struct { | ||
Manifest manifest.CredentialManifest `json:"manifest" validate:"required"` | ||
DWNResponse dwnpkg.DWNPublishManifestResponse `json:"dwnResponse" validate:"required"` | ||
} | ||
|
||
// PublishManifest godoc | ||
// @Summary Public Manifest to DWN | ||
// @Description Public Manifest to DWN | ||
// @Tags DWNAPI | ||
// @Accept json | ||
// @Produce json | ||
// @Param request body PublishManifestRequest true "request body" | ||
// @Success 201 {object} PublishManifestResponse | ||
// @Failure 400 {string} string "Bad request" | ||
// @Failure 500 {string} string "Internal server error" | ||
// @Router /v1/dwn/manifest [put] | ||
func (dwnr DWNRouter) PublishManifest(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
|
||
if dwnr.service.Config().DWNEndpoint == "" { | ||
errMsg := "could not publish manifest to dwn because dwn endpoint is not configured" | ||
logrus.Error(errMsg) | ||
return framework.NewRequestError(errors.New(errMsg), http.StatusInternalServerError) | ||
} | ||
|
||
var request PublishManifestRequest | ||
if err := framework.Decode(r, &request); err != nil { | ||
errMsg := "invalid publish manifest message request" | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest) | ||
} | ||
|
||
req := request.ToServiceRequest() | ||
publishManifestResponse, err := dwnr.service.GetManifest(req) | ||
|
||
if err != nil || &publishManifestResponse.Manifest == nil { | ||
errMsg := "could not retrieve manifest" | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusInternalServerError) | ||
} | ||
|
||
dwnResp, err := dwnpkg.PublishManifest(dwnr.service.Config().DWNEndpoint, publishManifestResponse.Manifest) | ||
|
||
if err != nil { | ||
errMsg := "could not publish manifest to DWN" | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusInternalServerError) | ||
} | ||
|
||
resp := PublishManifestResponse{Manifest: publishManifestResponse.Manifest, DWNResponse: *dwnResp} | ||
return framework.Respond(ctx, w, resp, http.StatusAccepted) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package router | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tbd54566975/ssi-service/config" | ||
"github.com/tbd54566975/ssi-service/pkg/service/dwn" | ||
"github.com/tbd54566975/ssi-service/pkg/service/framework" | ||
"github.com/tbd54566975/ssi-service/pkg/storage" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestDWNRouter(t *testing.T) { | ||
// remove the db file after the test | ||
t.Cleanup(func() { | ||
_ = os.Remove(storage.DBFile) | ||
}) | ||
|
||
t.Run("Nil Service", func(tt *testing.T) { | ||
dwnRouter, err := NewDWNRouter(nil) | ||
assert.Error(tt, err) | ||
assert.Empty(tt, dwnRouter) | ||
assert.Contains(tt, err.Error(), "service cannot be nil") | ||
}) | ||
|
||
t.Run("Bad Service", func(tt *testing.T) { | ||
dwnRouter, err := NewDWNRouter(&testService{}) | ||
assert.Error(tt, err) | ||
assert.Empty(tt, dwnRouter) | ||
assert.Contains(tt, err.Error(), "could not create dwn router with service type: test") | ||
}) | ||
|
||
t.Run("DWN Service Test", func(tt *testing.T) { | ||
bolt, err := storage.NewBoltDB() | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, bolt) | ||
|
||
serviceConfig := config.DWNServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "dwn"}} | ||
dwnService, err := dwn.NewDWNService(serviceConfig, bolt) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, dwnService) | ||
|
||
// check type and status | ||
assert.Equal(tt, framework.DWN, dwnService.Type()) | ||
assert.Equal(tt, framework.StatusReady, dwnService.Status().Status) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.