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 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIP 2 - Create Credential Manifests CRUD (#91)
* wip * adding complete manifest crud route * cleaning up test * changing model to have concrete definition instead of mapping Co-authored-by: Neal Roessler <[email protected]> Co-authored-by: Gabe <[email protected]>
- Loading branch information
1 parent
ee6df8d
commit 62b812c
Showing
18 changed files
with
947 additions
and
18 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,184 @@ | ||
package router | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/TBD54566975/ssi-sdk/credential/exchange" | ||
manifestsdk "github.com/TBD54566975/ssi-sdk/credential/manifest" | ||
"github.com/tbd54566975/ssi-service/pkg/service/manifest" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/tbd54566975/ssi-service/pkg/server/framework" | ||
svcframework "github.com/tbd54566975/ssi-service/pkg/service/framework" | ||
) | ||
|
||
type ManifestRouter struct { | ||
service *manifest.Service | ||
} | ||
|
||
func NewManifestRouter(s svcframework.Service) (*ManifestRouter, error) { | ||
if s == nil { | ||
return nil, errors.New("service cannot be nil") | ||
} | ||
credService, ok := s.(*manifest.Service) | ||
if !ok { | ||
return nil, fmt.Errorf("could not create manifest router with service type: %s", s.Type()) | ||
} | ||
return &ManifestRouter{ | ||
service: credService, | ||
}, nil | ||
} | ||
|
||
type CreateManifestRequest struct { | ||
Issuer string `json:"issuer" validate:"required"` | ||
// A context is optional. If not present, we'll apply default, required context values. | ||
Context string `json:"@context"` | ||
OutputDescriptors []manifestsdk.OutputDescriptor `json:"outputDescriptors" validate:"required"` | ||
PresentationDefinition exchange.PresentationDefinition `json:"presentationDefinition" validate:"required"` | ||
} | ||
|
||
func (c CreateManifestRequest) ToServiceRequest() manifest.CreateManifestRequest { | ||
return manifest.CreateManifestRequest{ | ||
Issuer: c.Issuer, | ||
Context: c.Context, | ||
OutputDescriptors: c.OutputDescriptors, | ||
PresentationDefinition: c.PresentationDefinition, | ||
} | ||
} | ||
|
||
type CreateManifestResponse struct { | ||
Manifest manifestsdk.CredentialManifest `json:"manifest"` | ||
} | ||
|
||
// CreateManifest godoc | ||
// @Summary Create manifest | ||
// @Description Create manifest | ||
// @Tags ManifestAPI | ||
// @Accept json | ||
// @Produce json | ||
// @Param request body CreateManifestRequest true "request body" | ||
// @Success 201 {object} CreateManifestResponse | ||
// @Failure 400 {string} string "Bad request" | ||
// @Failure 500 {string} string "Internal server error" | ||
// @Router /v1/manifests [put] | ||
func (mr ManifestRouter) CreateManifest(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
var request CreateManifestRequest | ||
if err := framework.Decode(r, &request); err != nil { | ||
errMsg := "invalid create manifest request" | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest) | ||
} | ||
|
||
req := request.ToServiceRequest() | ||
createManifestResponse, err := mr.service.CreateManifest(req) | ||
if err != nil { | ||
errMsg := "could not create manifest" | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusInternalServerError) | ||
} | ||
|
||
resp := CreateManifestResponse{Manifest: createManifestResponse.Manifest} | ||
|
||
return framework.Respond(ctx, w, resp, http.StatusCreated) | ||
} | ||
|
||
type GetManifestResponse struct { | ||
ID string `json:"id"` | ||
Manifest manifestsdk.CredentialManifest `json:"manifest"` | ||
} | ||
|
||
// GetManifest godoc | ||
// @Summary Get manifest | ||
// @Description Get manifest by id | ||
// @Tags ManifestAPI | ||
// @Accept json | ||
// @Produce json | ||
// @Param id path string true "ID" | ||
// @Success 200 {object} GetManifestResponse | ||
// @Failure 400 {string} string "Bad request" | ||
// @Router /v1/manifests/{id} [get] | ||
func (mr ManifestRouter) GetManifest(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
id := framework.GetParam(ctx, IDParam) | ||
if id == nil { | ||
errMsg := "cannot get manifest without ID parameter" | ||
logrus.Error(errMsg) | ||
return framework.NewRequestErrorMsg(errMsg, http.StatusBadRequest) | ||
} | ||
|
||
gotManifest, err := mr.service.GetManifest(manifest.GetManifestRequest{ID: *id}) | ||
if err != nil { | ||
errMsg := fmt.Sprintf("could not get manifest with id: %s", *id) | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest) | ||
} | ||
|
||
resp := GetManifestResponse{ | ||
ID: gotManifest.Manifest.ID, | ||
Manifest: gotManifest.Manifest, | ||
} | ||
return framework.Respond(ctx, w, resp, http.StatusOK) | ||
} | ||
|
||
type GetManifestsResponse struct { | ||
Manifests []manifestsdk.CredentialManifest `json:"manifests"` | ||
} | ||
|
||
// GetManifests godoc | ||
// @Summary Get manifests | ||
// @Description Checks for the presence of a query parameter and calls the associated filtered get method | ||
// @Tags ManifestAPI | ||
// @Accept json | ||
// @Produce json | ||
// @Param issuer query string false "string issuer" | ||
// @Param schema query string false "string schema" | ||
// @Param subject query string false "string subject" | ||
// @Success 200 {object} GetManifestsResponse | ||
// @Failure 400 {string} string "Bad request" | ||
// @Failure 500 {string} string "Internal server error" | ||
// @Router /v1/manifests [get] | ||
func (mr ManifestRouter) GetManifests(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
gotManifests, err := mr.service.GetManifests() | ||
|
||
if err != nil { | ||
errMsg := fmt.Sprintf("could not get manifests") | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusBadRequest) | ||
} | ||
|
||
resp := GetManifestsResponse{ | ||
Manifests: gotManifests.Manifests, | ||
} | ||
|
||
return framework.Respond(ctx, w, resp, http.StatusOK) | ||
} | ||
|
||
// DeleteManifest godoc | ||
// @Summary Delete manifests | ||
// @Description Delete manifest by ID | ||
// @Tags ManifestAPI | ||
// @Accept json | ||
// @Produce json | ||
// @Param id path string true "ID" | ||
// @Success 200 {string} string "OK" | ||
// @Failure 400 {string} string "Bad request" | ||
// @Failure 500 {string} string "Internal server error" | ||
// @Router /v1/manifests/{id} [delete] | ||
func (mr ManifestRouter) DeleteManifest(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
id := framework.GetParam(ctx, IDParam) | ||
if id == nil { | ||
errMsg := "cannot delete manifest without ID parameter" | ||
logrus.Error(errMsg) | ||
return framework.NewRequestErrorMsg(errMsg, http.StatusBadRequest) | ||
} | ||
|
||
if err := mr.service.DeleteManifest(manifest.DeleteManifestRequest{ID: *id}); err != nil { | ||
errMsg := fmt.Sprintf("could not delete manifest with id: %s", *id) | ||
logrus.WithError(err).Error(errMsg) | ||
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusInternalServerError) | ||
} | ||
|
||
return framework.Respond(ctx, w, nil, http.StatusOK) | ||
} |
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,95 @@ | ||
package router | ||
|
||
import ( | ||
"github.com/TBD54566975/ssi-sdk/credential/exchange" | ||
manifestsdk "github.com/TBD54566975/ssi-sdk/credential/manifest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/tbd54566975/ssi-service/config" | ||
"github.com/tbd54566975/ssi-service/pkg/service/framework" | ||
"github.com/tbd54566975/ssi-service/pkg/service/manifest" | ||
"github.com/tbd54566975/ssi-service/pkg/storage" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestManifestRouter(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) { | ||
manifestRouter, err := NewManifestRouter(nil) | ||
assert.Error(tt, err) | ||
assert.Empty(tt, manifestRouter) | ||
assert.Contains(tt, err.Error(), "service cannot be nil") | ||
}) | ||
|
||
t.Run("Bad Service", func(tt *testing.T) { | ||
manifestRouter, err := NewManifestRouter(&testService{}) | ||
assert.Error(tt, err) | ||
assert.Empty(tt, manifestRouter) | ||
assert.Contains(tt, err.Error(), "could not create manifest router with service type: test") | ||
}) | ||
|
||
t.Run("Manifest Service Test", func(tt *testing.T) { | ||
bolt, err := storage.NewBoltDB() | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, bolt) | ||
|
||
serviceConfig := config.ManifestServiceConfig{BaseServiceConfig: &config.BaseServiceConfig{Name: "manifest"}} | ||
manifestService, err := manifest.NewManifestService(serviceConfig, bolt) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, manifestService) | ||
|
||
// check type and status | ||
assert.Equal(tt, framework.Manifest, manifestService.Type()) | ||
assert.Equal(tt, framework.StatusReady, manifestService.Status().Status) | ||
|
||
// good request | ||
createManifestRequest := getValidManifestRequest() | ||
|
||
createdManifest, err := manifestService.CreateManifest(createManifestRequest) | ||
assert.NoError(tt, err) | ||
assert.NotEmpty(tt, createdManifest) | ||
assert.NotEmpty(tt, createdManifest.Manifest) | ||
}) | ||
} | ||
|
||
func getValidManifestRequest() manifest.CreateManifestRequest { | ||
createManifestRequest := manifest.CreateManifestRequest{ | ||
Issuer: "did:abc:123", | ||
Context: "context123", | ||
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", | ||
}, | ||
}, | ||
} | ||
|
||
return createManifestRequest | ||
} |
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
Oops, something went wrong.