-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
351 lines (306 loc) · 10.3 KB
/
db.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
342
343
344
345
346
347
348
349
350
351
package main
import (
"database/sql"
"encoding/base32"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
)
type dbRes struct {
email string
password []byte
refreshToken string
emailToken *string
sms *string
smsVerified *time.Time
totp *string
totpVerified *time.Time
errorCount int
flowType string
metaSystem *string
}
func findAuthByEmail(email string) (*dbRes, error) {
var res dbRes
var pw string
query := `SELECT sms, password, meta_system, refresh_token,
email_token, totp, sms_verified, totp_verified, error_count, flow_type
FROM auth WHERE email = $1`
err := db.QueryRow(query, email).Scan(
&res.sms, &pw, &res.metaSystem, &res.refreshToken, &res.emailToken,
&res.totp, &res.smsVerified, &res.totpVerified, &res.errorCount, &res.flowType)
if err != nil {
return nil, err
}
res.password, err = base32.StdEncoding.DecodeString(pw)
if err != nil {
return nil, err
}
res.email = email
return &res, nil
}
func insertUser(email string, pwRaw []byte, emailToken string, refreshToken string, flowType string, now time.Time) error {
var pw *string
if pwRaw != nil {
s1 := base32.StdEncoding.EncodeToString(pwRaw)
pw = &s1
}
stmt, err := db.Prepare(`INSERT INTO auth as a (email, password, email_token, refresh_token, flow_type, created_at)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (email) DO
UPDATE SET password=$2, email_token = $3
WHERE a.email_token IS NOT NULL`)
if err != nil {
return fmt.Errorf("prepare INSERT INTO auth for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(email, pw, emailToken, refreshToken, flowType, now)
return handleErr(res, err, "INSERT INTO auth", email)
}
func deleteDbUser(email string) error {
stmt, err := db.Prepare(`DELETE FROM auth where email = $1`)
if err != nil {
return fmt.Errorf("prepare INSERT INTO auth for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(email, email)
return handleErr(res, err, "DELETE FROM auth", email)
}
func updateRefreshToken(oldRefreshToken string, newRefreshToken string) error {
stmt, err := db.Prepare("UPDATE auth SET refresh_token = $1 WHERE refresh_token = $2")
if err != nil {
return fmt.Errorf("prepare UPDATE refreshTokenfor statement failed: %v", err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(newRefreshToken, oldRefreshToken)
return handleErrEffect(res, err, "UPDATE refreshToken", "n/a", false)
}
func updateSystemMeta(email string, systemMeta string) error {
stmt, err := db.Prepare("UPDATE auth SET meta_system = $1 WHERE email=$2")
if err != nil {
return fmt.Errorf("prepare UPDATE meta_system statement failed: %v", err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(systemMeta, email)
return handleErr(res, err, "UPDATE meta_system", "n/a")
}
func updatePasswordInvite(email string, emailToken string, newPw []byte) error {
pw := base32.StdEncoding.EncodeToString(newPw)
stmt, err := db.Prepare(`UPDATE auth SET password = $1, email_token = NULL
WHERE email = $2 AND email_token = $3 AND password IS NULL`)
if err != nil {
return fmt.Errorf("prepare UPDATE auth password for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(pw, email, emailToken)
return handleErr(res, err, "UPDATE auth password invite", email)
}
func updatePasswordForgot(email string, forgetEmailToken string, newPw []byte) error {
pw := base32.StdEncoding.EncodeToString(newPw)
stmt, err := db.Prepare(`UPDATE auth SET password = $1, totp = NULL, sms = NULL, forget_email_token = NULL
WHERE email = $2 AND forget_email_token = $3`)
if err != nil {
return fmt.Errorf("prepare UPDATE auth password for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(pw, email, forgetEmailToken)
return handleErr(res, err, "UPDATE auth password", email)
}
func updateEmailForgotToken(email string, token string) error {
//TODO: don't accept too old forget tokens
stmt, err := db.Prepare("UPDATE auth SET forget_email_token = $1 WHERE email = $2")
if err != nil {
return fmt.Errorf("prepare UPDATE auth forgetEmailToken for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(token, email)
return handleErr(res, err, "UPDATE auth forgetEmailToken", email)
}
func updateTOTP(email string, totp string) error {
stmt, err := db.Prepare("UPDATE auth SET totp = $1 WHERE email = $2 and totp IS NULL")
if err != nil {
return fmt.Errorf("prepare UPDATE auth totp for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(totp, email)
return handleErr(res, err, "UPDATE auth totp", email)
}
func updateSMS(email string, totp string, sms string) error {
stmt, err := db.Prepare("UPDATE auth SET totp = $1, sms = $2 WHERE email = $3 AND sms_verified IS NULL")
if err != nil {
return fmt.Errorf("prepare UPDATE auth totp for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(totp, sms, email)
return handleErr(res, err, "UPDATE auth totp", email)
}
func updateEmailToken(email string, token string) error {
stmt, err := db.Prepare("UPDATE auth SET email_token = NULL WHERE email = $1 AND email_token = $2")
if err != nil {
return fmt.Errorf("prepare UPDATE auth for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(email, token)
return handleErr(res, err, "UPDATE auth", email)
}
func updateSMSVerified(email string, now time.Time) error {
stmt, err := db.Prepare("UPDATE auth SET sms_verified = $1 WHERE email = $2 AND sms IS NOT NULL")
if err != nil {
return fmt.Errorf("prepare UPDATE auth for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(now, email)
return handleErr(res, err, "UPDATE auth SMS timestamp", email)
}
func updateTOTPVerified(email string, now time.Time) error {
stmt, err := db.Prepare("UPDATE auth SET totp_verified = $1 WHERE email = $2 AND totp IS NOT NULL")
if err != nil {
return fmt.Errorf("prepare UPDATE auth for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(now, email)
return handleErr(res, err, "UPDATE auth totp timestamp", email)
}
func incErrorCount(email string) error {
stmt, err := db.Prepare("UPDATE auth set error_count = error_count + 1 WHERE email = $1")
if err != nil {
return fmt.Errorf("prepare UPDATE auth status for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(email)
return handleErr(res, err, "UPDATE auth errorCount", email)
}
func resetCount(email string) error {
stmt, err := db.Prepare("UPDATE auth set error_count = 0 WHERE email = $1")
if err != nil {
return fmt.Errorf("prepare UPDATE auth status for %v statement failed: %v", email, err)
}
defer closeAndLog(stmt)
res, err := stmt.Exec(email)
return handleErr(res, err, "UPDATE auth status", email)
}
func handleErr(res sql.Result, err error, info string, email string) error {
return handleErrEffect(res, err, info, email, true)
}
func handleErrEffect(res sql.Result, err error, info string, email string, mustHaveEffect bool) error {
if err != nil {
return fmt.Errorf("%v query %v failed: %v", info, email, err)
}
nr, err := res.RowsAffected()
if mustHaveEffect && nr == 0 {
return fmt.Errorf("%v rows %v, affected: %v", info, nr, email)
}
if err != nil {
return fmt.Errorf("%v %v err: %v", info, email, err)
}
return nil
}
///////// Setup
// Meta data can be additional information that will be encoded in the JWT token
func addInitialUserWithMeta(username string, password string, metaSystem *string) error {
res, err := findAuthByEmail(username)
if res == nil || err != nil {
dk, err := newPw(password, 0)
if err != nil {
return err
}
err = insertUser(username, dk, "emailToken", "refreshToken", "sys", timeNow())
if err != nil {
return err
}
err = updateEmailToken(username, "emailToken")
if err != nil {
return err
}
if metaSystem != nil {
if !json.Valid([]byte(*metaSystem)) {
return fmt.Errorf("not valid json: %v", *metaSystem)
}
err = updateSystemMeta(username, *metaSystem)
if err != nil {
return err
}
}
}
return nil
}
func initDB() (*sql.DB, error) {
db, err := sql.Open(opts.DBDriver, opts.DBPath)
if err != nil {
return nil, err
}
err = db.Ping()
now := time.Now()
for err != nil && now.Add(time.Duration(10)*time.Second).After(timeNow()) {
time.Sleep(time.Second)
err = db.Ping()
}
if err != nil {
return nil, err
}
//this will create or alter tables
//https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go
for _, file := range strings.Split(opts.DBScripts, ":") {
if file == "" {
continue
}
//https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go
if _, err := os.Stat(file); err == nil {
fileBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
//https://stackoverflow.com/questions/12682405/strip-out-c-style-comments-from-a-byte
re := regexp.MustCompile("(?s)//.*?\n|/\\*.*?\\*/|(?s)--.*?\n|(?s)#.*?\n")
newBytes := re.ReplaceAll(fileBytes, nil)
requests := strings.Split(string(newBytes), ";")
for _, request := range requests {
request = strings.TrimSpace(request)
if len(request) > 0 {
_, err := db.Exec(request)
if err != nil {
return nil, fmt.Errorf("[%v] %v", request, err)
}
}
}
} else {
log.Printf("ignoring file [%v] (%v)", file, err)
}
}
log.Infof("Successfully connected!")
return db, nil
}
func setupDB() {
if opts.Users != "" {
//add user for development
users := strings.Split(opts.Users, ";")
for _, user := range users {
userPwMeta := strings.SplitN(user, ":", 3)
var metaSystem *string
if len(userPwMeta) >= 3 {
metaSystem = &userPwMeta[2]
}
if len(userPwMeta) >= 2 {
err := addInitialUserWithMeta(userPwMeta[0], userPwMeta[1], metaSystem)
if err == nil {
log.Printf("insterted user %v", userPwMeta[0])
} else {
log.Printf("could not insert %v: %v", userPwMeta[0], err)
}
} else {
log.Printf("username and password need to be seperated by ':'")
}
}
}
}
func closeAndLog(c io.Closer) {
err := c.Close()
if err != nil {
log.Printf("could not close: %v", err)
}
}