Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

[OSE-300] Credential applications can be cancelled #234

Merged
merged 4 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions integration/presentation_exchange_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/tbd54566975/ssi-service/pkg/service/operation/submission"
"github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
)

var presentationExchangeContext = NewTestContext("PresentationExchange")
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestSubmissionFlow(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "false", done)

reviewOutput, err := ReviewSubmission(submission.ID(opID))
reviewOutput, err := ReviewSubmission(storage.StatusObjectID(opID))
assert.NoError(t, err)
status, err := getJSONElement(reviewOutput, "$.status")
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/server_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tbd54566975/ssi-service/pkg/server/router"
"github.com/tbd54566975/ssi-service/pkg/service/operation"
"github.com/tbd54566975/ssi-service/pkg/service/operation/submission"
opstorage "github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
"github.com/tbd54566975/ssi-service/pkg/storage"
)

Expand All @@ -28,7 +28,7 @@ func TestOperationsAPI(t *testing.T) {
holderSigner, holderDID := getSigner(t)
definition := createPresentationDefinition(t, pRouter)
submissionOp := createSubmission(t, pRouter, definition.PresentationDefinition.ID, VerifiableCredential(), holderDID, holderSigner)
sub := reviewSubmission(t, pRouter, submission.ID(submissionOp.ID))
sub := reviewSubmission(t, pRouter, opstorage.StatusObjectID(submissionOp.ID))

createdID := submissionOp.ID
req := httptest.NewRequest(
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestOperationsAPI(t *testing.T) {
holderSigner, holderDID := getSigner(t)
definition := createPresentationDefinition(t, pRouter)
submissionOp := createSubmission(t, pRouter, definition.PresentationDefinition.ID, VerifiableCredential(), holderDID, holderSigner)
_ = reviewSubmission(t, pRouter, submission.ID(submissionOp.ID))
_ = reviewSubmission(t, pRouter, opstorage.StatusObjectID(submissionOp.ID))

createdID := submissionOp.ID
req := httptest.NewRequest(
Expand Down
18 changes: 9 additions & 9 deletions pkg/server/server_presentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/tbd54566975/ssi-service/config"
"github.com/tbd54566975/ssi-service/internal/keyaccess"
"github.com/tbd54566975/ssi-service/pkg/server/router"
opsubmission "github.com/tbd54566975/ssi-service/pkg/service/operation/submission"
opstorage "github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
"github.com/tbd54566975/ssi-service/pkg/service/presentation"
"github.com/tbd54566975/ssi-service/pkg/service/presentation/model"
"github.com/tbd54566975/ssi-service/pkg/storage"
Expand Down Expand Up @@ -163,14 +163,14 @@ func TestPresentationAPI(t *testing.T) {
"id": "did:web:andresuribe.com",
})), holderDID, holderSigner)

req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("https://ssi-service.com/v1/presentations/submissions/%s", opsubmission.ID(op.ID)), nil)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("https://ssi-service.com/v1/presentations/submissions/%s", opstorage.StatusObjectID(op.ID)), nil)
w := httptest.NewRecorder()

assert.NoError(ttt, pRouter.GetSubmission(newRequestContextWithParams(map[string]string{"id": opsubmission.ID(op.ID)}), w, req))
assert.NoError(ttt, pRouter.GetSubmission(newRequestContextWithParams(map[string]string{"id": opstorage.StatusObjectID(op.ID)}), w, req))

var resp router.GetSubmissionResponse
assert.NoError(ttt, json.NewDecoder(w.Body).Decode(&resp))
assert.Equal(ttt, opsubmission.ID(op.ID), resp.Submission.ID)
assert.Equal(ttt, opstorage.StatusObjectID(op.ID), resp.Submission.ID)
assert.Equal(ttt, definition.PresentationDefinition.ID, resp.Submission.DefinitionID)
assert.Equal(ttt, "pending", resp.Submission.Status)
})
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestPresentationAPI(t *testing.T) {
}

