Skip to content

Commit

Permalink
chore: Additional events on failures (trustbloc#1499)
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Stasyszyn <[email protected]>
  • Loading branch information
bstasyszyn authored Oct 30, 2023
1 parent ded42fd commit 31c00a5
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 124 deletions.
2 changes: 2 additions & 0 deletions pkg/restapi/resterr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
PresentationDefinitionMismatch ErrorCode = "presentation-definition-mismatch"
ClaimsNotReceived ErrorCode = "claims-not-received"
ClaimsNotFound ErrorCode = "claims-not-found"
ClaimsValidationErr ErrorCode = "invalid-claims"
DataNotFound ErrorCode = "data-not-found"
OpStateKeyDuplication ErrorCode = "op-state-key duplication"
CredentialTemplateNotConfigured ErrorCode = "credential-template-not-configured"
Expand All @@ -52,6 +53,7 @@ const (
CredentialFormatNotSupported ErrorCode = "credential-format-not-supported"
VCOptionsNotConfigured ErrorCode = "vc-options-not-configured"
InvalidIssuerURL ErrorCode = "invalid-issuer-url"
InvalidStateTransition ErrorCode = "invalid-state-transition"
)

type Component = string
Expand Down
2 changes: 1 addition & 1 deletion pkg/restapi/v1/issuer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func (c *Controller) PrepareCredential(e echo.Context) error {
}

if err = c.validateClaims(result.Credential, result.CredentialTemplate, result.EnforceStrictValidation); err != nil {
return fmt.Errorf("validate claims: %w", err)
return resterr.NewCustomError(resterr.ClaimsValidationErr, err)
}

signedCredential, err := c.signCredential(
Expand Down
2 changes: 1 addition & 1 deletion pkg/restapi/v1/issuer/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ func TestController_PrepareCredential(t *testing.T) {

req := `{"tx_id":"123","type":"UniversityDegreeCredential","format":"ldp_vc"}`
ctx := echoContext(withRequestBody([]byte(req)))
assert.EqualError(t, c.PrepareCredential(ctx), "validate claims: validation error")
assert.EqualError(t, c.PrepareCredential(ctx), "invalid-claims: validation error")
})
}

Expand Down
21 changes: 16 additions & 5 deletions pkg/service/oidc4ci/oidc4ci_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,11 @@ func (s *Service) PrepareClaimDataAuthorizationRequest(
}

if err = s.store.Update(ctx, tx); err != nil {
s.sendFailedTransactionEvent(ctx, tx, err)
return nil, err
e := resterr.NewSystemError(resterr.TransactionStoreComponent, "Update", err)

s.sendFailedTransactionEvent(ctx, tx, e)

return nil, e
}

if err = s.sendIssuanceAuthRequestPreparedTxEvent(ctx, tx); err != nil {
Expand Down Expand Up @@ -361,7 +364,7 @@ func (s *Service) updateAuthorizationDetails(ctx context.Context, ad *Authorizat
tx.AuthorizationDetails = ad

if err := s.store.Update(ctx, tx); err != nil {
return fmt.Errorf("update tx: %w", err)
return resterr.NewSystemError(resterr.TransactionStoreComponent, "Update", err)
}

return nil
Expand Down Expand Up @@ -454,13 +457,21 @@ func (s *Service) PrepareCredential(
expectedAudience := fmt.Sprintf("%s/oidc/idp/%s/%s", s.issuerVCSPublicHost, tx.ProfileID, tx.ProfileVersion)

if req.AudienceClaim == "" || req.AudienceClaim != expectedAudience {
return nil, resterr.NewValidationError(resterr.InvalidOrMissingProofOIDCErr, req.AudienceClaim,
e := resterr.NewValidationError(resterr.InvalidOrMissingProofOIDCErr, req.AudienceClaim,
errors.New("invalid aud"))

s.sendFailedTransactionEvent(ctx, tx, e)

return nil, e
}

claimData, err := s.getClaimsData(ctx, tx)
if err != nil {
return nil, fmt.Errorf("get claims data: %w", err)
e := fmt.Errorf("get claims data: %w", err)

s.sendFailedTransactionEvent(ctx, tx, e)

return nil, e
}

contexts := tx.CredentialTemplate.Contexts
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/oidc4ci/oidc4ci_service_exchange_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func TestExchangeCodeInvalidState(t *testing.T) {

resp, err := svc.ExchangeAuthorizationCode(context.TODO(), "sadsadas")
assert.Empty(t, resp)
assert.ErrorContains(t, err, "unexpected transaction from 5 to 4")
assert.ErrorContains(t, err, "unexpected transition from 5 to 4")
}

func TestExchangeCodePublishError(t *testing.T) {
Expand Down
9 changes: 7 additions & 2 deletions pkg/service/oidc4ci/oidc4ci_service_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ SPDX-License-Identifier: Apache-2.0

package oidc4ci

import "fmt"
import (
"fmt"

"github.com/trustbloc/vcs/pkg/restapi/resterr"
)

func (s *Service) validateStateTransition(
oldState TransactionState,
Expand Down Expand Up @@ -37,5 +41,6 @@ func (s *Service) validateStateTransition(
return nil
}

return fmt.Errorf("unexpected transaction from %v to %v", oldState, newState)
return resterr.NewCustomError(resterr.InvalidStateTransition,
fmt.Errorf("unexpected transition from %v to %v", oldState, newState))
}
2 changes: 1 addition & 1 deletion pkg/service/oidc4ci/oidc4ci_service_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ func TestInvalidTransition(t *testing.T) {
assert.NoError(t, err)

assert.ErrorContains(t, s.validateStateTransition(TransactionStateUnknown, TransactionStateIssuanceInitiated),
"unexpected transaction from 0 to 1")
"unexpected transition from 0 to 1")
}
Loading

0 comments on commit 31c00a5

Please sign in to comment.