forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlite3_opt_userauth_test.go
644 lines (563 loc) · 15 KB
/
sqlite3_opt_userauth_test.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
// Copyright (C) 2018 G.J.R. Timmer <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build sqlite_userauth
// +build sqlite_userauth
package sqlite3
import (
"database/sql"
"fmt"
"os"
"testing"
)
var (
conn *SQLiteConn
create func(t *testing.T, username, password string) (file string, err error)
createWithCrypt func(t *testing.T, username, password, crypt, salt string) (file string, err error)
connect func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error)
connectWithCrypt func(t *testing.T, f string, username, password string, crypt string, salt string) (file string, db *sql.DB, c *SQLiteConn, err error)
authEnabled func(db *sql.DB) (exists bool, err error)
addUser func(db *sql.DB, username, password string, admin int) (rv int, err error)
userExists func(db *sql.DB, username string) (rv int, err error)
isAdmin func(db *sql.DB, username string) (rv bool, err error)
modifyUser func(db *sql.DB, username, password string, admin int) (rv int, err error)
deleteUser func(db *sql.DB, username string) (rv int, err error)
)
func init() {
// Create database connection
sql.Register("sqlite3_with_conn",
&SQLiteDriver{
ConnectHook: func(c *SQLiteConn) error {
conn = c
return nil
},
})
create = func(t *testing.T, username, password string) (file string, err error) {
var db *sql.DB
file, db, _, err = connect(t, "", username, password)
db.Close()
return
}
createWithCrypt = func(t *testing.T, username, password, crypt, salt string) (file string, err error) {
var db *sql.DB
file, db, _, err = connectWithCrypt(t, "", "admin", "admin", crypt, salt)
db.Close()
return
}
connect = func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error) {
conn = nil // Clear connection
file = f // Copy provided file (f) => file
if file == "" {
// Create dummy file
file = TempFilename(t)
}
params := "?_auth"
if len(username) > 0 {
params = fmt.Sprintf("%s&_auth_user=%s", params, username)
}
if len(password) > 0 {
params = fmt.Sprintf("%s&_auth_pass=%s", params, password)
}
db, err = sql.Open("sqlite3_with_conn", "file:"+file+params)
if err != nil {
defer os.Remove(file)
return file, nil, nil, err
}
// Dummy query to force connection and database creation
// Will return ErrUnauthorized (SQLITE_AUTH) if user authentication fails
if _, err = db.Exec("SELECT 1;"); err != nil {
defer os.Remove(file)
defer db.Close()
return file, nil, nil, err
}
c = conn
return
}
connectWithCrypt = func(t *testing.T, f string, username, password string, crypt string, salt string) (file string, db *sql.DB, c *SQLiteConn, err error) {
conn = nil // Clear connection
file = f // Copy provided file (f) => file
if file == "" {
// Create dummy file
file = TempFilename(t)
}
db, err = sql.Open("sqlite3_with_conn", "file:"+file+fmt.Sprintf("?_auth&_auth_user=%s&_auth_pass=%s&_auth_crypt=%s&_auth_salt=%s", username, password, crypt, salt))
if err != nil {
defer os.Remove(file)
return file, nil, nil, err
}
// Dummy query to force connection and database creation
// Will return ErrUnauthorized (SQLITE_AUTH) if user authentication fails
if _, err = db.Exec("SELECT 1;"); err != nil {
defer os.Remove(file)
defer db.Close()
return file, nil, nil, err
}
c = conn
return
}
authEnabled = func(db *sql.DB) (exists bool, err error) {
err = db.QueryRow("select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';").Scan(&exists)
return
}
addUser = func(db *sql.DB, username, password string, admin int) (rv int, err error) {
err = db.QueryRow("select auth_user_add(?, ?, ?);", username, password, admin).Scan(&rv)
return
}
userExists = func(db *sql.DB, username string) (rv int, err error) {
err = db.QueryRow("select count(uname) from sqlite_user where uname=?", username).Scan(&rv)
return
}
isAdmin = func(db *sql.DB, username string) (rv bool, err error) {
err = db.QueryRow("select isAdmin from sqlite_user where uname=?", username).Scan(&rv)
return
}
modifyUser = func(db *sql.DB, username, password string, admin int) (rv int, err error) {
err = db.QueryRow("select auth_user_change(?, ?, ?);", username, password, admin).Scan(&rv)
return
}
deleteUser = func(db *sql.DB, username string) (rv int, err error) {
err = db.QueryRow("select auth_user_delete(?);", username).Scan(&rv)
return
}
}
func TestUserAuthCreateDatabase(t *testing.T) {
f, db, c, err := connect(t, "", "admin", "admin")
if err != nil && c == nil && db == nil {
t.Fatal(err)
}
defer db.Close()
defer os.Remove(f)
enabled, err := authEnabled(db)
if err != nil || !enabled {
t.Fatalf("UserAuth not enabled: %s", err)
}
e, err := userExists(db, "admin")
if err != nil {
t.Fatal(err)
}
if e != 1 {
t.Fatal("UserAuth: admin does not exists")
}
a, err := isAdmin(db, "admin")
if err != nil {
t.Fatal(err)
}
if !a {
t.Fatal("UserAuth: User is not administrator")
}
}
func TestUserAuthCreateDatabaseWithoutArgs(t *testing.T) {
_, db, c, err := connect(t, "", "", "")
if err == nil && c == nil && db == nil {
t.Fatal("Should have failed due to missing _auth_* parameters")
}
_, db, c, err = connect(t, "", "", "admin")
if err == nil && c == nil && db == nil {
t.Fatal("Should have failed due to missing _auth_user parameter")
}
_, db, c, err = connect(t, "", "admin", "")
if err == nil && c == nil && db == nil {
t.Fatal("Should have failed due to missing _auth_pass parameter")
}
}
func TestUserAuthLogin(t *testing.T) {
f1, err := create(t, "admin", "admin")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f1)
f2, db2, c2, err := connect(t, f1, "admin", "admin")
if err != nil {
t.Fatal(err)
}
defer db2.Close()
if f1 != f2 {
t.Fatal("UserAuth: Database file mismatch")
}
// Test lower level authentication
err = c2.Authenticate("admin", "admin")
if err != nil {
t.Fatalf("UserAuth: *SQLiteConn.Authenticate() Failed: %s", err)
}
// Test Login Failed
_, _, _, err = connect(t, f1, "admin", "invalid")
if err == nil {
t.Fatal("Login successful while expecting to fail")
}
if err != ErrUnauthorized {
t.Fatal(err)
}
err = c2.Authenticate("admin", "invalid")
if err == nil {
t.Fatal("Login successful while expecting to fail")
}
if err != ErrUnauthorized {
t.Fatal(err)
}
}
func TestUserAuthAddAdmin(t *testing.T) {
f, db, c, err := connect(t, "", "admin", "admin")
if err != nil && c == nil && db == nil {
t.Fatal(err)
}
defer db.Close()
defer os.Remove(f)
// Add Admin User through SQL call
rv, err := addUser(db, "admin2", "admin2", 1)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
// Check if user was created
exists, err := userExists(db, "admin2")
if err != nil {
t.Fatal(err)
}
if exists != 1 {
t.Fatal("UserAuth: 'admin2' does not exists")
}
// Check if user was created as an Administrator
admin, err := isAdmin(db, "admin2")
if err != nil {
t.Fatal(err)
}
if !admin {
t.Fatal("UserAuth: 'admin2' is not administrator")
}
// Test *SQLiteConn
err = c.AuthUserAdd("admin3", "admin3", true)
if err != nil {
t.Fatal(err)
}
// Check if user was created
exists, err = userExists(db, "admin2")
if err != nil {
t.Fatal(err)
}
if exists != 1 {
t.Fatal("UserAuth: 'admin3' does not exists")
}
// Check if the user was created as an Administrator
admin, err = isAdmin(db, "admin3")
if err != nil {
t.Fatal(err)
}
if !admin {
t.Fatal("UserAuth: 'admin3' is not administrator")
}
}
func TestUserAuthAddUser(t *testing.T) {
f1, db1, c, err := connect(t, "", "admin", "admin")
if err != nil && c == nil && db == nil {
t.Fatal(err)
}
defer os.Remove(f1)
// Add user through SQL call
rv, err := addUser(db1, "user", "user", 0)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
// Check if user was created
exists, err := userExists(db1, "user")
if err != nil {
t.Fatal(err)
}
if exists != 1 {
t.Fatal("UserAuth: 'user' does not exists")
}
// Check if user was created as an Administrator
admin, err := isAdmin(db1, "user")
if err != nil {
t.Fatal(err)
}
if admin {
t.Fatal("UserAuth: 'user' is administrator")
}
// Test *SQLiteConn
err = c.AuthUserAdd("user2", "user2", false)
if err != nil {
t.Fatal(err)
}
// Check if user was created
exists, err = userExists(db1, "user2")
if err != nil {
t.Fatal(err)
}
if exists != 1 {
t.Fatal("UserAuth: 'user2' does not exists")
}
// Check if the user was created as an Administrator
admin, err = isAdmin(db1, "user2")
if err != nil {
t.Fatal(err)
}
if admin {
t.Fatal("UserAuth: 'user2' is administrator")
}
// Reconnect as normal user
db1.Close()
_, db2, c2, err := connect(t, f1, "user", "user")
if err != nil {
t.Fatal(err)
}
defer db2.Close()
// Try to create admin user while logged in as normal user
rv, err = addUser(db2, "admin2", "admin2", 1)
if err != nil {
t.Fatal(err)
}
if rv != SQLITE_AUTH {
t.Fatal("Created admin user while not allowed")
}
err = c2.AuthUserAdd("admin3", "admin3", true)
if err != ErrAdminRequired {
t.Fatal("Created admin user while not allowed")
}
// Try to create normal user while logged in as normal user
rv, err = addUser(db2, "user3", "user3", 0)
if err != nil {
t.Fatal(err)
}
if rv != SQLITE_AUTH {
t.Fatal("Created user while not allowed")
}
err = c2.AuthUserAdd("user4", "user4", false)
if err != ErrAdminRequired {
t.Fatal("Created user while not allowed")
}
}
func TestUserAuthModifyUser(t *testing.T) {
f1, db1, c1, err := connect(t, "", "admin", "admin")
if err != nil && c1 == nil && db == nil {
t.Fatal(err)
}
defer os.Remove(f1)
// Modify Password for current logged in admin
// through SQL
rv, err := modifyUser(db1, "admin", "admin2", 1)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to modify password for admin")
}
// Modify password for current logged in admin
// through *SQLiteConn
err = c1.AuthUserChange("admin", "admin3", true)
if err != nil {
t.Fatal(err)
}
// Modify Administrator Flag
// Because we are current logged in as 'admin'
// Changing our own admin flag should fail.
rv, err = modifyUser(db1, "admin", "admin3", 0)
if err != nil {
t.Fatal(err)
}
if rv != SQLITE_AUTH {
t.Fatal("Successfully changed admin flag while not allowed")
}
// Modify admin flag through (*SQLiteConn)
// Because we are current logged in as 'admin'
// Changing our own admin flag should fail.
err = c1.AuthUserChange("admin", "admin3", false)
if err != ErrAdminRequired {
t.Fatal("Successfully changed admin flag while not allowed")
}
// Add normal user
rv, err = addUser(db1, "user", "password", 0)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
rv, err = addUser(db1, "user2", "user2", 0)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
// Modify other user password and flag through SQL
rv, err = modifyUser(db1, "user", "pass", 1)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to modify password for user")
}
// Modify other user password and flag through *SQLiteConn
err = c1.AuthUserChange("user", "newpass", false)
if err != nil {
t.Fatal(err)
}
// Disconnect database for reconnect
db1.Close()
_, db2, c2, err := connect(t, f1, "user", "newpass")
if err != nil {
t.Fatal(err)
}
defer db2.Close()
// Modify other user password through SQL
rv, err = modifyUser(db2, "user2", "newpass", 0)
if err != nil {
t.Fatal(err)
}
if rv != SQLITE_AUTH {
t.Fatal("Password change successful while not allowed")
}
// Modify other user password and flag through *SQLiteConn
err = c2.AuthUserChange("user2", "invalid", false)
if err != ErrAdminRequired {
t.Fatal("Password change successful while not allowed")
}
}
func TestUserAuthDeleteUser(t *testing.T) {
f1, db1, c, err := connect(t, "", "admin", "admin")
if err != nil && c == nil && db == nil {
t.Fatal(err)
}
defer os.Remove(f1)
// Add Admin User 2
rv, err := addUser(db1, "admin2", "admin2", 1)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
rv, err = addUser(db1, "admin3", "admin3", 1)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
// Check if user was created
exists, err := userExists(db1, "admin2")
if err != nil {
t.Fatal(err)
}
if exists != 1 {
t.Fatal("UserAuth: 'admin2' does not exists")
}
exists, err = userExists(db1, "admin3")
if err != nil {
t.Fatal(err)
}
if exists != 1 {
t.Fatal("UserAuth: 'admin2' does not exists")
}
// Delete user through SQL
rv, err = deleteUser(db1, "admin2")
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to delete admin2")
}
// Verify user admin2 deleted
exists, err = userExists(db1, "admin2")
if err != nil {
t.Fatal(err)
}
if exists != 0 {
t.Fatal("UserAuth: 'admin2' still exists")
}
// Delete user through *SQLiteConn
rv, err = deleteUser(db1, "admin3")
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to delete admin3")
}
// Verify user admin3 deleted
exists, err = userExists(db1, "admin3")
if err != nil {
t.Fatal(err)
}
if exists != 0 {
t.Fatal("UserAuth: 'admin3' still exists")
}
// Add normal user for reconnect and privileges check
rv, err = addUser(db1, "reconnect", "reconnect", 0)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
// Add normal user for deletion through SQL
rv, err = addUser(db1, "user", "user", 0)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
rv, err = addUser(db1, "user2", "user2", 0)
if err != nil {
t.Fatal(err)
}
if rv != 0 {
t.Fatal("Failed to add user")
}
// Close database for reconnect
db1.Close()
// Reconnect as normal user
_, db2, c2, err := connect(t, f1, "reconnect", "reconnect")
if err != nil {
t.Fatal(err)
}
defer db2.Close()
// Delete user while logged in as normal user
// through SQL
rv, err = deleteUser(db2, "user")
if err != nil {
t.Fatal(err)
}
if rv != SQLITE_AUTH {
t.Fatal("Successfully deleted user wthout proper privileges")
}
// Delete user while logged in as normal user
// through *SQLiteConn
err = c2.AuthUserDelete("user2")
if err != ErrAdminRequired {
t.Fatal("Successfully deleted user wthout proper privileges")
}
}
func TestUserAuthEncoders(t *testing.T) {
cases := map[string]string{
"sha1": "",
"ssha1": "salted",
"sha256": "",
"ssha256": "salted",
"sha384": "",
"ssha384": "salted",
"sha512": "",
"ssha512": "salted",
}
for enc, salt := range cases {
f, err := createWithCrypt(t, "admin", "admin", enc, salt)
if err != nil {
t.Fatal(err)
}
defer os.Remove(f)
_, db, _, err := connectWithCrypt(t, f, "admin", "admin", enc, salt)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if e, err := authEnabled(db); err != nil && !e {
t.Fatalf("UserAuth (%s) not enabled %s", enc, err)
}
}
}