Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discovery: DID registration by clients #2709

Merged
merged 19 commits into from
Jan 16, 2024
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
154 changes: 77 additions & 77 deletions README.rst

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion auth/api/iam/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func CreateSystem(shutdownCallback context.CancelFunc) *core.System {
vdrInstance := vdr.NewVDR(cryptoInstance, networkInstance, didStore, eventManager, storageInstance)
credentialInstance := vcr.NewVCRInstance(cryptoInstance, vdrInstance, networkInstance, jsonld, eventManager, storageInstance, pkiInstance)
didmanInstance := didman.NewDidmanInstance(vdrInstance, credentialInstance, jsonld)
discoveryInstance := discovery.New(storageInstance, credentialInstance)
discoveryInstance := discovery.New(storageInstance, credentialInstance, vdrInstance)
authInstance := auth.NewAuthInstance(auth.DefaultConfig(), vdrInstance, credentialInstance, cryptoInstance, didmanInstance, jsonld, pkiInstance)
statusEngine := status.NewStatusEngine(system)
metricsEngine := core.NewMetricsEngine()
Expand Down Expand Up @@ -224,7 +224,7 @@ func CreateSystem(shutdownCallback context.CancelFunc) *core.System {
system.RegisterRoutes(authIAMAPI.New(authInstance, credentialInstance, vdrInstance, storageInstance, policyInstance))
system.RegisterRoutes(&authMeansAPI.Wrapper{Auth: authInstance})
system.RegisterRoutes(&didmanAPI.Wrapper{Didman: didmanInstance})
system.RegisterRoutes(&discoveryAPI.Wrapper{Server: discoveryInstance})
system.RegisterRoutes(&discoveryAPI.Wrapper{Server: discoveryInstance, Client: discoveryInstance})

// Register engines
// without dependencies
Expand Down
44 changes: 43 additions & 1 deletion discovery/api/v1/wrapper.go → discovery/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"context"
"errors"
"github.com/labstack/echo/v4"
"github.com/nuts-foundation/go-did/did"
"github.com/nuts-foundation/nuts-node/audit"
"github.com/nuts-foundation/nuts-node/core"
"github.com/nuts-foundation/nuts-node/discovery"
"net/http"
Expand Down Expand Up @@ -56,6 +58,9 @@ func (w *Wrapper) Routes(router core.EchoRouter) {
return f(ctx, request)
}
},
func(f StrictHandlerFunc, operationID string) StrictHandlerFunc {
return audit.StrictMiddleware(f, discovery.ModuleName, operationID)
},
}))
}

Expand All @@ -77,7 +82,7 @@ func (w *Wrapper) GetPresentations(_ context.Context, request GetPresentationsRe
}

func (w *Wrapper) RegisterPresentation(_ context.Context, request RegisterPresentationRequestObject) (RegisterPresentationResponseObject, error) {
err := w.Server.Add(request.ServiceID, *request.Body)
err := w.Server.Register(request.ServiceID, *request.Body)
if err != nil {
return nil, err
}
Expand All @@ -99,3 +104,40 @@ func (w *Wrapper) SearchPresentations(_ context.Context, request SearchPresentat
}
return SearchPresentations200JSONResponse(result), nil
}

func (w *Wrapper) ActivateServiceForDID(ctx context.Context, request ActivateServiceForDIDRequestObject) (ActivateServiceForDIDResponseObject, error) {
subjectDID, err := did.ParseDID(request.Did)
if err != nil {
return nil, err
}
err = w.Client.ActivateServiceForDID(ctx, request.ServiceID, *subjectDID)
if errors.Is(err, discovery.ErrPresentationRegistrationFailed) {
// registration failed, but will be retried
return ActivateServiceForDID202JSONResponse{
Reason: err.Error(),
}, nil
}
if err != nil {
// other error
return nil, err
}
return ActivateServiceForDID200Response{}, nil
}

