-
Notifications
You must be signed in to change notification settings - Fork 1
/
standard.go
157 lines (132 loc) · 3.45 KB
/
standard.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
package sfgo
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
uuid "github.com/satori/go.uuid"
)
type Items struct {
RetrievedItems []Item `json:"retrieved_items"`
SavedItems []Item `json:"saved_items"`
Unsaved []Item `json:"unsaved"`
SyncToken string `json:"sync_token"`
CursorToken *string `json:"cursor_token"`
}
type Item struct {
UUID string `json:"uuid"`
Content string `json:"content"`
ContentType string `json:"content_type"`
EncItemKey string `json:"enc_item_key"`
AuthHash *string `json:"auth_hash"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Deleted bool `json:"deleted"`
PlanText []byte `json:"-"`
}
type syncReply struct {
Items []Item `json:"items"`
SyncToken *string `json:"sync_token"`
Limit int `json:"limit"`
CursorToken *string `json:"cursor_token"`
}
// UpdateItem Put item in que to be synced
func (sess *Session) UpdateItem(item Item) error {
sess.AddedItems = append(sess.AddedItems, item)
return nil
}
// EncryptItem Encrypt an item with existing EncItemKey
func (sess *Session) EncryptItem(item *Item) error {
if len(item.EncItemKey) < 1 {
return errors.New("Item did not contain EncItemKey")
}
sess.generateContent(item)
return nil
}
// NewItem Create new item
func (sess *Session) NewItem(plainText []byte, contentType string) error {
uid, err := uuid.NewV4()
if err != nil {
return err
}
item := Item{
UUID: uid.String(),
ContentType: contentType,
CreatedAt: time.Now().String(),
UpdatedAt: time.Now().String(),
Deleted: false,
PlanText: plainText,
}
err = sess.generateEncItemKey(&item)
if err != nil {
return err
}
err = sess.generateContent(&item)
if err != nil {
return err
}
sess.AddedItems = append(sess.AddedItems, item)
return nil
}
// NewSession create a new session
func NewSession(URL string, email string) *Session {
return &Session{
URL: URL,
Email: email,
AddedItems: make([]Item, 0),
}
}
// Sync Sync with server
func (sess *Session) Sync() (Items, error) {
r := syncReply{
SyncToken: sess.SyncToken,
Limit: 150,
Items: sess.AddedItems,
}
jsonStr, err := json.Marshal(&r)
if err != nil {
return Items{}, err
}
sess.AddedItems = make([]Item, 0) // Items should now be synced. Delete them
req, err := http.NewRequest("POST", sess.URL+"/items/sync", bytes.NewBuffer(jsonStr))
if err != nil {
return Items{}, err
}
req.Header.Add("Authorization", "Bearer "+sess.JWT)
req.Header.Add("Content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return Items{}, err
}
var items Items
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&items)
if err != nil {
return Items{}, err
}
// For some reason a newline is added to this. Remove it
stoken := strings.TrimRight(items.SyncToken, "\n")
sess.SyncToken = &stoken // Update sync token
return items, nil
}
type Session struct {
Email string
JWT string
URL string
SyncToken *string
Auth AuthParmas
AddedItems []Item // List off items that has not been synced
}
type AuthParmas struct {
Identifier string `json:"identifier"`
PwSalt *string `json:"pw_salt"`
PwCost int `json:"pw_cost"`
PwNonce string `json:"pw_nonce"`
Version string `json:"version"`
PW string `json:"-"`
MK string `json:"-"`
AK string `json:"-"`
}