diff --git a/vcr/pe/presentation_submission.go b/vcr/pe/presentation_submission.go index 3fa9a2aaf9..fdd1bf6cdc 100644 --- a/vcr/pe/presentation_submission.go +++ b/vcr/pe/presentation_submission.go @@ -177,21 +177,10 @@ func (b *PresentationSubmissionBuilder) Build(format string) (PresentationSubmis // Resolve returns a map where each of the input descriptors is mapped to the corresponding VerifiableCredential. // If an input descriptor can't be mapped to a VC, an error is returned. // This function is specified by https://identity.foundation/presentation-exchange/#processing-of-submission-entries -func (s PresentationSubmission) Resolve(envelope interface{}) (map[string]vc.VerifiableCredential, error) { - switch envelope.(type) { - case []interface{}: - // list of VPs - case map[string]interface{}: - // single VP (JSON) - case string: - // single VP (JWT) - default: - return nil, errors.New("invalid Presentation Exchange envelope") - } - +func (s PresentationSubmission) Resolve(envelope Envelope) (map[string]vc.VerifiableCredential, error) { result := make(map[string]vc.VerifiableCredential) for _, inputDescriptor := range s.DescriptorMap { - resolvedCredential, err := resolveCredential(nil, inputDescriptor, envelope) + resolvedCredential, err := resolveCredential(nil, inputDescriptor, envelope.asInterface) if err != nil { return nil, fmt.Errorf("unable to resolve credential for input descriptor '%s': %w", inputDescriptor.Id, err) } @@ -261,7 +250,7 @@ func resolveCredential(path []string, mapping InputDescriptorMappingObject, valu // The Presentation Definitions are passed in the envelope, as specified by the PEX specification. // It assumes credentials of the presentations only map in 1 way to the input descriptors. func (s PresentationSubmission) Validate(envelope Envelope, definition PresentationDefinition) (map[string]vc.VerifiableCredential, error) { - actualCredentials, err := s.Resolve(envelope.Interface) + actualCredentials, err := s.Resolve(envelope) if err != nil { return nil, fmt.Errorf("resolve credentials from presentation submission: %w", err) } diff --git a/vcr/pe/presentation_submission_test.go b/vcr/pe/presentation_submission_test.go index 25d52d3872..8724e2b0ee 100644 --- a/vcr/pe/presentation_submission_test.go +++ b/vcr/pe/presentation_submission_test.go @@ -212,7 +212,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, vp).Interface) + credentials, err := submission.Resolve(toEnvelope(t, vp)) require.NoError(t, err) assert.Len(t, credentials, 1) @@ -241,7 +241,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, vp).Interface) + credentials, err := submission.Resolve(toEnvelope(t, vp)) require.NoError(t, err) assert.Len(t, credentials, 2) @@ -284,7 +284,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, []interface{}{vp1, vp2}).Interface) + credentials, err := submission.Resolve(toEnvelope(t, []interface{}{vp1, vp2})) require.NoError(t, err) assert.Len(t, credentials, 2) @@ -309,7 +309,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, vp).Interface) + credentials, err := submission.Resolve(toEnvelope(t, vp)) require.EqualError(t, err, "unable to resolve credential for input descriptor '1': path '$.verifiableCredential' does not reference a credential") assert.Nil(t, credentials) @@ -332,7 +332,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, vp).Interface) + credentials, err := submission.Resolve(toEnvelope(t, vp)) require.ErrorContains(t, err, "unable to resolve credential for input descriptor '1': invalid JSON-LD credential at path") assert.Nil(t, credentials) @@ -355,7 +355,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, vp).Interface) + credentials, err := submission.Resolve(toEnvelope(t, vp)) require.ErrorContains(t, err, "unable to resolve credential for input descriptor '1': invalid JSON-LD presentation at path") assert.Nil(t, credentials) @@ -378,7 +378,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) { var submission PresentationSubmission require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission)) - credentials, err := submission.Resolve(toEnvelope(t, vp).Interface) + credentials, err := submission.Resolve(toEnvelope(t, vp)) assert.EqualError(t, err, "unable to resolve credential for input descriptor '1': value of Go type 'string' at path '$.verifiableCredential.expirationDate' can't be decoded using format 'ldp_vc'") assert.Nil(t, credentials) diff --git a/vcr/pe/util.go b/vcr/pe/util.go index 28c7dd5b5e..6600a12fe5 100644 --- a/vcr/pe/util.go +++ b/vcr/pe/util.go @@ -25,9 +25,11 @@ import ( "github.com/nuts-foundation/go-did/vc" ) +// Envelope is a parsed Presentation Exchange envelope, containing zero or more Verifiable Presentations that are referenced by the Presentation Submission. type Envelope struct { - Interface interface{} + // Presentations contains the Verifiable Presentations that were parsed from the envelope. Presentations []vc.VerifiablePresentation + asInterface interface{} } // ParseEnvelope parses a Presentation Exchange envelope, which is a JSON type that encompasses zero or more Verifiable Presentations. @@ -46,7 +48,7 @@ func ParseEnvelope(envelopeBytes []byte) (*Envelope, error) { return nil, err } return &Envelope{ - Interface: asInterface, + asInterface: asInterface, Presentations: presentations, }, nil } @@ -56,7 +58,7 @@ func ParseEnvelope(envelopeBytes []byte) (*Envelope, error) { return nil, err } return &Envelope{ - Interface: asInterface, + asInterface: asInterface, Presentations: []vc.VerifiablePresentation{*presentation}, }, nil } diff --git a/vcr/pe/util_test.go b/vcr/pe/util_test.go index 87bafef08c..291e8f0054 100644 --- a/vcr/pe/util_test.go +++ b/vcr/pe/util_test.go @@ -33,7 +33,7 @@ func TestParseEnvelope(t *testing.T) { presentation := test.CreateJWTPresentation(t, did.MustParseDID("did:example:1"), credential.ValidNutsOrganizationCredential(t)) envelope, err := ParseEnvelope([]byte(presentation.Raw())) require.NoError(t, err) - require.Equal(t, presentation.ID.String(), envelope.Interface.(map[string]interface{})["id"]) + require.Equal(t, presentation.ID.String(), envelope.asInterface.(map[string]interface{})["id"]) require.Len(t, envelope.Presentations, 1) }) t.Run("invalid JWT", func(t *testing.T) { @@ -44,7 +44,7 @@ func TestParseEnvelope(t *testing.T) { t.Run("JSON object", func(t *testing.T) { envelope, err := ParseEnvelope([]byte(`{"id": "value"}`)) require.NoError(t, err) - require.Equal(t, map[string]interface{}{"id": "value"}, envelope.Interface) + require.Equal(t, map[string]interface{}{"id": "value"}, envelope.asInterface) require.Len(t, envelope.Presentations, 1) }) t.Run("invalid VP as JSON object", func(t *testing.T) { @@ -55,7 +55,7 @@ func TestParseEnvelope(t *testing.T) { t.Run("JSON array with objects", func(t *testing.T) { envelope, err := ParseEnvelope([]byte(`[{"id": "value"}]`)) require.NoError(t, err) - require.Equal(t, []interface{}{map[string]interface{}{"id": "value"}}, envelope.Interface) + require.Equal(t, []interface{}{map[string]interface{}{"id": "value"}}, envelope.asInterface) require.Len(t, envelope.Presentations, 1) }) t.Run("JSON array with JWTs", func(t *testing.T) { @@ -64,7 +64,7 @@ func TestParseEnvelope(t *testing.T) { listJSON, _ := json.Marshal(presentations) envelope, err := ParseEnvelope(listJSON) require.NoError(t, err) - require.Len(t, envelope.Interface, 2) + require.Len(t, envelope.asInterface, 2) require.Len(t, envelope.Presentations, 2) }) t.Run("invalid VPs list as JSON", func(t *testing.T) {