-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
290 lines (254 loc) · 7.22 KB
/
db.go
File metadata and controls
290 lines (254 loc) · 7.22 KB
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"sync"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/unixpickle/essentials"
)
var (
ErrPassword = errors.New("password incorrect")
ErrNoEmail = errors.New("no such email address")
)
type Availability int
const (
Offline Availability = iota
Available
Away
)
// UserStatus stores a user's current status.
type UserStatus struct {
Availability Availability
Message string
Time time.Time
UserMetadata string
}
// UserInfo stores meta-data for a user.
//
// This does not include information that relies on a
// a user's current connection.
type UserInfo struct {
Email string
Hash []byte
VerifyToken string
Verified bool
Buddies []string
IncomingRequests []string
OutgoingRequests []string
LatestStatus UserStatus
}
// Copy creates a deep copy of the object.
func (u *UserInfo) Copy() *UserInfo {
res := *u
for _, field := range []*[]string{&res.Buddies, &res.IncomingRequests, &res.OutgoingRequests} {
*field = append([]string{}, *field...)
}
return &res
}
// A DB provides synchronized access to a persistent store
// of user information.
type DB interface {
AddUser(email, password string) error
VerifyUser(email, token string) error
CheckLogin(email, password string) error
GetUserInfo(email string) (*UserInfo, error)
SetPassword(email, oldPass, newPass string) error
SendRequest(from, to string) error
AcceptRequest(email, other string) error
DeleteBuddy(email, other string) error
SetStatus(email string, status UserStatus) error
GetStatuses(emails []string) ([]UserStatus, error)
}
type fileDB struct {
Lock sync.RWMutex
Path string
UserRecords []*UserInfo
}
func (f *fileDB) AddUser(email, password string) error {
return f.mutate("add user", func() error {
if f.findUser(email) != nil {
return errors.New("email already in use")
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
f.UserRecords = append(f.UserRecords, &UserInfo{
Email: email,
Hash: hash,
// TODO: support verification.
Verified: true,
LatestStatus: UserStatus{Availability: Available, Time: time.Now()},
})
return nil
})
}
func (f *fileDB) VerifyUser(email, token string) error {
// TODO: support verification.
return nil
}
func (f *fileDB) CheckLogin(email, password string) (err error) {
defer essentials.AddCtxTo("check login", &err)
f.Lock.RLock()
defer f.Lock.RUnlock()
if user := f.findUser(email); user != nil {
return bcrypt.CompareHashAndPassword(user.Hash, []byte(password))
}
return ErrNoEmail
}
func (f *fileDB) GetUserInfo(email string) (*UserInfo, error) {
f.Lock.RLock()
defer f.Lock.RUnlock()
if user := f.findUser(email); user != nil {
return user.Copy(), nil
}
return nil, essentials.AddCtx("get user info", ErrNoEmail)
}
func (f *fileDB) SetPassword(email, oldPass, newPass string) error {
return f.mutate("set password", func() error {
if user := f.findUser(email); user != nil {
if err := bcrypt.CompareHashAndPassword(user.Hash, []byte(oldPass)); err != nil {
return err
}
hash, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
if err != nil {
return err
}
user.Hash = hash
}
return ErrNoEmail
})
}
func (f *fileDB) SendRequest(from, to string) error {
return f.mutate("send request", func() error {
if fromUser := f.findUser(from); fromUser != nil {
if toUser := f.findUser(to); toUser != nil {
if containsEmail(toUser.Buddies, fromUser.Email) {
return errors.New("already buddies")
} else if containsEmail(toUser.OutgoingRequests, fromUser.Email) {
return errors.New("request exists in the other direction")
} else if containsEmail(toUser.IncomingRequests, fromUser.Email) {
return errors.New("request already exists")
}
toUser.IncomingRequests = append(toUser.IncomingRequests, fromUser.Email)
fromUser.OutgoingRequests = append(fromUser.OutgoingRequests, toUser.Email)
return nil
}
}
return ErrNoEmail
})
}
func (f *fileDB) AcceptRequest(email, other string) error {
return f.mutate("accept request", func() error {
if user := f.findUser(email); user != nil {
if otherUser := f.findUser(other); otherUser != nil {
if !containsEmail(otherUser.OutgoingRequests, user.Email) {
return errors.New("request does not exist")
}
removeEmail(&otherUser.OutgoingRequests, user.Email)
removeEmail(&user.IncomingRequests, otherUser.Email)
otherUser.Buddies = append(otherUser.Buddies, user.Email)
user.Buddies = append(user.Buddies, otherUser.Email)
return nil
}
}
return ErrNoEmail
})
}
func (f *fileDB) DeleteBuddy(email, other string) error {
return f.mutate("delete buddy", func() error {
if user := f.findUser(email); user != nil {
if otherUser := f.findUser(other); otherUser != nil {
if containsEmail(user.IncomingRequests, otherUser.Email) {
removeEmail(&user.IncomingRequests, otherUser.Email)
removeEmail(&otherUser.OutgoingRequests, user.Email)
} else if containsEmail(user.OutgoingRequests, otherUser.Email) {
removeEmail(&user.OutgoingRequests, otherUser.Email)
removeEmail(&otherUser.IncomingRequests, user.Email)
} else if containsEmail(user.Buddies, otherUser.Email) {
removeEmail(&user.Buddies, otherUser.Email)
removeEmail(&otherUser.Buddies, user.Email)
} else {
return errors.New("not buddies")
}
return nil
}
}
return ErrNoEmail
})
}
func (f *fileDB) SetStatus(email string, status UserStatus) error {
return f.mutate("set status", func() error {
if user := f.findUser(email); user != nil {
if status.Availability != Available && status.Availability != Away {
return errors.New("invalid availability")
}
user.LatestStatus = status
user.LatestStatus.Time = time.Now()
return nil
}
return ErrNoEmail
})
}
func (f *fileDB) GetStatuses(emails []string) ([]*UserStatus, error) {
f.Lock.RLock()
defer f.Lock.RUnlock()
var result []*UserStatus
for _, email := range emails {
if user := f.findUser(email); user != nil {
status := user.LatestStatus
result = append(result, &status)
} else {
return nil, ErrNoEmail
}
}
return result, nil
}
func (f *fileDB) mutate(ctx string, mutator func() error) (err error) {
f.Lock.Lock()
defer f.Lock.Unlock()
if err := mutator(); err != nil {
return essentials.AddCtx(ctx, err)
}
contents, err := json.Marshal(f.UserRecords)
if err != nil {
return err
}
return ioutil.WriteFile(f.Path, contents, 0600)
}
func (f *fileDB) findUser(email string) *UserInfo {
for _, user := range f.UserRecords {
if emailsEquivalent(user.Email, email) {
return user
}
}
return nil
}
func emailsEquivalent(e1, e2 string) bool {
// TODO: check for dots, case sensitivity, etc.
return e1 == e2
}
func containsEmail(list []string, email string) bool {
for _, item := range list {
if emailsEquivalent(item, email) {
return true
}
}
return false
}
func removeEmail(list *[]string, email string) {
for i, item := range *list {
if emailsEquivalent(item, email) {
essentials.OrderedDelete(list, i)
return
}
}
}
func hashPassword(pass string) string {
hash := sha256.Sum256([]byte(pass))
return hex.EncodeToString(hash[:])
}