Skip to content

Commit

Permalink
✨ Cross validate @key and @assetType on NewKey
Browse files Browse the repository at this point in the history
  • Loading branch information
andremacedopv committed Jul 5, 2023
1 parent d9d5bba commit 4068477
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion assets/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package assets

import (
"encoding/json"
"net/http"
"strings"

"github.com/goledgerdev/cc-tools/errors"
)
Expand Down Expand Up @@ -49,8 +51,16 @@ func NewKey(m map[string]interface{}) (k Key, err errors.ICCError) {
k[t] = v
}

// Generate object key
// Validate if @key corresponds to asset type
_, keyExists := k["@key"]
if keyExists {
index := strings.Index(k["@assetType"].(string), k["@key"].(string))
if index != 0 {
keyExists = false
}
}

// Generate object key
if !keyExists {
var keyStr string
keyStr, err = GenerateKey(k)
Expand All @@ -60,6 +70,12 @@ func NewKey(m map[string]interface{}) (k Key, err errors.ICCError) {
k["@key"] = keyStr
}

// Validate if @key corresponds to asset type
index := strings.Index(k["@assetType"].(string), k["@key"].(string))
if index != 0 {
err = errors.NewCCError("key does not correspond to asset type", http.StatusBadRequest)
}

for t := range k {
if t != "@key" && t != "@assetType" {
delete(k, t)
Expand Down

0 comments on commit 4068477

Please sign in to comment.