Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: verifier.oidc-interaction-qr-scanned.v1 for s3 #1425

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions component/oidc/vp/requestobjectstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/url"
"strings"

"github.com/trustbloc/vcs/pkg/event/spi"
"github.com/trustbloc/vcs/pkg/service/requestobject"
)

Expand All @@ -24,39 +23,27 @@ type requestObjectStoreRepository interface {
GetResourceURL(key string) string
}

type eventService interface {
Publish(ctx context.Context, topic string, messages ...*spi.Event) error
}

type RequestObjectStore struct {
repo requestObjectStoreRepository
eventSvc eventService
eventTopic string

repo requestObjectStoreRepository
selfURI string
}

func NewRequestObjectStore(
repo requestObjectStoreRepository,
eventSvc eventService,
selfURI, eventTopic string,
selfURI string,
) *RequestObjectStore {
return &RequestObjectStore{
repo: repo,
eventSvc: eventSvc,
selfURI: selfURI,
eventTopic: eventTopic,
repo: repo,
selfURI: selfURI,
}
}

func (s *RequestObjectStore) Publish(
ctx context.Context,
requestObject string,
accessRequestObjectEvent *spi.Event,
) (string, error) {
resp, err := s.repo.Create(ctx, requestobject.RequestObject{
Content: requestObject,
AccessRequestObjectEvent: accessRequestObjectEvent,
Content: requestObject,
})
if err != nil {
return "", err
Expand Down Expand Up @@ -86,10 +73,5 @@ func (s *RequestObjectStore) Get(ctx context.Context, id string) (*requestobject
return nil, err
}

err = s.eventSvc.Publish(ctx, s.eventTopic, result.AccessRequestObjectEvent)
if err != nil {
return nil, err
}

return result, nil
}
55 changes: 11 additions & 44 deletions component/oidc/vp/requestobjectstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/trustbloc/vcs/pkg/event/spi"
"github.com/trustbloc/vcs/pkg/service/requestobject"
)

Expand All @@ -37,19 +36,16 @@ func TestRequestObjectStore(t *testing.T) {

repo := NewMockRequestObjectStoreRepository(gomock.NewController(t))
repo.EXPECT().Create(context.TODO(), requestobject.RequestObject{
Content: strData,
AccessRequestObjectEvent: &spi.Event{},
Content: strData,
}).Return(&requestobject.RequestObject{
ID: randomID,
Content: strData,
}, nil)
repo.EXPECT().GetResourceURL(randomID).Return("")

eventSvc := NewMockEventService(gomock.NewController(t))

store := NewRequestObjectStore(repo, eventSvc, uri, spi.VerifierEventTopic)
store := NewRequestObjectStore(repo, uri)

finalURI, err := store.Publish(context.TODO(), string(dataBytes), &spi.Event{})
finalURI, err := store.Publish(context.TODO(), string(dataBytes))

assert.NoError(t, err)

Expand All @@ -61,19 +57,16 @@ func TestRequestObjectStore(t *testing.T) {

repo := NewMockRequestObjectStoreRepository(gomock.NewController(t))
repo.EXPECT().Create(context.TODO(), requestobject.RequestObject{
Content: strData,
AccessRequestObjectEvent: &spi.Event{},
Content: strData,
}).Return(&requestobject.RequestObject{
ID: randomID,
Content: strData,
}, nil)
repo.EXPECT().GetResourceURL(randomID).Return("https://awesome-url/resources/2135321")

eventSvc := NewMockEventService(gomock.NewController(t))
store := NewRequestObjectStore(repo, uri)

store := NewRequestObjectStore(repo, eventSvc, uri, spi.VerifierEventTopic)

finalURI, err := store.Publish(context.TODO(), string(dataBytes), &spi.Event{})
finalURI, err := store.Publish(context.TODO(), string(dataBytes))

assert.NoError(t, err)

Expand All @@ -86,11 +79,9 @@ func TestRequestObjectStore(t *testing.T) {
repo := NewMockRequestObjectStoreRepository(gomock.NewController(t))
repo.EXPECT().Create(gomock.Any(), gomock.Any()).Return(nil, errors.New(errorStr))

eventSvc := NewMockEventService(gomock.NewController(t))

store := NewRequestObjectStore(repo, eventSvc, uri, spi.VerifierEventTopic)
store := NewRequestObjectStore(repo, uri)

finalURI, err := store.Publish(context.TODO(), string(dataBytes), &spi.Event{})
finalURI, err := store.Publish(context.TODO(), string(dataBytes))
assert.Empty(t, finalURI)
assert.ErrorContains(t, err, errorStr)
})
Expand All @@ -102,10 +93,7 @@ func TestRequestObjectStore(t *testing.T) {
ID: id,
}, nil)

