Skip to content

Commit

Permalink
Update constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 28, 2022
1 parent 780f4d3 commit 499f895
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
25 changes: 13 additions & 12 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
// jsonpb Marshaller is deprecated, but is needed because there's only one way to proto
// marshal in combination with our proto generator version
"encoding/json"
"strings"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -102,15 +103,18 @@ func (rs RequestService) prepareDereferencingResult(did string, dereferencingOpt

// https://w3c-ccg.github.io/did-resolution/#resolving
func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionOption) (types.DidResolution, error) {
if !resolutionOptions.Accept.IsSupported() {
return types.DidResolution{ResolutionMetadata: types.NewResolutionMetadata(did, types.JSON, types.RepresentationNotSupportedError)}, nil
}
didResolutionMetadata := types.NewResolutionMetadata(did, resolutionOptions.Accept, "")

if didMethod, _, _, _ := cheqdUtils.TrySplitDID(did); didMethod != rs.didMethod {
didResolutionMetadata.ResolutionError = types.ResolutionMethodNotSupported
didResolutionMetadata.ResolutionError = types.MethodNotSupportedError
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
}

if !cheqdUtils.IsValidDID(did, "", rs.ledgerService.GetNamespaces()) {
didResolutionMetadata.ResolutionError = types.ResolutionInvalidDID
didResolutionMetadata.ResolutionError = types.InvalidDIDError
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil

}
Expand All @@ -126,17 +130,14 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO
}

if !isFound {
didResolutionMetadata.ResolutionError = types.ResolutionNotFound
didResolutionMetadata.ResolutionError = types.NotFoundError
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
}

if didResolutionMetadata.ContentType == types.DIDJSONLD || didResolutionMetadata.ContentType == types.JSONLD {
didDoc.Context = append(didDoc.Context, types.DIDSchemaJSONLD)
} else if didResolutionMetadata.ContentType == types.DIDJSON {
didDoc.Context = []string{}
} else {
didResolutionMetadata.ResolutionError = types.DereferencingNotSupported
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
didDoc.Context = []string{}
}
return types.DidResolution{Did: didDoc, Metadata: resolvedMetadata, ResolutionMetadata: didResolutionMetadata}, nil
}
Expand All @@ -147,13 +148,13 @@ func (rs RequestService) Dereference(didUrl string, dereferenceOptions types.Der
log.Info().Msgf("did: %s, path: %s, query: %s, fragmentId: %s", did, path, query, fragmentId)

if err != nil || !cheqdUtils.IsValidDIDUrl(didUrl, "", []string{}) {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingInvalidDIDUrl)
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.InvalidDIDUrlError)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}

// TODO: implement
if query != "" {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotSupported)
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.RepresentationNotSupportedError)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}

Expand All @@ -175,7 +176,7 @@ func (rs RequestService) dereferencePrimary(path string, did string, didUrl stri
resourceId := utils.GetResourceId(path)
// Only `resource` path is supported
if resourceId == "" {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotSupported)
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.RepresentationNotSupportedError)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}

Expand All @@ -184,7 +185,7 @@ func (rs RequestService) dereferencePrimary(path string, did string, didUrl stri
return types.DidDereferencing{}, err
}
if !isFound {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotFound)
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.NotFoundError)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}
jsonFragment, err := rs.didDocService.MarshallContentStream(&resource, dereferenceOptions.Accept)
Expand Down Expand Up @@ -217,7 +218,7 @@ func (rs RequestService) dereferenceSecondary(did string, fragmentId string, did
}

if protoContent == nil {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotFound)
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.NotFoundError)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}

