-
Notifications
You must be signed in to change notification settings - Fork 9
/
item.go
194 lines (160 loc) · 4.49 KB
/
item.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package libsf
import (
"encoding/json"
"time"
"github.com/pkg/errors"
)
const (
// ContentTypeUserPreferences for items that holds user's preferences.
ContentTypeUserPreferences = "SN|UserPreferences"
// ContentTypePrivileges for items that holds note's privileges.
ContentTypePrivileges = "SN|Privileges"
// ContentTypeComponent are items that describes an editor extension.
ContentTypeComponent = "SN|Component"
// ContentTypeNote are the items with user's written data.
ContentTypeNote = "Note"
)
type (
// A SyncItems is used when a client want to sync items.
SyncItems struct {
// Common fields
API string `json:"api"` // Since 20190520
ComputeIntegrity bool `json:"compute_integrity"`
Limit int `json:"limit"`
SyncToken string `json:"sync_token"`
CursorToken string `json:"cursor_token"`
ContentType string `json:"content_type"` // optional, only return items of these type if present
// Fields used for request
Items []*Item `json:"items"`
// Fields used in response
Retrieved []*Item `json:"retrieved_items"`
Saved []*Item `json:"saved_items"`
// 20161215 fields
Unsaved []*UnsavedItem `json:"unsaved"`
// 20190520 fields
Conflicts []*ConflictItem `json:"conflicts"`
}
// An Item holds all the data created by end user.
Item struct {
ID string `json:"uuid"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
UserID string `json:"user_uuid"`
Content string `json:"content"`
ContentType string `json:"content_type"`
EncryptedItemKey string `json:"enc_item_key"`
Deleted bool `json:"deleted"`
// Internal
AuthParams Auth `json:"-"`
Note *Note `json:"-"`
key vault
content vault
}
// An UnsavedItem is an object containing an item that has not been saved.
// Used before API version 20190520.
UnsavedItem struct {
Item Item `json:"item"`
Error struct {
Message string `json:"message"`
Tag string `json:"tag"`
} `json:"error"`
}
// A ConflictItem is an object containing an item that can't be saved caused by conflicts.
// Used since API version 20190520.
ConflictItem struct {
UnsavedItem Item `json:"unsaved_item,omitempty"`
ServerItem Item `json:"server_item,omitempty"`
Type string `json:"type"`
}
)
// NewSyncItems returns an empty SyncItems with initilized defaults.
func NewSyncItems() SyncItems {
return SyncItems{
Items: []*Item{},
Retrieved: []*Item{},
Saved: []*Item{},
Unsaved: []*UnsavedItem{},
Conflicts: []*ConflictItem{},
}
}
// Seal encrypts Note to item's Content.
func (i *Item) Seal(mk, ak string) error {
//
// Key
//
ik, err := GenerateItemEncryptionKey()
if err != nil {
return errors.Wrap(err, "could not generate encryption key")
}
i.key = vault{
version: i.AuthParams.Version(),
uuid: i.ID,
params: i.AuthParams,
}
err = i.key.seal([]byte(ik), mk, ak)
if err != nil {
return errors.Wrap(err, "EncryptedItemKey")
}
i.EncryptedItemKey, err = serialize(i.key)
if err != nil {
return errors.Wrap(err, "EncryptedItemKey")
}
//
// Content
//
// Split item key in encryption key and auth key
ek := ik[:len(ik)/2]
ak = ik[len(ik)/2:]
note, err := json.Marshal(i.Note)
if err != nil {
return errors.Wrap(err, "could not serialize note")
}
i.content = vault{
version: i.AuthParams.Version(),
uuid: i.ID,
params: i.AuthParams,
}
err = i.content.seal(note, ek, ak)
if err != nil {
return errors.Wrap(err, "Content")
}
i.Content, err = serialize(i.content)
return errors.Wrap(err, "Content")
}
// Unseal decrypts the item's Content into Note.
func (i *Item) Unseal(mk, ak string) error {
//
// Key
//
if i.EncryptedItemKey == "" {
return errors.New("missing item encryption key")
}
v, err := parse(i.EncryptedItemKey, i.ID)
if err != nil {
return errors.Wrap(err, "EncryptedItemKey")
}
i.key = v
i.AuthParams = v.params // Set current auth params as default
ik, err := i.key.unseal(mk, ak)
if err != nil {
return errors.Wrap(err, "EncryptedItemKey")
}
//
// Content
//
v, err = parse(i.Content, i.ID)
if err != nil {
return errors.Wrap(err, "Content")
}
i.content = v
// Split item key in encryption key and auth key
ek := string(ik[:len(ik)/2])
ak = string(ik[len(ik)/2:])
note, err := i.content.unseal(ek, ak)
if err != nil {
return errors.Wrap(err, "Content")
}
i.Note = new(Note)
err = json.Unmarshal(note, i.Note)
return errors.Wrap(err, "could not parse note")
}