Skip to content

Commit 183e7d6

Browse files
committed
UPD: User Authentication
Implemented table check; only activate User Authentication on a database which has no UA enabled. Closes mattn#582
1 parent 2d9b52a commit 183e7d6

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

sqlite3.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -1368,14 +1368,8 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
13681368
return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
13691369
}
13701370

1371-
// TODO: Table exists check for table 'sqlite_user'
1372-
// replace 'authExists := false' with return value of table exists check
1373-
//
1374-
// REPLACE BY RESULT FROM TABLE EXISTS
1375-
// SELECT count(type) as exists FROM sqlite_master WHERE type='table' AND name='sqlite_user';
1376-
// Scan result 'exists' and use it instead of boolean below.
1377-
authExists := false
1378-
1371+
// Check if User Authentication is Enabled
1372+
authExists := conn.AuthIsEnabled()
13791373
if !authExists {
13801374
if err := conn.AuthUserAdd(authUser, authPass, true); err != nil {
13811375
return nil, err

sqlite3_opt_userauth.go

+27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ _sqlite3_user_delete(sqlite3* db, const char* zUsername)
4040
{
4141
return sqlite3_user_delete(db, zUsername);
4242
}
43+
44+
static int
45+
_sqlite3_auth_is_enabled(sqlite3* db)
46+
{
47+
int exists = -1;
48+
49+
sqlite3_stmt *stmt;
50+
sqlite3_prepare_v2(db, "select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';", -1, &stmt, NULL);
51+
52+
while ( sqlite3_step(stmt) == SQLITE_ROW) {
53+
exists = sqlite3_column_int(stmt, 0);
54+
}
55+
56+
sqlite3_finalize(stmt);
57+
58+
return exists;
59+
}
4360
*/
4461
import "C"
4562
import (
@@ -165,4 +182,14 @@ func (c *SQLiteConn) AuthUserDelete(username string) error {
165182
return nil
166183
}
167184

185+
// Check is database is protected by user authentication
186+
func (c *SQLiteConn) AuthIsEnabled() (exists bool) {
187+
rv := C._sqlite3_auth_is_enabled(c.db)
188+
if rv == 1 {
189+
exists = true
190+
}
191+
192+
return
193+
}
194+
168195
// EOF

0 commit comments

Comments
 (0)