value := newRequestValue(ttt, request)
createdID := opsubmission.ID(submissionOp.ID)
createdID := opstorage.StatusObjectID(submissionOp.ID)
req := httptest.NewRequest(
http.MethodPut,
fmt.Sprintf("https://ssi-service.com/v1/presentations/submissions/%s/review", createdID),
Expand All @@ -243,7 +243,7 @@ func TestPresentationAPI(t *testing.T) {
holderSigner, holderDID := getSigner(ttt)
definition := createPresentationDefinition(ttt, pRouter)
submissionOp := createSubmission(t, pRouter, definition.PresentationDefinition.ID, VerifiableCredential(), holderDID, holderSigner)
createdID := opsubmission.ID(submissionOp.ID)
createdID := opstorage.StatusObjectID(submissionOp.ID)
_ = reviewSubmission(ttt, pRouter, createdID)

request := router.ReviewSubmissionRequest{
Expand Down Expand Up @@ -323,14 +323,14 @@ func TestPresentationAPI(t *testing.T) {
{
Status: "pending",
PresentationSubmission: &exchange.PresentationSubmission{
ID: opsubmission.ID(op.ID),
ID: opstorage.StatusObjectID(op.ID),
DefinitionID: definition.PresentationDefinition.ID,
},
},
{
Status: "pending",
PresentationSubmission: &exchange.PresentationSubmission{
ID: opsubmission.ID(op2.ID),
ID: opstorage.StatusObjectID(op2.ID),
DefinitionID: definition.PresentationDefinition.ID,
},
},
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestPresentationAPI(t *testing.T) {
{
Status: "pending",
PresentationSubmission: &exchange.PresentationSubmission{
ID: opsubmission.ID(op.ID),
ID: opstorage.StatusObjectID(op.ID),
DefinitionID: definition.PresentationDefinition.ID,
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/service/manifest/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (s Service) ProcessApplicationSubmission(request model.SubmitApplicationReq
applicantDID := request.ApplicantDID
storageRequest := manifeststg.StoredApplication{
ID: applicationID,
Status: opcredential.StatusFulfilled,
ManifestID: manifestID,
ApplicantDID: applicantDID,
Application: request.Application,
Expand Down
17 changes: 10 additions & 7 deletions pkg/service/manifest/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/tbd54566975/ssi-service/pkg/service/operation/credential"

"github.com/TBD54566975/ssi-sdk/credential/manifest"

Expand All @@ -16,9 +17,9 @@ import (
)

const (
manifestNamespace = "manifest"
applicationNamespace = "application"
responseNamespace = "response"
manifestNamespace = "manifest"

responseNamespace = "response"
)

type StoredManifest struct {
Expand All @@ -30,6 +31,8 @@ type StoredManifest struct {

type StoredApplication struct {
ID string `json:"id"`
Status credential.Status `json:"status"`
Reason string `json:"reason"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be an enum or user defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm imagining it's user defined free form text. We can change later once we see real use cases.

ManifestID string `json:"manifestId"`
ApplicantDID string `json:"applicantDid"`
Application manifest.CredentialApplication `json:"application"`
Expand Down Expand Up @@ -132,11 +135,11 @@ func (ms *Storage) StoreApplication(application StoredApplication) error {
if err != nil {
return util.LoggingErrorMsgf(err, "could not store application: %s", id)
}
return ms.db.Write(applicationNamespace, id, applicationBytes)
return ms.db.Write(credential.ApplicationNamespace, id, applicationBytes)
}

func (ms *Storage) GetApplication(id string) (*StoredApplication, error) {
applicationBytes, err := ms.db.Read(applicationNamespace, id)
applicationBytes, err := ms.db.Read(credential.ApplicationNamespace, id)
if err != nil {
return nil, util.LoggingErrorMsgf(err, "could not get application: %s", id)
}
Expand All @@ -152,7 +155,7 @@ func (ms *Storage) GetApplication(id string) (*StoredApplication, error) {

// GetApplications attempts to get all stored applications. It will return those it can even if it has trouble with some.
func (ms *Storage) GetApplications() ([]StoredApplication, error) {
gotApplications, err := ms.db.ReadAll(applicationNamespace)
gotApplications, err := ms.db.ReadAll(credential.ApplicationNamespace)
if err != nil {
return nil, util.LoggingErrorMsg(err, "could not get all applications")
}
Expand All @@ -173,7 +176,7 @@ func (ms *Storage) GetApplications() ([]StoredApplication, error) {
}

func (ms *Storage) DeleteApplication(id string) error {
if err := ms.db.Delete(applicationNamespace, id); err != nil {
if err := ms.db.Delete(credential.ApplicationNamespace, id); err != nil {
return util.LoggingErrorMsgf(err, "could not delete application: %s", id)
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/service/operation/credential/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func (s Status) String() string {
return "fulfilled"
case StatusRejected:
return "rejected"
case StatusCancelled:
return "cancelled"
default:
return "unknown"
}
Expand All @@ -32,4 +34,7 @@ const (
StatusPending
StatusFulfilled
StatusRejected
StatusCancelled
)

const ApplicationNamespace = "application"
42 changes: 30 additions & 12 deletions pkg/service/operation/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/tbd54566975/ssi-service/internal/util"
"github.com/tbd54566975/ssi-service/pkg/service/operation/credential"
opstorage "github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
"github.com/tbd54566975/ssi-service/pkg/service/operation/storage/namespace"
"github.com/tbd54566975/ssi-service/pkg/service/operation/submission"
Expand All @@ -23,9 +24,12 @@ type Storage struct {
}

func (b Storage) CancelOperation(id string) (*opstorage.StoredOperation, error) {
if strings.HasPrefix(id, submission.ParentResource) {
_, opData, err := b.db.UpdateValueAndOperation(
submission.Namespace, submission.ID(id), storage.NewUpdater(map[string]any{
var opData []byte
var err error
switch {
case strings.HasPrefix(id, submission.ParentResource):
_, opData, err = b.db.UpdateValueAndOperation(
submission.Namespace, opstorage.StatusObjectID(id), storage.NewUpdater(map[string]any{
"status": submission.StatusCancelled,
"reason": cancelledReason,
}),
Expand All @@ -34,16 +38,30 @@ func (b Storage) CancelOperation(id string) (*opstorage.StoredOperation, error)
"done": true,
}),
})
if err != nil {
return nil, errors.Wrap(err, "updating value and op")
}
var op opstorage.StoredOperation
if err = json.Unmarshal(opData, &op); err != nil {
return nil, errors.Wrap(err, "unmarshalling data")
}
return &op, nil
case strings.HasPrefix(id, credential.ParentResource):
_, opData, err = b.db.UpdateValueAndOperation(
credential.ApplicationNamespace, opstorage.StatusObjectID(id), storage.NewUpdater(map[string]any{
"status": credential.StatusCancelled,
"reason": cancelledReason,
}),
namespace.FromID(id), id, submission.OperationUpdater{
UpdaterWithMap: storage.NewUpdater(map[string]any{
"done": true,
}),
},
)
default:
return nil, errors.New("unrecognized id structure")
}

if err != nil {
return nil, errors.Wrap(err, "updating value and op")
}
var op opstorage.StoredOperation
if err = json.Unmarshal(opData, &op); err != nil {
return nil, errors.Wrap(err, "unmarshalling data")
}
return nil, errors.New("unrecognized id structure")
return &op, nil
}

func (b Storage) StoreOperation(op opstorage.StoredOperation) error {
Expand Down
12 changes: 12 additions & 0 deletions pkg/service/operation/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package storage

import (
"strings"

"go.einride.tech/aip/filtering"
)

Expand Down Expand Up @@ -35,3 +37,13 @@ type Storage interface {
DeleteOperation(id string) error
CancelOperation(id string) (*StoredOperation, error)
}

// StatusObjectID attempts to parse the submission id from the ID of the operation. This is done by taking the last word
// that results from splitting the id by "/". On failures, the empty string is returned.
func StatusObjectID(opID string) string {
i := strings.LastIndex(opID, "/")
if i == -1 {
return ""
}
return opID[(i + 1):]
}
Loading