Skip to content

Commit

Permalink
feat: Accept contetType params for DID URL Resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Jan 31, 2025
1 parent d2ef828 commit cfe32ba
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
24 changes: 24 additions & 0 deletions services/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,27 @@ func PrepareQueries(c echo.Context) (rawQuery string, flag *string) {
func GetDidParam(c echo.Context) (string, error) {
return url.QueryUnescape(c.Param("did"))
}

func ExtractMediaTypeParams(mediaType string) map[string]string {
// Initialize an empty map to hold the parameters
params := make(map[string]string)

// Split by ';' to separate media type from parameters
parts := strings.Split(mediaType, ";")
if len(parts) <= 1 {
return params // No parameters present
}

// Iterate over the parts (skipping the first one, which is the media type)
for _, param := range parts[1:] {
param = strings.TrimSpace(param) // Trim spaces around the parameter
// Split parameter by '=' to get the key and value
keyValue := strings.Split(param, "=")
if len(keyValue) == 2 {
// Store the key and value in the map
params[keyValue[0]] = strings.Trim(keyValue[1], "\"") // Remove quotes from value
}
}

return params
}
8 changes: 8 additions & 0 deletions services/resource/echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"github.com/cheqd/did-resolver/services"
"github.com/cheqd/did-resolver/types"
"github.com/labstack/echo/v4"
)

Expand All @@ -22,6 +23,13 @@ import (
// @Failure 501 {object} types.IdentityError
// @Router /{did}/resources/{resourceId} [get]
func ResourceDataEchoHandler(c echo.Context) error {
// Get Accept header
requestedContentTypeParam := services.ExtractMediaTypeParams(c.Request().Header.Get(echo.HeaderAccept))

if requestedContentTypeParam["profile"] == types.W3IDDIDURL {
return services.EchoWrapHandler(&ResourceDataWithMetadataDereferencingService{})(c)
}

return services.EchoWrapHandler(&ResourceDataDereferencingService{})(c)
}

Expand Down
54 changes: 54 additions & 0 deletions services/resource/resource_data_with_metadata_dereferencing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package resources

import (
"net/http"

"github.com/cheqd/did-resolver/migrations"
"github.com/cheqd/did-resolver/services"
"github.com/cheqd/did-resolver/types"
"github.com/cheqd/did-resolver/utils"
)

type ResourceDataWithMetadataDereferencingService struct {
services.BaseRequestService
ResourceId string
}

func (dr *ResourceDataWithMetadataDereferencingService) Setup(c services.ResolverContext) error {
dr.IsDereferencing = true
return nil
}

func (dr *ResourceDataWithMetadataDereferencingService) SpecificPrepare(c services.ResolverContext) error {
dr.ResourceId = c.Param("resource")
return nil
}

func (dr ResourceDataWithMetadataDereferencingService) Redirect(c services.ResolverContext) error {
migratedDid := migrations.MigrateDID(dr.GetDid())

path := types.RESOLVER_PATH + migratedDid + types.RESOURCE_PATH + dr.ResourceId
return c.Redirect(http.StatusMovedPermanently, path)
}

func (dr *ResourceDataWithMetadataDereferencingService) SpecificValidation(c services.ResolverContext) error {
if !utils.IsValidUUID(dr.ResourceId) {
return types.NewInvalidDidUrlError(dr.ResourceId, dr.RequestedContentType, nil, dr.IsDereferencing)
}

// We not allow query here
if len(dr.Queries) != 0 {
return types.NewInvalidDidUrlError(dr.GetDid(), dr.RequestedContentType, nil, dr.IsDereferencing)
}
return nil
}

func (dr *ResourceDataWithMetadataDereferencingService) Query(c services.ResolverContext) error {
result, err := c.ResourceService.DereferenceResourceWithMetadata(dr.GetDid(), dr.ResourceId, dr.GetContentType())
if err != nil {
err.IsDereferencing = dr.IsDereferencing
return err
}

return dr.SetResponse(result)
}
20 changes: 20 additions & 0 deletions services/resource_dereference_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ func (rds ResourceService) DereferenceResourceData(did string, resourceId string

return &types.ResourceDereferencing{ContentStream: &result, DereferencingMetadata: dereferenceMetadata}, nil
}

func (rds ResourceService) DereferenceResourceWithMetadata(did string, resourceId string, contentType types.ContentType) (*types.ResourceDereferencing, *types.IdentityError) {
dereferenceMetadata := types.NewDereferencingMetadata(did, contentType, "")

resource, err := rds.ledgerService.QueryResource(did, strings.ToLower(resourceId))
if err != nil {
err.ContentType = contentType
return nil, err
}

var context string
if contentType == types.DIDJSONLD || contentType == types.JSONLD {
context = types.ResolutionSchemaJSONLD
}

result := types.DereferencedResourceData(resource.Resource.Data)
metadata := types.NewDereferencedResource(did, resource.Metadata)

return &types.ResourceDereferencing{Context: context, ContentStream: &result, Metadata: metadata, DereferencingMetadata: dereferenceMetadata}, nil
}
9 changes: 5 additions & 4 deletions types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package types
type ContentType string

const (
DIDJSON ContentType = "application/did+json"
DIDJSONLD ContentType = "application/did+ld+json"
JSONLD ContentType = "application/ld+json"
JSON ContentType = "application/json"
DIDJSON ContentType = "application/did+json"
DIDJSONLD ContentType = "application/did+ld+json"
JSONLD ContentType = "application/ld+json"
JSON ContentType = "application/json"
W3IDDIDURL string = "https://w3id.org/did-url-dereferencing"
)

func (cType ContentType) IsSupported() bool {
Expand Down
2 changes: 1 addition & 1 deletion types/resource_metadata.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package types

type ResolutionResourceMetadata struct{}
type ResolutionResourceMetadata interface{}

0 comments on commit cfe32ba

Please sign in to comment.