From 78c1928288d5b5432aabdf05f1c9b4d15da06b58 Mon Sep 17 00:00:00 2001 From: reinkrul Date: Fri, 29 Apr 2022 11:30:38 +0200 Subject: [PATCH] VCR: Make VCR.SearchVCs() according to spec (#1051) Co-authored-by: Rein Krul --- vcr/api/v2/api.go | 27 +++++++++++++++++---------- vcr/api/v2/registry.go | 7 +++++-- vcr/api/v2/registry_test.go | 4 ++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/vcr/api/v2/api.go b/vcr/api/v2/api.go index 4d857271bf..cc6cf4bb6e 100644 --- a/vcr/api/v2/api.go +++ b/vcr/api/v2/api.go @@ -177,17 +177,11 @@ func (w *Wrapper) SearchIssuedVCs(ctx echo.Context, params SearchIssuedVCsParams if err != nil { return err } - result := make([]SearchVCResult, len(foundVCs)) - for i, resolvedVC := range foundVCs { - var revocation *Revocation - revocation, err = w.VCR.Verifier().GetRevocation(*resolvedVC.ID) - if err != nil && !errors.Is(err, verifier.ErrNotFound) { - return err - } - result[i] = SearchVCResult{VerifiableCredential: resolvedVC, Revocation: revocation} + result, err := w.vcsWithRevocationsToSearchResults(foundVCs) + if err != nil { + return err } - - return ctx.JSON(http.StatusOK, SearchVCResults{VerifiableCredentials: result}) + return ctx.JSON(http.StatusOK, SearchVCResults{result}) } // VerifyVC handles API request to verify a Verifiable Credential. @@ -335,6 +329,19 @@ func (w *Wrapper) ListUntrusted(ctx echo.Context, credentialType string) error { return ctx.JSON(http.StatusOK, result) } +func (w *Wrapper) vcsWithRevocationsToSearchResults(foundVCs []vc.VerifiableCredential) ([]SearchVCResult, error) { + result := make([]SearchVCResult, len(foundVCs)) + for i, resolvedVC := range foundVCs { + var revocation *Revocation + revocation, err := w.VCR.Verifier().GetRevocation(*resolvedVC.ID) + if err != nil && !errors.Is(err, verifier.ErrNotFound) { + return nil, nil + } + result[i] = SearchVCResult{VerifiableCredential: resolvedVC, Revocation: revocation} + } + return result, nil +} + type trustChangeFunc func(ssi.URI, ssi.URI) error func changeTrust(ctx echo.Context, f trustChangeFunc) error { diff --git a/vcr/api/v2/registry.go b/vcr/api/v2/registry.go index d55ab29ddd..23404fe73a 100644 --- a/vcr/api/v2/registry.go +++ b/vcr/api/v2/registry.go @@ -116,8 +116,11 @@ func (w *Wrapper) searchOrgs(ctx echo.Context, allowUntrusted bool, vcQuery vc.V if err != nil { return err } - - return ctx.JSON(http.StatusOK, results) + searchResults, err := w.vcsWithRevocationsToSearchResults(results) + if err != nil { + return err + } + return ctx.JSON(http.StatusOK, SearchVCResults{searchResults}) } func (w *Wrapper) searchAuths(ctx echo.Context, allowUntrusted bool, vcQuery vc.VerifiableCredential) error { diff --git a/vcr/api/v2/registry_test.go b/vcr/api/v2/registry_test.go index a4b5eaab2f..6b6a84577e 100644 --- a/vcr/api/v2/registry_test.go +++ b/vcr/api/v2/registry_test.go @@ -136,7 +136,7 @@ func TestWrapper_SearchVCs(t *testing.T) { return []VerifiableCredential{}, nil }, ) - ctx.echo.EXPECT().JSON(http.StatusOK, []VerifiableCredential{}) + ctx.echo.EXPECT().JSON(http.StatusOK, SearchVCResults{[]SearchVCResult{}}) err := ctx.client.SearchVCs(ctx.echo) @@ -179,7 +179,7 @@ func TestWrapper_SearchVCs(t *testing.T) { _ = json.Unmarshal([]byte(untrustedOrganizationQuery), f) }) ctx.vcr.EXPECT().Search(context.Background(), gomock.Any(), true, nil).Return([]VerifiableCredential{}, nil) - ctx.echo.EXPECT().JSON(http.StatusOK, []VerifiableCredential{}) + ctx.echo.EXPECT().JSON(http.StatusOK, SearchVCResults{[]SearchVCResult{}}) err := ctx.client.SearchVCs(ctx.echo)