Skip to content

Commit

Permalink
Merge pull request #1692 from bstasyszyn/remove-dup-cred-types
Browse files Browse the repository at this point in the history
fix: Remove duplicate credential types in issuer policy request
  • Loading branch information
bstasyszyn authored Apr 5, 2024
2 parents 9a91885 + d43f6b9 commit b90afe6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 14 additions & 1 deletion pkg/service/trustregistry/trustregistry_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *Service) ValidateIssuance(

req := &IssuancePolicyEvaluationRequest{
IssuerDID: profile.SigningDID.DID,
CredentialTypes: data.CredentialTypes,
CredentialTypes: removeDuplicates(data.CredentialTypes),
}

if data.AttestationVP != "" {
Expand Down Expand Up @@ -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
}
16 changes: 12 additions & 4 deletions pkg/service/trustregistry/trustregistry_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package trustregistry_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
Expand Down Expand Up @@ -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 {
Expand All @@ -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}`)),
Expand All @@ -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) {
Expand Down Expand Up @@ -320,7 +328,7 @@ func TestService_ValidateIssuance(t *testing.T) {
&trustregistry.ValidateIssuanceData{
AttestationVP: attestationVP,
Nonce: nonce,
CredentialTypes: nil,
CredentialTypes: credentialTypes,
},
),
)
Expand Down

0 comments on commit b90afe6

Please sign in to comment.