/
fileSafeCredentials.go
341 lines (262 loc) · 7.55 KB
/
fileSafeCredentials.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package items
import (
"encoding/json"
"fmt"
"slices"
"time"
"github.com/jonhadfield/gosn-v2/common"
)
type FileSafeCredentialsContent struct {
Keys json.RawMessage `json:"keys"`
AuthParams json.RawMessage `json:"authParams"`
IsDefault json.RawMessage `json:"isDefault"`
ItemReferences ItemReferences `json:"references"`
AppData AppDataContent `json:"appData"`
Name string `json:"name"`
DissociatedItemIds []string `json:"disassociatedItemIds"`
AssociatedItemIds []string `json:"associatedItemIds"`
Active interface{} `json:"active"`
}
func parseFileSafeCredentials(i DecryptedItem) Item {
c := FileSafeCredentials{}
c.UUID = i.UUID
c.ItemsKeyID = i.ItemsKeyID
c.ContentType = i.ContentType
c.Deleted = i.Deleted
c.UpdatedAt = i.UpdatedAt
c.CreatedAt = i.CreatedAt
c.ContentSize = len(i.Content)
var err error
if !c.Deleted {
var content Content
content, err = processContentModel(i.ContentType, i.Content)
if err != nil {
panic(err)
}
c.Content = *content.(*FileSafeCredentialsContent)
}
var cAt, uAt time.Time
cAt, err = parseSNTime(i.CreatedAt)
if err != nil {
panic(err)
}
c.CreatedAt = cAt.Format(common.TimeLayout)
uAt, err = parseSNTime(i.UpdatedAt)
if err != nil {
panic(err)
}
c.UpdatedAt = uAt.Format(common.TimeLayout)
return &c
}
type FileSafeCredentials struct {
ItemCommon
Content FileSafeCredentialsContent
}
func (c FileSafeCredentials) IsDefault() bool {
return false
}
func (i Items) FileSafeCredentials() (c FileSafeCredentialss) {
for _, x := range i {
if x.GetContentType() == "FileSafeCredentials" {
component := x.(*FileSafeCredentials)
c = append(c, *component)
}
}
return c
}
func (c *FileSafeCredentialss) DeDupe() {
var encountered []string
var deDuped FileSafeCredentialss
for _, i := range *c {
if !slices.Contains(encountered, i.UUID) {
deDuped = append(deDuped, i)
}
encountered = append(encountered, i.UUID)
}
*c = deDuped
}
// NewFileSafeCredentials returns an Item of type FileSafeCredentials without content.
func NewFileSafeCredentials() FileSafeCredentials {
now := time.Now().UTC().Format(common.TimeLayout)
var c FileSafeCredentials
c.ContentType = "FileSafeCredentials"
c.CreatedAt = now
c.UUID = GenUUID()
return c
}
// NewTagContent returns an empty Tag content instance.
func NewFileSafeCredentialsContent() *FileSafeCredentialsContent {
c := &FileSafeCredentialsContent{}
c.SetUpdateTime(time.Now().UTC())
return c
}
type FileSafeCredentialss []FileSafeCredentials
func (c FileSafeCredentialss) Validate() error {
var updatedTime time.Time
var err error
for _, item := range c {
// validate content if being added
if !item.Deleted {
updatedTime, err = item.Content.GetUpdateTime()
if err != nil {
return err
}
switch {
case item.Content.Name == "":
err = fmt.Errorf("failed to create \"%s\" due to missing title: \"%s\"",
item.ContentType, item.UUID)
case updatedTime.IsZero():
err = fmt.Errorf("failed to create \"%s\" due to missing content updated time: \"%s\"",
item.ContentType, item.Content.GetTitle())
case item.CreatedAt == "":
err = fmt.Errorf("failed to create \"%s\" due to missing created at date: \"%s\"",
item.ContentType, item.Content.GetTitle())
}
if err != nil {
return err
}
}
}
return err
}
func (c FileSafeCredentials) IsDeleted() bool {
return c.Deleted
}
func (c *FileSafeCredentials) SetDeleted(d bool) {
c.Deleted = d
}
func (c FileSafeCredentials) GetContent() Content {
return &c.Content
}
func (c *FileSafeCredentials) SetContent(cc Content) {
c.Content = *cc.(*FileSafeCredentialsContent)
}
func (c FileSafeCredentials) GetItemsKeyID() string {
return c.ItemsKeyID
}
func (c FileSafeCredentials) GetUUID() string {
return c.UUID
}
func (c FileSafeCredentials) GetDuplicateOf() string {
return c.DuplicateOf
}
func (c *FileSafeCredentials) SetUUID(u string) {
c.UUID = u
}
func (c FileSafeCredentials) GetContentType() string {
return c.ContentType
}
func (c FileSafeCredentials) GetCreatedAt() string {
return c.CreatedAt
}
func (c *FileSafeCredentials) SetCreatedAt(ca string) {
c.CreatedAt = ca
}
func (c FileSafeCredentials) GetUpdatedAt() string {
return c.UpdatedAt
}
func (c *FileSafeCredentials) SetUpdatedAt(ca string) {
c.UpdatedAt = ca
}
func (c FileSafeCredentials) GetCreatedAtTimestamp() int64 {
return c.CreatedAtTimestamp
}
func (c *FileSafeCredentials) SetCreatedAtTimestamp(ca int64) {
c.CreatedAtTimestamp = ca
}
func (c FileSafeCredentials) GetUpdatedAtTimestamp() int64 {
return c.UpdatedAtTimestamp
}
func (c *FileSafeCredentials) SetUpdatedAtTimestamp(ca int64) {
c.UpdatedAtTimestamp = ca
}
func (c *FileSafeCredentials) SetContentType(ct string) {
c.ContentType = ct
}
func (c FileSafeCredentials) GetContentSize() int {
return c.ContentSize
}
func (c *FileSafeCredentials) SetContentSize(s int) {
c.ContentSize = s
}
func (cc *FileSafeCredentialsContent) AssociateItems(newItems []string) {
// add to associated item ids
for _, newRef := range newItems {
var existingFound bool
var existingDFound bool
for _, existingRef := range cc.AssociatedItemIds {
if existingRef == newRef {
existingFound = true
}
}
for _, existingDRef := range cc.DissociatedItemIds {
if existingDRef == newRef {
existingDFound = true
}
}
// add reference if it doesn't exist
if !existingFound {
cc.AssociatedItemIds = append(cc.AssociatedItemIds, newRef)
}
// remove reference (from disassociated) if it does exist in that list
if existingDFound {
cc.DissociatedItemIds = removeStringFromSlice(newRef, cc.DissociatedItemIds)
}
}
}
func (cc *FileSafeCredentialsContent) GetItemAssociations() []string {
return cc.AssociatedItemIds
}
func (cc *FileSafeCredentialsContent) GetItemDisassociations() []string {
return cc.DissociatedItemIds
}
func (cc *FileSafeCredentialsContent) DisassociateItems(itemsToRemove []string) {
// remove from associated item ids
for _, delRef := range itemsToRemove {
var existingFound bool
for _, existingRef := range cc.AssociatedItemIds {
if existingRef == delRef {
existingFound = true
}
}
// remove reference (from disassociated) if it does exist in that list
if existingFound {
cc.AssociatedItemIds = removeStringFromSlice(delRef, cc.AssociatedItemIds)
}
}
}
func (cc *FileSafeCredentialsContent) GetUpdateTime() (time.Time, error) {
if cc.AppData.OrgStandardNotesSN.ClientUpdatedAt == "" {
return time.Time{}, fmt.Errorf("ClientUpdatedAt not set")
}
return time.Parse(common.TimeLayout, cc.AppData.OrgStandardNotesSN.ClientUpdatedAt)
}
func (cc *FileSafeCredentialsContent) SetUpdateTime(uTime time.Time) {
cc.AppData.OrgStandardNotesSN.ClientUpdatedAt = uTime.Format(common.TimeLayout)
}
func (cc FileSafeCredentialsContent) GetTitle() string {
return ""
}
func (cc *FileSafeCredentialsContent) GetName() string {
return cc.Name
}
func (cc *FileSafeCredentialsContent) GetActive() bool {
return cc.Active.(bool)
}
func (cc *FileSafeCredentialsContent) SetTitle(title string) {
}
func (cc *FileSafeCredentialsContent) GetAppData() AppDataContent {
return cc.AppData
}
func (cc *FileSafeCredentialsContent) SetAppData(data AppDataContent) {
cc.AppData = data
}
func (cc FileSafeCredentialsContent) References() ItemReferences {
return cc.ItemReferences
}
func (cc *FileSafeCredentialsContent) UpsertReferences(input ItemReferences) {
panic("implement me")
}
func (cc *FileSafeCredentialsContent) SetReferences(input ItemReferences) {
cc.ItemReferences = input
}