func (w *Wrapper) DeactivateServiceForDID(ctx context.Context, request DeactivateServiceForDIDRequestObject) (DeactivateServiceForDIDResponseObject, error) {
subjectDID, err := did.ParseDID(request.Did)
if err != nil {
return nil, err
}
err = w.Client.DeactivateServiceForDID(ctx, request.ServiceID, *subjectDID)
if errors.Is(err, discovery.ErrPresentationRegistrationFailed) {
// deactivation succeeded, but Verifiable Presentation couldn't be removed from remote Discovery Server.
return DeactivateServiceForDID202JSONResponse{
Reason: err.Error(),
}, nil
}
if err != nil {
return nil, err
}
return DeactivateServiceForDID200Response{}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package v1
import (
"errors"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/did"
"github.com/nuts-foundation/go-did/vc"
"github.com/nuts-foundation/nuts-node/discovery"
"github.com/nuts-foundation/nuts-node/vcr/credential"
Expand Down Expand Up @@ -80,7 +81,7 @@ func TestWrapper_RegisterPresentation(t *testing.T) {
t.Run("ok", func(t *testing.T) {
test := newMockContext(t)
presentation := vc.VerifiablePresentation{}
test.server.EXPECT().Add(serviceID, presentation).Return(nil)
test.server.EXPECT().Register(serviceID, presentation).Return(nil)

response, err := test.wrapper.RegisterPresentation(nil, RegisterPresentationRequestObject{
ServiceID: serviceID,
Expand All @@ -93,7 +94,7 @@ func TestWrapper_RegisterPresentation(t *testing.T) {
t.Run("error", func(t *testing.T) {
test := newMockContext(t)
presentation := vc.VerifiablePresentation{}
test.server.EXPECT().Add(serviceID, presentation).Return(discovery.ErrInvalidPresentation)
test.server.EXPECT().Register(serviceID, presentation).Return(discovery.ErrInvalidPresentation)

_, err := test.wrapper.RegisterPresentation(nil, RegisterPresentationRequestObject{
ServiceID: serviceID,
Expand All @@ -104,6 +105,75 @@ func TestWrapper_RegisterPresentation(t *testing.T) {
})
}

func TestWrapper_ActivateServiceForDID(t *testing.T) {
t.Run("ok", func(t *testing.T) {
test := newMockContext(t)
expectedDID := "did:web:example.com"
test.client.EXPECT().ActivateServiceForDID(gomock.Any(), serviceID, did.MustParseDID(expectedDID)).Return(nil)

response, err := test.wrapper.ActivateServiceForDID(nil, ActivateServiceForDIDRequestObject{
ServiceID: serviceID,
Did: expectedDID,
})

assert.NoError(t, err)
assert.IsType(t, ActivateServiceForDID200Response{}, response)
})
t.Run("ok, but registration failed", func(t *testing.T) {
test := newMockContext(t)
expectedDID := "did:web:example.com"
test.client.EXPECT().ActivateServiceForDID(gomock.Any(), gomock.Any(), gomock.Any()).Return(discovery.ErrPresentationRegistrationFailed)

response, err := test.wrapper.ActivateServiceForDID(nil, ActivateServiceForDIDRequestObject{
ServiceID: serviceID,
Did: expectedDID,
})

assert.NoError(t, err)
assert.IsType(t, ActivateServiceForDID202JSONResponse{}, response)
})
t.Run("other error", func(t *testing.T) {
test := newMockContext(t)
expectedDID := "did:web:example.com"
test.client.EXPECT().ActivateServiceForDID(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("foo"))

_, err := test.wrapper.ActivateServiceForDID(nil, ActivateServiceForDIDRequestObject{
ServiceID: serviceID,
Did: expectedDID,
})

assert.Error(t, err)
})
}

func TestWrapper_DeactivateServiceForDID(t *testing.T) {
t.Run("ok", func(t *testing.T) {
test := newMockContext(t)
expectedDID := "did:web:example.com"
test.client.EXPECT().DeactivateServiceForDID(gomock.Any(), serviceID, did.MustParseDID(expectedDID)).Return(nil)

response, err := test.wrapper.DeactivateServiceForDID(nil, DeactivateServiceForDIDRequestObject{
ServiceID: serviceID,
Did: expectedDID,
})

assert.NoError(t, err)
assert.IsType(t, DeactivateServiceForDID200Response{}, response)
})
t.Run("error", func(t *testing.T) {
test := newMockContext(t)
expectedDID := "did:web:example.com"
test.client.EXPECT().DeactivateServiceForDID(gomock.Any(), serviceID, did.MustParseDID(expectedDID)).Return(errors.New("foo"))

_, err := test.wrapper.DeactivateServiceForDID(nil, DeactivateServiceForDIDRequestObject{
ServiceID: serviceID,
Did: expectedDID,
})

assert.Error(t, err)
})
}

func TestWrapper_ResolveStatusCode(t *testing.T) {
expected := map[error]int{
discovery.ErrServerModeDisabled: http.StatusBadRequest,
Expand Down
Loading
Loading