From 2139f08fc7a87abf3a94fdeb426539ae654ee704 Mon Sep 17 00:00:00 2001 From: Stas Dmytryshyn Date: Tue, 9 May 2023 17:10:00 +0200 Subject: [PATCH] perf: decrease redis roundtrips (#1314) Signed-off-by: Stas D --- .../oidc4ci_service_initiate_issuance.go | 14 +--- .../oidc4ci_service_initiate_issuance_test.go | 76 +------------------ 2 files changed, 7 insertions(+), 83 deletions(-) diff --git a/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance.go b/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance.go index 0668e2820..c3b42ebb1 100644 --- a/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance.go +++ b/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance.go @@ -116,21 +116,15 @@ func (s *Service) InitiateIssuance( // nolint:funlen,gocyclo,gocognit data.OpState = data.PreAuthCode // set opState as it will be empty for pre-auth } + if req.UserPinRequired { + data.UserPin = s.pinGenerator.Generate(uuid.NewString()) + } + tx, err := s.store.Create(ctx, data) if err != nil { return nil, fmt.Errorf("store tx: %w", err) } - if req.UserPinRequired { - data.UserPin = s.pinGenerator.Generate(string(tx.ID)) - tx.UserPin = data.UserPin - - err = s.store.Update(ctx, tx) - if err != nil { - return nil, fmt.Errorf("store pin tx: %w", err) - } - } - if errSendEvent := s.sendEvent(ctx, tx, spi.IssuerOIDCInteractionInitiated); errSendEvent != nil { return nil, errSendEvent } diff --git a/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance_test.go b/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance_test.go index 05fb2c376..0f255111f 100644 --- a/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance_test.go +++ b/pkg/service/oidc4ci/oidc4ci_service_initiate_issuance_test.go @@ -137,7 +137,7 @@ func TestService_InitiateIssuance(t *testing.T) { ) (*oidc4ci.Transaction, error) { assert.NotEqual(t, data.OpState, initialOpState) assert.Equal(t, data.OpState, data.PreAuthCode) - assert.True(t, len(data.UserPin) == 0) + assert.True(t, len(data.UserPin) > 0) assert.Equal(t, true, data.IsPreAuthFlow) assert.NotEmpty(t, data.ClaimDataID) assert.Equal(t, oidc4ci.TransactionStateIssuanceInitiated, data.State) @@ -153,7 +153,7 @@ func TestService_InitiateIssuance(t *testing.T) { }, PreAuthCode: expectedCode, IsPreAuthFlow: true, - UserPin: "567", + UserPin: "123456789", }, }, nil }) @@ -179,12 +179,7 @@ func TestService_InitiateIssuance(t *testing.T) { mockWellKnownService.EXPECT().GetOIDCConfiguration(gomock.Any(), walletWellKnownURL).Return( &oidc4ci.OIDCConfiguration{}, nil) - pinGenerator.EXPECT().Generate("txID").Return("123456789") - mockTransactionStore.EXPECT().Update(gomock.Any(), gomock.Any()).DoAndReturn( - func(ctx context.Context, tx *oidc4ci.Transaction) error { - assert.Equal(t, "123456789", tx.UserPin) - return nil - }) + pinGenerator.EXPECT().Generate(gomock.Any()).Return("123456789") issuanceReq = &oidc4ci.InitiateIssuanceRequest{ CredentialTemplateID: "templateID", @@ -420,71 +415,6 @@ func TestService_InitiateIssuance(t *testing.T) { resp.InitiateIssuanceURL) }, }, - { - name: "Fail Pre-Auth with PIN because of saving pin tx", - setup: func() { - initialOpState := "eyJhbGciOiJSU0Et" - expectedCode := "super-secret-pre-auth-code" - claimData := map[string]interface{}{ - "my_awesome_claim": "claim", - } - - profile = &testProfile - mockTransactionStore.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()). - DoAndReturn(func( - ctx context.Context, - data *oidc4ci.TransactionData, - params ...func(insertOptions *oidc4ci.InsertOptions), - ) (*oidc4ci.Transaction, error) { - assert.NotEqual(t, data.OpState, initialOpState) - assert.Equal(t, data.OpState, data.PreAuthCode) - assert.True(t, len(data.UserPin) == 0) - assert.Equal(t, true, data.IsPreAuthFlow) - assert.NotEmpty(t, data.ClaimDataID) - assert.Equal(t, oidc4ci.TransactionStateIssuanceInitiated, data.State) - - return &oidc4ci.Transaction{ - ID: "txID", - TransactionData: oidc4ci.TransactionData{ - ProfileID: profile.ID, - CredentialTemplate: &profileapi.CredentialTemplate{ - ID: "templateID", - }, - PreAuthCode: expectedCode, - IsPreAuthFlow: true, - UserPin: "567", - }, - }, nil - }) - - chunks := &dataprotect.EncryptedData{ - Encrypted: []byte{0x1, 0x2, 0x3}, - EncryptedNonce: []byte{0x0, 0x2}, - } - - crypto.EXPECT().Encrypt(gomock.Any(), gomock.Any()). - Return(chunks, nil) - mockClaimDataStore.EXPECT().Create(gomock.Any(), gomock.Any()).Return("claimDataID", nil) - - mockWellKnownService.EXPECT().GetOIDCConfiguration(gomock.Any(), issuerWellKnownURL).Return( - &oidc4ci.OIDCConfiguration{}, nil) - - pinGenerator.EXPECT().Generate("txID").Return("123456789") - mockTransactionStore.EXPECT().Update(gomock.Any(), gomock.Any()).Return(errors.New("pin saving error")) - - issuanceReq = &oidc4ci.InitiateIssuanceRequest{ - CredentialTemplateID: "templateID", - ClientWellKnownURL: walletWellKnownURL, - ClaimEndpoint: "https://vcs.pb.example.com/claim", - OpState: initialOpState, - UserPinRequired: true, - ClaimData: claimData, - } - }, - check: func(t *testing.T, resp *oidc4ci.InitiateIssuanceResponse, err error) { - require.ErrorContains(t, err, "store pin tx: pin saving error") - }, - }, { name: "Fail Pre-Auth with PIN because of error during saving claim data", setup: func() {