This repository has been archived by the owner on Dec 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
293 lines (242 loc) · 8.01 KB
/
user.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
package sqlite
import (
"context"
"crypto/sha256"
"database/sql"
"fmt"
// import sqlite dependency
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
cabby "github.com/pladdy/cabby2"
)
const minPasswordLength = 8
// UserService implements a SQLite version of the servce
type UserService struct {
DB *sql.DB
DataStore *DataStore
}
// CreateUser creates a user in the data store
func (s UserService) CreateUser(ctx context.Context, user cabby.User, password string) error {
resource, action := "User", "create"
start := cabby.LogServiceStart(ctx, resource, action)
err := validateUserPasswordCombo(user, password)
if err == nil {
err = s.createUser(user, password)
} else {
log.WithFields(log.Fields{"error": err, "password": password, "user": user}).Error("Invalid user and/or password")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s UserService) createUser(u cabby.User, password string) error {
sql := `insert into taxii_user (email, can_admin) values (?, ?)`
args := []interface{}{u.Email, u.CanAdmin}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return err
}
sql = `insert into taxii_user_pass (email, pass) values (?, ?)`
args = []interface{}{u.Email, hash(password)}
err = s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// CreateUserCollection creates an association of a user to a collection
func (s UserService) CreateUserCollection(ctx context.Context, user string, ca cabby.CollectionAccess) error {
resource, action := "UserCollection", "create"
start := cabby.LogServiceStart(ctx, resource, action)
err := validateUserCollection(user, ca)
if err == nil {
err = s.createUserCollection(user, ca)
} else {
log.WithFields(log.Fields{"error": err, "collection_access": ca, "user": user}).Error("Invalid user and/or collection")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s UserService) createUserCollection(user string, ca cabby.CollectionAccess) error {
sql := `insert into taxii_user_collection (email, collection_id, can_read, can_write)
values (?, ?, ?, ?)`
args := []interface{}{user, ca.ID.String(), ca.CanRead, ca.CanWrite}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// DeleteUserCollection deletes a collection from a user
func (s UserService) DeleteUserCollection(ctx context.Context, user, id string) error {
resource, action := "UserCollection", "delete"
start := cabby.LogServiceStart(ctx, resource, action)
err := s.deleteUserCollection(user, id)
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s UserService) deleteUserCollection(user, id string) error {
sql := `delete from taxii_user_collection where email = ? and collection_id = ?`
args := []interface{}{user, id}
_, err := s.DB.Exec(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// DeleteUser creates a user in the data store
func (s UserService) DeleteUser(ctx context.Context, user string) error {
resource, action := "User", "delete"
start := cabby.LogServiceStart(ctx, resource, action)
err := s.deleteUser(user)
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s UserService) deleteUser(user string) error {
sql := `delete from taxii_user where email = ?`
args := []interface{}{user}
_, err := s.DB.Exec(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return err
}
sql = `delete from taxii_user_pass where email = ?`
_, err = s.DB.Exec(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// UpdateUser creates a user in the data store
func (s UserService) UpdateUser(ctx context.Context, user cabby.User) error {
resource, action := "User", "update"
start := cabby.LogServiceStart(ctx, resource, action)
err := user.Validate()
if err == nil {
err = s.updateUser(user)
} else {
log.WithFields(log.Fields{"error": err, "user": user}).Error("Invalid user")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s UserService) updateUser(u cabby.User) error {
sql := `update taxii_user set can_admin = ? where email = ?`
args := []interface{}{u.CanAdmin, u.Email}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// UpdateUserCollection update a users access to a specfific collection
func (s UserService) UpdateUserCollection(ctx context.Context, user string, ca cabby.CollectionAccess) error {
resource, action := "UserCollection", "update"
start := cabby.LogServiceStart(ctx, resource, action)
err := validateUserCollection(user, ca)
if err == nil {
err = s.updateUserCollection(user, ca)
} else {
log.WithFields(log.Fields{"error": err, "collection_access": ca, "user": user}).Error("Invalid user and/or collection")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s UserService) updateUserCollection(user string, ca cabby.CollectionAccess) error {
sql := `update taxii_user_collection set can_read = ?, can_write = ? where email = ?`
args := []interface{}{ca.CanRead, ca.CanWrite, user}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// User will read from the data store and populate the result with a resource
func (s UserService) User(ctx context.Context, user, password string) (cabby.User, error) {
resource, action := "User", "read"
start := cabby.LogServiceStart(ctx, resource, action)
result, err := s.user(user, password)
cabby.LogServiceEnd(ctx, resource, action, start)
return result, err
}
func (s UserService) user(user, password string) (cabby.User, error) {
sql := `select tu.email, tu.can_admin
from
taxii_user tu
inner join taxii_user_pass tup
on tu.email = tup.email
where tu.email = ? and tup.pass = ?`
args := []interface{}{user, hash(password)}
u := cabby.User{}
rows, err := s.DB.Query(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return u, err
}
defer rows.Close()
for rows.Next() {
if err := rows.Scan(&u.Email, &u.CanAdmin); err != nil {
return u, err
}
}
err = rows.Err()
return u, err
}
// UserCollections will read from the data store and populate the result with a resource
func (s UserService) UserCollections(ctx context.Context, user string) (cabby.UserCollectionList, error) {
resource, action := "UserCollectionList", "read"
start := cabby.LogServiceStart(ctx, resource, action)
result, err := s.userCollections(user)
cabby.LogServiceEnd(ctx, resource, action, start)
return result, err
}
func (s UserService) userCollections(user string) (cabby.UserCollectionList, error) {
sql := `select tuc.collection_id, tuc.can_read, tuc.can_write
from
taxii_user tu
inner join taxii_user_collection tuc
on tu.email = tuc.email
where tu.email = ?`
args := []interface{}{user}
ucl := cabby.UserCollectionList{Email: user, CollectionAccessList: map[cabby.ID]cabby.CollectionAccess{}}
rows, err := s.DB.Query(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return ucl, err
}
defer rows.Close()
for rows.Next() {
var ca cabby.CollectionAccess
if err := rows.Scan(&ca.ID, &ca.CanRead, &ca.CanWrite); err != nil {
return ucl, err
}
ucl.CollectionAccessList[ca.ID] = ca
}
return ucl, nil
}
/* helpers */
func hash(password string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(password)))
}
func validatePassword(password string) (err error) {
if len(password) < minPasswordLength {
return fmt.Errorf("Password length is too small, minimum length of characters is %d", minPasswordLength)
}
return
}
func validateUserPasswordCombo(user cabby.User, password string) error {
err := user.Validate()
if err != nil {
return err
}
return validatePassword(password)
}
func validateUserCollection(user string, ca cabby.CollectionAccess) error {
if user == "" {
return fmt.Errorf("User undefined")
}
if ca.ID.IsEmpty() {
return fmt.Errorf("Invalid collection ID")
}
return nil
}