This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.go
319 lines (247 loc) · 7.19 KB
/
persistence.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
package incognitomail
import (
"errors"
"time"
"github.com/boltdb/bolt"
)
// IncognitoData holds a "connection" to the persistence layer. To create a valid IncognitoData object, call OpenIncognitoData().
type IncognitoData struct {
db *bolt.DB
}
const (
targetsBucketName = "targets"
accountsBucketName = "accounts"
handlesBucketName = "handles"
)
var (
// ErrEmptySecret is used when an empty account secret is used.
ErrEmptySecret = errors.New("empty secret")
// ErrEmptyTarget is used when an empty account target is used.
ErrEmptyTarget = errors.New("empty target")
// ErrAccountNotFound is used when an action requires an account to exist, but it wasn't found.
ErrAccountNotFound = errors.New("account not found")
// ErrAccountExists is used when trying to create an account with a given secret, but it already exists.
ErrAccountExists = errors.New("account already exists")
// ErrHandleNotFound is used when an action requires a handle to exist, but it wasn't found.
ErrHandleNotFound = errors.New("handle not found")
// ErrHandleExists is used when trying to create a handle, but it already exists.
ErrHandleExists = errors.New("handle already exists")
)
// OpenIncognitoData returns an IncognitoData object with a successful "connection" to the persistence layer, ready to be used.
func OpenIncognitoData() (*IncognitoData, error) {
db, err := bolt.Open(Config.Persistence.DatabasePath, 0600, nil)
if err != nil {
return nil, err
}
// Create "static" buckets that are used for persistence
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(targetsBucketName))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(accountsBucketName))
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(handlesBucketName))
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return &IncognitoData{
db: db,
}, nil
}
// NewAccount generates a new account with the given secret and target email address.
func (a *IncognitoData) NewAccount(secret, target string) error {
if secret == "" {
return ErrEmptySecret
}
if target == "" {
return ErrEmptyTarget
}
err := a.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(secret))
if b != nil {
return ErrAccountExists
}
_, err := tx.CreateBucket([]byte(secret))
if err != nil {
return err
}
b = tx.Bucket([]byte(targetsBucketName))
err = b.Put([]byte(secret), []byte(target))
if err != nil {
return err
}
now, err := time.Now().GobEncode()
if err != nil {
return err
}
b = tx.Bucket([]byte(accountsBucketName))
err = b.Put([]byte(secret), now)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// DeleteAccount deletes all information related to the account with the given secret. If no account with that secret exists, it does nothing.
func (a *IncognitoData) DeleteAccount(secret string) {
if secret == "" {
return
}
// Delete all handles associated with this account first
handles, err := a.ListAccountHandles(secret)
if err != nil {
return
}
for _, v := range handles {
a.DeleteAccountHandle(secret, v)
}
a.db.Update(func(tx *bolt.Tx) error {
tx.DeleteBucket([]byte(secret))
b := tx.Bucket([]byte(targetsBucketName))
b.Delete([]byte(secret))
b = tx.Bucket([]byte(accountsBucketName))
b.Delete([]byte(secret))
return nil
})
}
// NewAccountHandle stores the given handle for the account with the given secret.
func (a *IncognitoData) NewAccountHandle(secret, handle string) error {
if secret == "" {
return ErrEmptySecret
}
err := a.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(secret))
if b == nil {
return ErrAccountNotFound
}
hb := tx.Bucket([]byte(handlesBucketName))
h := hb.Get([]byte(handle))
if h != nil {
return ErrHandleExists
}
now, err := time.Now().GobEncode()
if err != nil {
return err
}
err = b.Put([]byte(handle), now)
if err != nil {
return err
}
err = hb.Put([]byte(handle), now)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// DeleteAccountHandle deletes the given handle from the account with the given secret. If either the account or the handle does not exist, this does nothing.
func (a *IncognitoData) DeleteAccountHandle(secret, handle string) {
if secret == "" || handle == "" {
return
}
// Note that we still return errors from the following func, but don't care about them
a.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(secret))
if b == nil {
return ErrAccountNotFound
}
// Also delete from the global handles name
hb := tx.Bucket([]byte(handlesBucketName))
b.Delete([]byte(handle))
hb.Delete([]byte(handle))
return nil
})
}
// GetAccountTarget returns the target registered for the account with the given secret.
func (a *IncognitoData) GetAccountTarget(secret string) (string, error) {
if secret == "" {
return "", ErrEmptySecret
}
var target string
err := a.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(targetsBucketName))
t := b.Get([]byte(secret))
if t == nil {
return ErrAccountNotFound
}
// Note: boltdb only keeps the value of t until the transaction ends, so we must copy it somewhere else now.
// However, the call to string(t) internally does that for us, as it will ultimately call copy() to copy the values to a new byte slice for the resulting string.
target = string(t)
return nil
})
if err != nil {
return "", err
}
return target, nil
}
// HasAccount returns true if an account with the given secret exists, false otherwise.
func (a *IncognitoData) HasAccount(secret string) bool {
if secret == "" {
return false
}
err := a.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(accountsBucketName))
t := b.Get([]byte(secret))
if t == nil {
return ErrAccountNotFound
}
return nil
})
return err == nil
}
// HasHandleGlobal returns true if the given handle exists for any account, false otherwise.
func (a *IncognitoData) HasHandleGlobal(handle string) bool {
if handle == "" {
return false
}
err := a.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(handlesBucketName))
t := b.Get([]byte(handle))
if t == nil {
return ErrAccountNotFound
}
return nil
})
return err == nil
}
// ListAccountHandles returns an array with all handles from the account with the given secret.
func (a *IncognitoData) ListAccountHandles(secret string) ([]string, error) {
if secret == "" {
return nil, ErrEmptySecret
}
var result []string
err := a.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(secret))
b.ForEach(func(k, v []byte) error {
// Note: boltdb only keeps the values of k and v until the transaction ends, so we must copy these values somewhere else now.
// However, the call to string(k) internally does that for us, as it will ultimately call copy() to copy the values to a new byte slice for the resulting string.
result = append(result, string(k))
return nil
})
return nil
})
if err != nil {
return nil, err
}
return result, nil
}
// Close closes the "connection" with the persistence layer.
func (a *IncognitoData) Close() {
a.db.Close()
}