eventSvc := NewMockEventService(gomock.NewController(t))
eventSvc.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(nil)

store := NewRequestObjectStore(repo, eventSvc, uri, spi.VerifierEventTopic)
store := NewRequestObjectStore(repo, uri)

resp, err := store.Get(context.TODO(), id)

Expand All @@ -118,26 +106,7 @@ func TestRequestObjectStore(t *testing.T) {
repo := NewMockRequestObjectStoreRepository(gomock.NewController(t))
repo.EXPECT().Find(gomock.Any(), gomock.Any()).Return(nil, errors.New("store failed"))

eventSvc := NewMockEventService(gomock.NewController(t))

store := NewRequestObjectStore(repo, eventSvc, uri, spi.VerifierEventTopic)

_, err := store.Get(context.TODO(), id)

assert.Error(t, err)
})

t.Run("Get publish event failed", func(t *testing.T) {
id := "21342315231w"
repo := NewMockRequestObjectStoreRepository(gomock.NewController(t))
repo.EXPECT().Find(gomock.Any(), gomock.Any()).Return(&requestobject.RequestObject{
ID: id,
}, nil)

eventSvc := NewMockEventService(gomock.NewController(t))
eventSvc.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(errors.New("publish failed"))

store := NewRequestObjectStore(repo, eventSvc, uri, spi.VerifierEventTopic)
store := NewRequestObjectStore(repo, uri)

_, err := store.Get(context.TODO(), id)

Expand Down Expand Up @@ -169,9 +138,7 @@ func TestDelete(t *testing.T) {
repo := NewMockRequestObjectStoreRepository(gomock.NewController(t))
repo.EXPECT().Delete(gomock.Any(), testCase.expectedID).Return(nil)

eventSvc := NewMockEventService(gomock.NewController(t))

store := NewRequestObjectStore(repo, eventSvc, "", spi.VerifierEventTopic)
store := NewRequestObjectStore(repo, "")

assert.NoError(t, store.Remove(context.TODO(), testCase.path))
})
Expand Down
13 changes: 6 additions & 7 deletions pkg/service/oidc4vp/oidc4vp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type transactionManager interface {
}

type requestObjectPublicStore interface {
Publish(ctx context.Context, requestObject string, accessRequestObjectEvent *spi.Event) (string, error)
Publish(ctx context.Context, requestObject string) (string, error)
}

type kmsRegistry interface {
Expand Down Expand Up @@ -254,12 +254,7 @@ func (s *Service) InitiateOidcInteraction(

logger.Debugc(ctx, "InitiateOidcInteraction request object created")

accessRequestObjectEvent, err := s.createEvent(tx, profile, spi.VerifierOIDCInteractionQRScanned, nil)
if err != nil {
return nil, err
}

requestURI, err := s.requestObjectPublicStore.Publish(ctx, token, accessRequestObjectEvent)
requestURI, err := s.requestObjectPublicStore.Publish(ctx, token)
if err != nil {
return nil, fmt.Errorf("fail publish request object: %w", err)
}
Expand Down Expand Up @@ -378,6 +373,10 @@ func (s *Service) VerifyOIDCVerifiablePresentation(ctx context.Context, txID TxI
return fmt.Errorf("inconsistent transaction state %w", err)
}

if errSendEvent := s.sendEvent(ctx, tx, profile, spi.VerifierOIDCInteractionQRScanned); errSendEvent != nil {
return errSendEvent
}

logger.Debugc(ctx, "VerifyOIDCVerifiablePresentation profile fetched", logfields.WithProfileID(profile.ID))

logger.Debugc(ctx, fmt.Sprintf("VerifyOIDCVerifiablePresentation count of tokens is %v", len(tokens)))
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/oidc4vp/oidc4vp_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func TestService_InitiateOidcInteraction(t *testing.T) {
PresentationDefinition: &presexch.PresentationDefinition{},
}, "nonce1", nil)
requestObjectPublicStore := NewMockRequestObjectPublicStore(gomock.NewController(t))
requestObjectPublicStore.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes().DoAndReturn(func(ctx context.Context, token string, event *spi.Event) (string, error) {
requestObjectPublicStore.EXPECT().Publish(gomock.Any(), gomock.Any()).
AnyTimes().DoAndReturn(func(ctx context.Context, token string) (string, error) {
return "someurl/abc", nil
})

Expand Down Expand Up @@ -173,7 +173,7 @@ func TestService_InitiateOidcInteraction(t *testing.T) {

t.Run("publish request object failed", func(t *testing.T) {
requestObjectPublicStoreErr := NewMockRequestObjectPublicStore(gomock.NewController(t))
requestObjectPublicStoreErr.EXPECT().Publish(gomock.Any(), gomock.Any(), gomock.Any()).
requestObjectPublicStoreErr.EXPECT().Publish(gomock.Any(), gomock.Any()).
AnyTimes().Return("", errors.New("fail"))

withError := oidc4vp.NewService(&oidc4vp.Config{
Expand Down
7 changes: 2 additions & 5 deletions pkg/service/requestobject/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ package requestobject

import (
"errors"

"github.com/trustbloc/vcs/pkg/event/spi"
)

type RequestObject struct {
ID string `json:"id"`
Content string `json:"content"`
AccessRequestObjectEvent *spi.Event `json:"accessRequestObjectEvent"`
ID string `json:"id"`
Content string `json:"content"`
}

var ErrDataNotFound = errors.New("data not found")
28 changes: 6 additions & 22 deletions pkg/storage/mongodb/requestobjectstore/request_object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"

"github.com/trustbloc/vcs/pkg/event/spi"
"github.com/trustbloc/vcs/pkg/service/requestobject"
"github.com/trustbloc/vcs/pkg/storage/mongodb"
)
Expand All @@ -30,9 +29,8 @@ type Store struct {
}

type mongoDocument struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Content string `bson:"content"`
AccessRequestObjectEvent map[string]interface{} `bson:"accessRequestObjectEvent"`
ID primitive.ObjectID `bson:"_id,omitempty"`
Content string `bson:"content"`
}

// NewStore creates Store.
Expand All @@ -47,15 +45,9 @@ func (p *Store) Create(
) (*requestobject.RequestObject, error) {
collection := p.mongoClient.Database().Collection(txCollection)

event, err := mongodb.StructureToMap(request.AccessRequestObjectEvent)
if err != nil {
return nil, fmt.Errorf("create doc: %w", err)
}

obj := &mongoDocument{
ID: primitive.ObjectID{},
Content: request.Content,
AccessRequestObjectEvent: event,
ID: primitive.ObjectID{},
Content: request.Content,
}

result, err := collection.InsertOne(ctx, obj)
Expand Down Expand Up @@ -94,17 +86,9 @@ func (p *Store) Find(
return nil, fmt.Errorf("tx find failed: %w", err)
}

event := &spi.Event{}

err = mongodb.MapToStructure(txDoc.AccessRequestObjectEvent, event)
if err != nil {
return nil, fmt.Errorf("access request object event deserialization failed: %w", err)
}

return &requestobject.RequestObject{
ID: txDoc.ID.Hex(),
Content: txDoc.Content,
AccessRequestObjectEvent: event,
ID: txDoc.ID.Hex(),
Content: txDoc.Content,
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package requestobjectstore

import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"
Expand All @@ -26,7 +25,6 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/trustbloc/vcs/pkg/event/spi"
"github.com/trustbloc/vcs/pkg/service/requestobject"
"github.com/trustbloc/vcs/pkg/storage/mongodb"
)
Expand Down Expand Up @@ -86,21 +84,8 @@ func TestObjectStore(t *testing.T) {
t.Run("Create and find by id", func(t *testing.T) {
u := uuid.New()

eventData := json.RawMessage(`{"test":"test"}`)

expectedEvent := spi.Event{
SpecVersion: "test1",
ID: "test2",
Source: "test2",
Type: "test4",
DataContentType: "test5",
Time: nil,
Data: eventData,
}

resp, err := store.Create(context.TODO(), requestobject.RequestObject{
Content: u,
AccessRequestObjectEvent: &expectedEvent,
Content: u,
})

assert.NoError(t, err)
Expand All @@ -109,11 +94,6 @@ func TestObjectStore(t *testing.T) {

assert.NoError(t, err2)
assert.Equal(t, u, resp2.Content)

if !reflect.DeepEqual(resp2.AccessRequestObjectEvent.Data, expectedEvent.Data) {
t.Errorf("ValidateCredential() got = %v, want %v",
resp2.AccessRequestObjectEvent, expectedEvent)
}
})

t.Run("Find non-existing document", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/trustbloc/vcs/pkg/event/spi"
"github.com/trustbloc/vcs/pkg/service/requestobject"
"github.com/trustbloc/vcs/pkg/storage/s3/requestobjectstore"
)

func TestCreate(t *testing.T) {
targetObj := &requestobject.RequestObject{
Content: "any string",
AccessRequestObjectEvent: &spi.Event{ID: "1234"},
Content: "any string",
}

t.Run("success", func(t *testing.T) {
Expand Down
Loading