From d43f6b9f50e3c0e29474963bf274880ff631a08c Mon Sep 17 00:00:00 2001 From: Bob Stasyszyn Date: Fri, 5 Apr 2024 12:09:47 -0400 Subject: [PATCH] fix: Remove duplicate credential types in issuer policy request Signed-off-by: Bob Stasyszyn --- .../trustregistry/trustregistry_service.go | 15 ++++++++++++++- .../trustregistry/trustregistry_service_test.go | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pkg/service/trustregistry/trustregistry_service.go b/pkg/service/trustregistry/trustregistry_service.go index 906e330f6..468fe8ab9 100644 --- a/pkg/service/trustregistry/trustregistry_service.go +++ b/pkg/service/trustregistry/trustregistry_service.go @@ -81,7 +81,7 @@ func (s *Service) ValidateIssuance( req := &IssuancePolicyEvaluationRequest{ IssuerDID: profile.SigningDID.DID, - CredentialTypes: data.CredentialTypes, + CredentialTypes: removeDuplicates(data.CredentialTypes), } if data.AttestationVP != "" { @@ -281,3 +281,16 @@ func (s *Service) requestPolicyEvaluation( return result, nil } + +func removeDuplicates(items []string) []string { + var uniqueItems []string + + for _, item := range items { + _, dup := lo.Find(uniqueItems, func(v string) bool { return v == item }) + if !dup { + uniqueItems = append(uniqueItems, item) + } + } + + return uniqueItems +} diff --git a/pkg/service/trustregistry/trustregistry_service_test.go b/pkg/service/trustregistry/trustregistry_service_test.go index 76dec3c29..02e359ae5 100644 --- a/pkg/service/trustregistry/trustregistry_service_test.go +++ b/pkg/service/trustregistry/trustregistry_service_test.go @@ -10,6 +10,7 @@ package trustregistry_test import ( "bytes" "context" + "encoding/json" "errors" "io" "net/http" @@ -69,9 +70,10 @@ func TestService_ValidateIssuance(t *testing.T) { var proofChecker *checker.ProofChecker var ( - attestationVP string - nonce string - profile *profileapi.Issuer + attestationVP string + nonce string + credentialTypes []string + profile *profileapi.Issuer ) tests := []struct { @@ -86,6 +88,11 @@ func TestService_ValidateIssuance(t *testing.T) { httpClient.EXPECT().Do(gomock.Any()).DoAndReturn( func(req *http.Request) (*http.Response, error) { + payload := &trustregistry.IssuancePolicyEvaluationRequest{} + + require.NoError(t, json.NewDecoder(req.Body).Decode(payload)) + require.Equal(t, []string{"Credential1", "Credential2"}, payload.CredentialTypes) + return &http.Response{ StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewBufferString(`{"allowed":true}`)), @@ -99,6 +106,7 @@ func TestService_ValidateIssuance(t *testing.T) { // prepare wallet attestation VP (in jwt_vp format) signed by wallet DID attestationVP = createAttestationVP(t, attestationVC, walletProofCreator, issuerDID, testNonce) nonce = testNonce + credentialTypes = []string{"Credential1", "Credential2", "Credential1"} profile = createIssuerProfile(t) }, check: func(t *testing.T, err error) { @@ -320,7 +328,7 @@ func TestService_ValidateIssuance(t *testing.T) { &trustregistry.ValidateIssuanceData{ AttestationVP: attestationVP, Nonce: nonce, - CredentialTypes: nil, + CredentialTypes: credentialTypes, }, ), )