Skip to content

Commit

Permalink
fix: credential issuance history
Browse files Browse the repository at this point in the history
Signed-off-by: Mykhailo Sizov <[email protected]>
  • Loading branch information
mishasizov-SK committed Oct 6, 2023
1 parent 9c17a39 commit 5974bf7
Show file tree
Hide file tree
Showing 17 changed files with 550 additions and 334 deletions.
12 changes: 8 additions & 4 deletions cmd/vc-rest/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import (
oidc4vpnoncestoremongo "github.com/trustbloc/vcs/pkg/storage/mongodb/oidc4vpnoncestore"
oidc4vptxstoremongo "github.com/trustbloc/vcs/pkg/storage/mongodb/oidc4vptxstore"
"github.com/trustbloc/vcs/pkg/storage/mongodb/requestobjectstore"
"github.com/trustbloc/vcs/pkg/storage/mongodb/vcissuancehistorystore"
"github.com/trustbloc/vcs/pkg/storage/mongodb/vcstatusstore"
"github.com/trustbloc/vcs/pkg/storage/redis"
redisclient "github.com/trustbloc/vcs/pkg/storage/redis"
Expand Down Expand Up @@ -599,12 +600,15 @@ func buildEchoHandler(
statusListVCSvc = credentialstatustracing.Wrap(statusListVCSvc, conf.Tracer)
}

vcIssuanceHistoryStore := vcissuancehistorystore.NewStore(mongodbClient)

var issueCredentialSvc issuecredential.ServiceInterface

issueCredentialSvc = issuecredential.New(&issuecredential.Config{
VCStatusManager: statusListVCSvc,
Crypto: vcCrypto,
KMSRegistry: kmsRegistry,
VCStatusManager: statusListVCSvc,
Crypto: vcCrypto,
KMSRegistry: kmsRegistry,
CredentialIssuanceHistoryStore: vcIssuanceHistoryStore,
})

if conf.IsTraceEnabled {
Expand Down Expand Up @@ -773,7 +777,7 @@ func buildEchoHandler(
IssueCredentialService: issueCredentialSvc,
VcStatusManager: statusListVCSvc,
OIDC4CIService: oidc4ciService,
CredentialIssuanceHistoryStore: vcStatusStore,
CredentialIssuanceHistoryStore: vcIssuanceHistoryStore,
ExternalHostURL: conf.StartupParameters.apiGatewayURL,
Tracer: conf.Tracer,
OpenidIssuerConfigProvider: openidCredentialIssuerConfigProviderSvc,
Expand Down
9 changes: 5 additions & 4 deletions component/credentialstatus/credentialstatus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type cslManager interface {
CreateCSLEntry(
ctx context.Context,
profile *profileapi.Issuer,
credentialMetadata *credentialstatus.CredentialMetadata,
credentialID string,
) (*credentialstatus.StatusListEntry, error)
}

Expand Down Expand Up @@ -173,18 +173,19 @@ func (s *Service) CreateStatusListEntry(
ctx context.Context,
profileID profileapi.ID,
profileVersion profileapi.Version,
credentialMetadata *credentialstatus.CredentialMetadata) (*credentialstatus.StatusListEntry, error) {
credentialID string,
) (*credentialstatus.StatusListEntry, error) {
logger.Debugc(ctx, "CreateStatusListEntry begin",
logfields.WithProfileID(profileID),
logfields.WithProfileVersion(profileVersion),
logfields.WithCredentialID(credentialMetadata.CredentialID))
logfields.WithCredentialID(credentialID))

profile, err := s.profileService.GetProfile(profileID, profileVersion)
if err != nil {
return nil, fmt.Errorf("get profile: %w", err)
}

statusListEntry, err := s.cslMgr.CreateCSLEntry(ctx, profile, credentialMetadata)
statusListEntry, err := s.cslMgr.CreateCSLEntry(ctx, profile, credentialID)
if err != nil {
return nil, fmt.Errorf("create CSL entry: %w", err)
}
Expand Down
57 changes: 17 additions & 40 deletions component/credentialstatus/credentialstatus_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ func TestCredentialStatusList_CreateStatusListEntry(t *testing.T) {

vcStatusStore := newMockVCStatusStore()

metadata := &credentialstatus.CredentialMetadata{CredentialID: credID}

cslMgr, err := cslmanager.New(
&cslmanager.Config{
CSLVCStore: cslVCStore,
Expand All @@ -141,11 +139,11 @@ func TestCredentialStatusList_CreateStatusListEntry(t *testing.T) {
})
require.NoError(t, err)

statusID, err := s.CreateStatusListEntry(ctx, profileID, profileVersion, metadata)
statusID, err := s.CreateStatusListEntry(ctx, profileID, profileVersion, credID)
require.NoError(t, err)
validateVCStatus(t, s, statusID, listID)

statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, metadata)
statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, credID)
require.NoError(t, err)
validateVCStatus(t, s, statusID, listID)

Expand All @@ -154,11 +152,11 @@ func TestCredentialStatusList_CreateStatusListEntry(t *testing.T) {
require.NoError(t, err)
require.NotEqual(t, updatedListID, listID)

statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, metadata)
statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, credID)
require.NoError(t, err)
validateVCStatus(t, s, statusID, updatedListID)

statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, metadata)
statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, credID)
require.NoError(t, err)
validateVCStatus(t, s, statusID, updatedListID)

