/
ietf.go
76 lines (59 loc) · 1.62 KB
/
ietf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package patchvalidator
import (
"encoding/json"
"fmt"
"strings"
jsonpatch "github.com/evanphx/json-patch"
"github.com/trustbloc/sidetree-go/pkg/document"
"github.com/trustbloc/sidetree-go/pkg/patch"
)
// NewJSONValidator creates new validator.
func NewJSONValidator() *JSONValidator {
return &JSONValidator{}
}
// JSONValidator implements validator for "ietf-json-patch" patch.
type JSONValidator struct {
}
// Validate validates patch.
func (v *JSONValidator) Validate(p patch.Patch) error {
value, err := p.GetValue()
if err != nil {
return err
}
patches, err := getRequiredArray(value)
if err != nil {
return fmt.Errorf("invalid json patch value: %s", err.Error())
}
patchesBytes, err := json.Marshal(patches)
if err != nil {
return err
}
return validateJSONPatches(patchesBytes)
}
func validateJSONPatches(patches []byte) error {
jsonPatches, err := jsonpatch.DecodePatch(patches)
if err != nil {
return fmt.Errorf("%s: %s", patch.JSONPatch, err.Error())
}
for _, p := range jsonPatches {
pathMsg, ok := p["path"]
if !ok {
return fmt.Errorf("%s: path not found", patch.JSONPatch)
}
var path string
if err := json.Unmarshal(*pathMsg, &path); err != nil {
return fmt.Errorf("%s: invalid path", patch.JSONPatch)
}
if strings.HasPrefix(path, "/"+document.ServiceProperty) {
return fmt.Errorf("%s: cannot modify services", patch.JSONPatch)
}
if strings.HasPrefix(path, "/"+document.PublicKeyProperty) {
return fmt.Errorf("%s: cannot modify public keys", patch.JSONPatch)
}
}
return nil
}