Expand Down
58 changes: 37 additions & 21 deletions services/request_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,16 @@ func TestResolve(t *testing.T) {
validMetadata := validMetadata()
validResource := validResource()
subtests := []struct {
name string
ledgerService MockLedgerService
resolutionType types.ContentType
identifier string
method string
namespace string
expectedDID cheqd.Did
expectedMetadata types.ResolutionDidDocMetadata
expectedError types.ErrorType
name string
ledgerService MockLedgerService
resolutionType types.ContentType
identifier string
method string
namespace string
expectedDID cheqd.Did
expectedMetadata types.ResolutionDidDocMetadata
expectedResolutionType types.ContentType
expectedError types.ErrorType
}{
{
name: "successful resolution",
Expand All @@ -149,7 +150,7 @@ func TestResolve(t *testing.T) {
namespace: validNamespace,
expectedDID: cheqd.Did{},
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.ResolutionNotFound,
expectedError: types.NotFoundError,
},
{
name: "invalid DID",
Expand All @@ -160,7 +161,7 @@ func TestResolve(t *testing.T) {
namespace: validNamespace,
expectedDID: cheqd.Did{},
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.ResolutionInvalidDID,
expectedError: types.InvalidDIDError,
},
{
name: "invalid method",
Expand All @@ -171,7 +172,7 @@ func TestResolve(t *testing.T) {
namespace: validNamespace,
expectedDID: cheqd.Did{},
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.ResolutionMethodNotSupported,
expectedError: types.MethodNotSupportedError,
},
{
name: "invalid namespace",
Expand All @@ -182,7 +183,19 @@ func TestResolve(t *testing.T) {
namespace: "invalid_namespace",
expectedDID: cheqd.Did{},
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.ResolutionInvalidDID,
expectedError: types.InvalidDIDError,
},
{
name: "representation is not supported",
ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource),
resolutionType: "text/html,application/xhtml+xml",
identifier: validIdentifier,
method: validMethod,
namespace: validNamespace,
expectedDID: cheqd.Did{},
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedResolutionType: types.JSON,
expectedError: types.RepresentationNotSupportedError,
},
}

Expand All @@ -200,15 +213,18 @@ func TestResolve(t *testing.T) {
} else {
subtest.expectedDID.Context = nil
}

expectedContentType := subtest.expectedResolutionType
if expectedContentType == "" {
expectedContentType = subtest.resolutionType
}
resolutionResult, err := requestService.Resolve(id, types.ResolutionOption{Accept: subtest.resolutionType})

fmt.Println(subtest.name + ": resolutionResult:")
fmt.Println(resolutionResult.Did.VerificationMethod)
fmt.Println(subtest.expectedDID.VerificationMethod)
require.EqualValues(t, subtest.expectedDID, resolutionResult.Did)
require.EqualValues(t, subtest.expectedMetadata, resolutionResult.Metadata)
require.EqualValues(t, subtest.resolutionType, resolutionResult.ResolutionMetadata.ContentType)
require.EqualValues(t, expectedContentType, resolutionResult.ResolutionMetadata.ContentType)
require.EqualValues(t, subtest.expectedError, resolutionResult.ResolutionMetadata.ResolutionError)
require.EqualValues(t, expectedDIDProperties, resolutionResult.ResolutionMetadata.DidProperties)
require.Empty(t, err)
Expand Down Expand Up @@ -280,47 +296,47 @@ func TestDereferencing(t *testing.T) {
didUrl: "unvalid_url",
dereferencingType: types.DIDJSONLD,
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.DereferencingInvalidDIDUrl,
expectedError: types.InvalidDIDUrlError,
},
{
name: "not supported path",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}),
dereferencingType: types.DIDJSONLD,
didUrl: validDid + "/unknown_path",
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.DereferencingNotSupported,
expectedError: types.RepresentationNotSupportedError,
},
{
name: "not supported query",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}),
dereferencingType: types.DIDJSONLD,
didUrl: validDid + "?unknown_query",
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.DereferencingNotSupported,
expectedError: types.RepresentationNotSupportedError,
},
{
name: "key not found",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}),
dereferencingType: types.DIDJSONLD,
didUrl: validDid + "#notFoundKey",
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.DereferencingNotFound,
expectedError: types.NotFoundError,
},
{
name: "resource not found",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}),
dereferencingType: types.DIDJSONLD,
didUrl: validDid + types.RESOURCE_PATH + "00000000-0000-0000-0000-000000000000",
expectedMetadata: types.ResolutionDidDocMetadata{},
expectedError: types.DereferencingNotFound,
expectedError: types.NotFoundError,
},
}

for _, subtest := range subtests {
t.Run(subtest.name, func(t *testing.T) {
requestService := NewRequestService("cheqd", subtest.ledgerService)
var expectedDIDProperties types.DidProperties
if subtest.expectedError != types.DereferencingInvalidDIDUrl {
if subtest.expectedError != types.InvalidDIDUrlError {
expectedDIDProperties = types.DidProperties{
DidString: validDid,
MethodSpecificId: validIdentifier,
Expand Down
42 changes: 34 additions & 8 deletions types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,51 @@ package types
type ErrorType string

const (
ResolutionInvalidDID ErrorType = "invalidDid"
ResolutionNotFound ErrorType = "notFound"
ResolutionMethodNotSupported ErrorType = "methodNotSupported"
InvalidDIDError ErrorType = "invalidDid"
InvalidDIDUrlError ErrorType = "invalidDidUrl"
NotFoundError ErrorType = "notFound"
RepresentationNotSupportedError ErrorType = "representationNotSupported"
MethodNotSupportedError ErrorType = "methodNotSupported"
InternalError ErrorType = "internalError"
)

const (
DereferencingInvalidDIDUrl ErrorType = "invalidDidUrl"
DereferencingNotFound ErrorType = "notFound"
DereferencingNotSupported ErrorType = "urlNotSupported"
)
func (e ErrorType) GetStatusCode() int {
switch e {
case InvalidDIDError:
return 400
case InvalidDIDUrlError:
return 400
case NotFoundError:
return 404
case RepresentationNotSupportedError:
return 406
case MethodNotSupportedError:
return 406
case "":
return 200
default:
return 500
}
}

type ContentType string

const (
DIDJSON ContentType = "application/did+json"
DIDJSONLD ContentType = "application/did+ld+json"
JSONLD ContentType = "application/ld+json"
JSON ContentType = "application/json"
)

func (cType ContentType) IsSupported() bool {
supportedTypes := map[ContentType]bool{
DIDJSON: true,
DIDJSONLD: true,
JSONLD: true,
}
return supportedTypes[cType]
}

const (
DIDSchemaJSONLD = "https://www.w3.org/ns/did/v1"
)
Expand Down

0 comments on commit 499f895

Please sign in to comment.