Expand All @@ -168,7 +166,7 @@ func TestCredentialStatusList_CreateStatusListEntry(t *testing.T) {
require.NotEqual(t, updatedListID, updatedListIDSecond)
require.NotEqual(t, listID, updatedListIDSecond)

statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, metadata)
statusID, err = s.CreateStatusListEntry(ctx, profileID, profileVersion, credID)
require.NoError(t, err)
validateVCStatus(t, s, statusID, updatedListIDSecond)
})
Expand All @@ -182,9 +180,7 @@ func TestCredentialStatusList_CreateStatusListEntry(t *testing.T) {
})
require.NoError(t, err)

meta := &credentialstatus.CredentialMetadata{CredentialID: credID}

status, err := s.CreateStatusListEntry(context.Background(), profileID, profileVersion, meta)
status, err := s.CreateStatusListEntry(context.Background(), profileID, profileVersion, credID)
require.Error(t, err)
require.Nil(t, status)
require.Contains(t, err.Error(), "get profile")
Expand Down Expand Up @@ -285,12 +281,10 @@ func TestCredentialStatusList_UpdateVCStatus(t *testing.T) {
})
require.NoError(t, err)

meta := &credentialstatus.CredentialMetadata{CredentialID: credID}

statusListEntry, err := s.CreateStatusListEntry(ctx, profileID, profileVersion, meta)
statusListEntry, err := s.CreateStatusListEntry(ctx, profileID, profileVersion, credID)
require.NoError(t, err)

err = vcStatusStore.Put(ctx, profileID, profileVersion, meta, statusListEntry.TypedID)
err = vcStatusStore.Put(ctx, profileID, profileVersion, credID, statusListEntry.TypedID)
require.NoError(t, err)

params := credentialstatus.UpdateVCStatusParams{
Expand Down Expand Up @@ -406,10 +400,8 @@ func TestCredentialStatusList_UpdateVCStatus(t *testing.T) {
})
require.NoError(t, err)

meta := &credentialstatus.CredentialMetadata{CredentialID: credID}

err = vcStore.Put(
context.Background(), profileID, profileVersion, meta, &verifiable.TypedID{
context.Background(), profileID, profileVersion, credID, &verifiable.TypedID{
Type: string(vc.StatusList2021VCStatus)})
require.NoError(t, err)

Expand Down Expand Up @@ -444,10 +436,8 @@ func TestCredentialStatusList_UpdateVCStatus(t *testing.T) {
})
require.NoError(t, err)

meta := &credentialstatus.CredentialMetadata{CredentialID: credID}

