Skip to content

Commit

Permalink
perf: decrease redis roundtrips (#1314)
Browse files Browse the repository at this point in the history
Signed-off-by: Stas D <[email protected]>
  • Loading branch information
skynet2 authored May 9, 2023
1 parent c95e437 commit 2139f08
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 83 deletions.
14 changes: 4 additions & 10 deletions pkg/service/oidc4ci/oidc4ci_service_initiate_issuance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
76 changes: 3 additions & 73 deletions pkg/service/oidc4ci/oidc4ci_service_initiate_issuance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -153,7 +153,7 @@ func TestService_InitiateIssuance(t *testing.T) {
},
PreAuthCode: expectedCode,
IsPreAuthFlow: true,
UserPin: "567",
UserPin: "123456789",
},
}, nil
})
Expand All @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 2139f08

Please sign in to comment.