Skip to content

Commit

Permalink
Add unmarshaller for ResourceDereferencing && isBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Feb 25, 2025
1 parent c354c1b commit aee3b30
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
5 changes: 5 additions & 0 deletions services/diddoc/diddoc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (dd *QueryDIDDocRequestService) Setup(c services.ResolverContext) error {
func (dd *QueryDIDDocRequestService) SpecificPrepare(c services.ResolverContext) error {
// if profile is W3IDDIDRES then dereferencing is false
acceptHeader := c.Request().Header.Get(echo.HeaderAccept)
userAgent := c.Request().UserAgent()
// TODO: Remove this when an alternate fix is created for browsers
if services.IsBrowser(userAgent) {
acceptHeader = types.DEFAULT_RESOLUTION_TYPE
}
contentType, profile := services.GetPriorityContentType(acceptHeader, dd.AreResourceQueriesPlaced(c))

dd.Profile = profile
Expand Down
19 changes: 14 additions & 5 deletions services/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ import (
"github.com/timewasted/go-accept-headers"
)

func IsBrowser(userAgent string) bool {
userAgent = strings.ToLower(userAgent)

return strings.Contains(userAgent, "chrome") ||
strings.Contains(userAgent, "firefox") ||
(strings.Contains(userAgent, "safari") && !strings.Contains(userAgent, "chrome")) ||
strings.Contains(userAgent, "opera") ||
strings.Contains(userAgent, "opr") ||
strings.Contains(userAgent, "edg") ||
strings.Contains(userAgent, "msie") ||
strings.Contains(userAgent, "trident")
}

func GetPriorityContentType(acceptHeader string, resource bool) (types.ContentType, string) {
// Parse the Accept header using the go-accept-headers package
if acceptHeader == types.DEFAULT_BROWSER_HEADER {
acceptHeader = "*/*"
}

acceptedTypes := accept.Parse(acceptHeader)
if len(acceptedTypes) == 0 {
// default content type
Expand All @@ -23,7 +32,7 @@ func GetPriorityContentType(acceptHeader string, resource bool) (types.ContentTy
for _, at := range acceptedTypes {
mediaType := types.ContentType(at.Type + "/" + at.Subtype)
// If the Header contains any media type, return the default content type
if mediaType == "*/*" {
if mediaType == types.DEFAULT_RESOLUTION_TYPE {
if resource { // If request is from Resource Handlers
return types.JSONLD, ""
} else { // If request is from DIDDoc Handlers
Expand Down
32 changes: 16 additions & 16 deletions types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ 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"
DIDRES ContentType = "application/did"
W3IDDIDRES string = "https://w3id.org/did-resolution"
TEXT ContentType = "text/plain"
W3IDDIDURL string = "https://w3id.org/did-url-dereferencing"
DEFAULT_BROWSER_HEADER string = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
DIDJSON ContentType = "application/did+json"
DIDJSONLD ContentType = "application/did+ld+json"
JSONLD ContentType = "application/ld+json"
JSON ContentType = "application/json"
DIDRES ContentType = "application/did"
W3IDDIDRES string = "https://w3id.org/did-resolution"
TEXT ContentType = "text/plain"
W3IDDIDURL string = "https://w3id.org/did-url-dereferencing"
)

func (cType ContentType) IsSupported() bool {
Expand Down Expand Up @@ -52,13 +51,14 @@ const (
)

const (
DID_METHOD = "cheqd"
RESOLVER_PATH = "/1.0/identifiers/"
DID_VERSION_PATH = "/version/"
DID_VERSIONS_PATH = "/versions"
DID_METADATA = "/metadata"
RESOURCE_PATH = "/resources/"
SWAGGER_PATH = "/swagger/*"
DID_METHOD = "cheqd"
RESOLVER_PATH = "/1.0/identifiers/"
DID_VERSION_PATH = "/version/"
DID_VERSIONS_PATH = "/versions"
DID_METADATA = "/metadata"
RESOURCE_PATH = "/resources/"
SWAGGER_PATH = "/swagger/*"
DEFAULT_RESOLUTION_TYPE = "*/*"
)

const (
Expand Down
21 changes: 21 additions & 0 deletions types/resource_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ func (e *ResolutionResourceMetadata) MarshalJSON() ([]byte, error) {
Resources: e.Resources,
})
}

func (e *ResolutionResourceMetadata) UnmarshalJSON(data []byte) error {
// Define a temporary structure to assist with unmarshalling
var aux struct {
Resources *DereferencedResourceList `json:"linkedResourceMetadata,omitempty"`
}

// First, try to unmarshal into ContentMetadata
if err := json.Unmarshal(data, &e.ContentMetadata); err == nil && e.ContentMetadata != nil {
return nil // Successfully unmarshalled into ContentMetadata, return early
}

// If ContentMetadata is nil, try to unmarshal into Resources
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

// Assign the extracted Resources
e.Resources = aux.Resources
return nil
}

0 comments on commit aee3b30

Please sign in to comment.