err = vcStore.Put(
context.Background(), profileID, profileVersion, meta,
context.Background(), profileID, profileVersion, credID,
&verifiable.TypedID{Type: string(vc.StatusList2021VCStatus)})
require.NoError(t, err)

Expand Down Expand Up @@ -662,9 +652,7 @@ func TestCredentialStatusList_UpdateVCStatus(t *testing.T) {
})
require.NoError(t, err)

meta := &credentialstatus.CredentialMetadata{CredentialID: credID}

statusListEntry, err := s.CreateStatusListEntry(context.Background(), profileID, profileVersion, meta)
statusListEntry, err := s.CreateStatusListEntry(context.Background(), profileID, profileVersion, credID)
require.NoError(t, err)

err = s.updateVCStatus(
Expand Down Expand Up @@ -726,9 +714,7 @@ func TestCredentialStatusList_UpdateVCStatus(t *testing.T) {
})
require.NoError(t, err)

meta := &credentialstatus.CredentialMetadata{CredentialID: credID}

statusListEntry, err := s.CreateStatusListEntry(context.Background(), profile.ID, profile.Version, meta)
statusListEntry, err := s.CreateStatusListEntry(context.Background(), profile.ID, profile.Version, credID)
require.NoError(t, err)

require.NoError(t, s.updateVCStatus(
Expand Down Expand Up @@ -1028,19 +1014,14 @@ func (m *mockCSLVCStore) Get(_ context.Context, cslURL string) (*credentialstatu
return w, nil
}

type storeData struct {
typedID *verifiable.TypedID
metadata *credentialstatus.CredentialMetadata
}

type mockVCStore struct {
putErr error
s map[string]storeData
s map[string]*verifiable.TypedID
}

func newMockVCStatusStore() *mockVCStore {
return &mockVCStore{
s: map[string]storeData{},
s: map[string]*verifiable.TypedID{},
}
}

Expand All @@ -1050,23 +1031,19 @@ func (m *mockVCStore) Get(_ context.Context, profileID, profileVersion, vcID str
return nil, errors.New("data not found")
}

return v.typedID, nil
return v, nil
}

func (m *mockVCStore) Put(
_ context.Context,
profileID, profileVersion string,
metadata *credentialstatus.CredentialMetadata,
profileID, profileVersion, credentialID string,
typedID *verifiable.TypedID,
) error {
if m.putErr != nil {
return m.putErr
}

m.s[fmt.Sprintf("%s_%s_%s", profileID, profileVersion, metadata.CredentialID)] = storeData{
typedID: typedID,
metadata: metadata,
}
m.s[fmt.Sprintf("%s_%s_%s", profileID, profileVersion, credentialID)] = typedID

return nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cslmanager/cslmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ type cslVCStore interface {
type vcStatusStore interface {
Put(
ctx context.Context,
profileID string,
profileVersion string,
metadata *credentialstatus.CredentialMetadata,
profileID profileapi.ID,
profileVersion profileapi.Version,
credentialID string,
typedID *verifiable.TypedID,
) error
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func New(config *Config) (*Manager, error) {
func (s *Manager) CreateCSLEntry(
ctx context.Context,
profile *profileapi.Issuer,
credentialMetadata *credentialstatus.CredentialMetadata,
credentialID string,
) (*credentialstatus.StatusListEntry, error) {
logger.Debugc(ctx, "CSL Manager - CreateCSLEntry",
logfields.WithProfileID(profile.ID), logfields.WithProfileVersion(profile.Version))
Expand All @@ -116,7 +116,7 @@ func (s *Manager) CreateCSLEntry(
}

// Store VC status to DB
err = s.vcStatusStore.Put(ctx, profile.ID, profile.Version, credentialMetadata, statusListEntry.TypedID)
err = s.vcStatusStore.Put(ctx, profile.ID, profile.Version, credentialID, statusListEntry.TypedID)
if err != nil {
return nil, fmt.Errorf("failed to store credential status: %w", err)
}
Expand Down
Loading

0 comments on commit 5974bf7

Please sign in to comment.